This is a minimal crash-course in the programming language Python. To learn more, take a look at the documentation at the Python web site, www. python. org; especially the tutorial. If you wonder why you should be interested, check out the comparison page where Python is compared to other languages.
This introduction has been translated into several languages, among them Portuguese, Italian, Spanish, Russian, French, Lithuanian, Japanese, German and Greek, and is currently being translated into Norwegian, Polish, and Korean. Since this document still might undergo changes, these translations may not always be up to date.
Note: To get the examples working properly, write the programs in a text file and then run that with the interpreter; do not try to run them directly in the interactive interpreter – not all of them will work. (Please do not ask me for details on this! I get swamped with emails on the subject… Check the documentation, or send an email to help@python. org).
To begin with, think of Python as pseudo-code. It’s almost true. Variables don’t have types, so you don’t have to declare them. They appear when you assign to them, and disappear when you don’t use them anymore. Assignment is done by the = operator. Equality is tested by the == operator. You can assign several variables at once:
X, y, z = 1,2,3
First, second = second, first
A = b = 123
Blocks are indicated through indentation, and only through indentation. (No BEGIN/END or braces.) Some common control structures are:
If x < 5 or (x > 10 and x < 20):
print “The value is OK.”
If x < 5 or 10 < x < 20:
print “The value is OK.”
For i in [1,2,3,4,5]:
print “This is iteration number”, i
X = 10
While x >= 0:
print “x is still not negative.”
x = x-1
/> The first two examples are equivalent.
The index variable given in the for loop iterates through the elements of a list (written as in the example). To make an “ordinary” for loop (that is, a counting loop), use the built-in function range().
# Print out the values from 0 to 99 inclusive.
For value in range(100):
(The line beginning with “#” is a comment, and is ignored by the interpreter.)
Okay; now you know enough to (in theory) implement any algorithm in Python. Let’s add some basic user interaction. To get input from the user (from a text prompt), use the builtin function input.
X = input(“Please enter a number: “)
Print “The square of that number is”, x*x
The input function displays the prompt given (which may be empty) and lets the user enter any valid Python value. In this case we were expecting a number – if something else (like a string) is entered, the program would crash. To avoid that we would need some error checking. I won’t go into that here; suffice it to say that if you want the user input stored verbatim as a string (so that anything can be entered), use the function raw_input instead. If you wanted to convert the input string s to an integer, you could then use int(s).
Note: If you want to input a string with input, the user has to write the quotes explicitly. In Python, strings can be enclosed in either single or double quotes.
So, we have control structures, input and output covered – now we need some snazzy data structures. The most important ones are lists and dictionaries. Lists are written with brackets, and can (naturally) be nested:
Name = [“Cleese”, “John”]
X = [[1,2,3],[y, z],[[]]]