Page Summary
-
Google's Python online tutorial is based on an internal introductory course and covers Python 3.
-
Python is a dynamic, interpreted language where type declarations are not required, allowing for flexible code but deferring type checking until runtime.
-
Python source code files use the ".py" extension and are called modules, which can be run directly or imported by other modules.
-
In Python, indentation strictly defines code blocks, and using 4 spaces for indentation is the recommended style.
-
You can get help with Python by using online resources, official documentation, mailing lists, Q&A sites, and the built-in
help()anddir()functions in the interpreter.
Prelude
Welcome to Google's Python online tutorial. It is based on the introductory Python course offered internally. As mentioned on the setup page, this material covers Python 3.
If you're seeking a companion MOOC course, try the ones from Udacity and Coursera (intro to programming [beginners] or intro to Python). Finally, if you're seeking self-paced online learning without watching videos, try the ones listed towards the end of this post — each feature learning content as well as a Python interactive interpreter you can practice with. What's this "interpreter" we mention? You'll find out in the next section!
Language Introduction
Python is a dynamic, interpreted (bytecode-compiled) language. There are no type declarations of variables, parameters, functions, or methods in source code. This makes the code short and flexible, and you lose the compile-time type checking of the source code. Python tracks the types of all values at runtime and flags code that does not make sense as it runs.
An excellent way to see how Python code works is to run the Python interpreter and type code right into it. If you ever have a question like, "What happens if I add an int to a list?" Just typing it into the Python interpreter is a fast and likely the best way to see what happens. (See below to see what really happens!)
$ python3 ## Run the Python interpreter Python 3.X.X (XXX, XXX XX XXXX, XX:XX:XX) [XXX] on XXX Type "help", "copyright", "credits" or "license" for more information. >>> a = 6 ## set a variable in this interpreter session >>> a ## entering an expression prints its value 6 >>> a + 2 8 >>> a = 'hi' ## 'a' can hold a string just as well >>> a 'hi' >>> len(a) ## call the len() function on a string 2 >>> a + len(a) ## try something that doesn't work Traceback (most recent call last): File ""