0% found this document useful (0 votes)
2 views

pythontest

This document is an introduction to Google's Python online tutorial, covering Python 3 and its features. It explains the dynamic and interpreted nature of Python, the use of the Python interpreter for experimentation, and the structure of Python source files. Additionally, it discusses how to run Python modules and handle command-line arguments.

Uploaded by

daomaithuytung
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

pythontest

This document is an introduction to Google's Python online tutorial, covering Python 3 and its features. It explains the dynamic and interpreted nature of Python, the use of the Python interpreter for experimentation, and the structure of Python source files. Additionally, it discusses how to run Python modules and handle command-line arguments.

Uploaded by

daomaithuytung
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Introduction

bookmark_border

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 nd 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 exible, and you lose the compile-time type checking of the
source code. Python tracks the types of all values at runtime and
ags 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!)
fl
fl
fi
$ 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 "", line 1, in
TypeError: can only concatenate str (not "int") to str
>>> a + str(len(a)) ## probably what you really wanted
'hi2'
>>> foo ## try something else that doesn't work
Traceback (most recent call last):
File "", line 1, in
NameError: name 'foo' is not de ned
>>> ^D ## type CTRL-d to exit (CTRL-z in Windows/DOS
terminal)
The two lines python prints after you type python and before the
>>> prompt tells you about the version of python you're using and
where it was built. As long as the rst thing printed is "Python 3.",
these examples should work for you.

As you can see above, it's easy to experiment with variables and
operators. Also, the interpreter throws, or "raises" in Python
fi
fi
parlance, a runtime error if the code tries to read a variable that
has not been assigned a value. Like C++ and Java, Python is
case sensitive so "a" and "A" are different variables. The end of a
line marks the end of a statement, so unlike C++ and Java,
Python does not require a semicolon at the end of each
statement. Comments begin with a '#' and extend to the end of
the line.

Python source code


Python source les use the ".py" extension and are called
"modules." With a Python module hello.py, the easiest way to run
it is with the shell command "python hello.py Alice" which calls the
Python interpreter to execute the code in hello.py, passing it the
command line argument "Alice". See the of cial docs page on all
the different options you have when running Python from the
command-line.

Here's a very simple hello.py program (notice that blocks of code


are delimited strictly using indentation rather than curly braces —
more on this later!):

#!/usr/bin/python3

# import modules used here -- sys is a very standard one


import sys

# Gather our code in a main() function


def main():
print('Hello there', sys.argv[1])
# Command line args are in sys.argv[1], sys.argv[2] ...
# sys.argv[0] is the script name itself and can be ignored
fi
fi
# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
main()
Running this program from the command line looks like:

$ python3 hello.py Guido


Hello there Guido
$ ./hello.py Alice ## without needing 'python3' rst (Unix)
Hello there Alice
Imports, Command-line arguments, and len()
The outermost statements in a Python le, or "module", do its
one-time setup — those statements run from top to bottom the
rst time the module is imported somewhere, setting up its
variables and functions. A Python module can be run directly —
as above python3 hello.py Bob — or it can be imported and used
by some other module. When a Python le is run directly, the
special variable "__name__" is set to "__main__". Therefore, it's
common to have the boilerplate if __name__ ==... shown above
to call a main() function when the module is run directly, but not
when the module is imported by some other module.

In a standard Python program, the list sys.argv contains the


command-line arguments in the standard way with sys.argv[0]
being the program itself, sys.argv[1] the rst argument, and so on.
If you know about argc, or the number of arguments, you can
simply request this value from Python with len(sys.argv), just like
we did in the interactive interpreter code above when requesting
the length of a string. In general, len() can tell you how long a
string is, the number of elements in lists and tuples (another
array-like data structure), and the number of key-value pairs in a
dictionary.
fi
fi
fi
fi
fi

You might also like