Python is a dynamic object-oriented programming language with extensive standard libraries. These notes summarize the Python 3.0 tutorial.
The Interpreter
python/python3.0 is the command-line interpreter with readline where available.
python -c command runs a single command.
sys.argv is a list of strings with the script if any in sys.argv[0]
»> prompts for a line . … prompts for a continuation of a previous line
PYTHONSTARTUP can point to a file of startup commands
Expressions are evaluated and printed.
The last printed expression is assigned to _ .
Basics
Comments start with # .
Variables can be assigned (using = ) without being defined but must be assigned before being used.
a,b = c,d does a multiple assignment.
Imaginary numbers can be created using complex(a,b) or a+bJ. .real and .imag will extract real and imaginary parts.
print(“a”,“b”,end=“”) prints a b with no newline at the end.
Lists
list=[1 ,'a' , “b”] is a list of three items.
list[4] is 4th element. list[2:4] is second and third elements. list[:2] is first 2 elements. list[2:] is third element to last one. list[:] is the whole list.
Negative indexes count from last element.
List slices can be modified. list[3:3] can be used to insert elements at position 3. list.append() appends to the list.
len(list) returns length of the list.
Lists can be nested.
Strings
Strings can be enclosed with double or single-quotes.
\ in a string by itself goes to the next line but does not insert a newline (\n does).
A raw string - r“” does no escaping.
+ concatenates strings, * repeats them .
A string can be treated as a list of letters.
But strings can't be modified using slices or indexes.