Some reference problems will be used to guide this tour of the Python programming language:
The simplest use of Python is as a calculator. Start the Python command line - in UNIX or Mac OSX type the command "python", in Windows start the IDLE command line (if you have installed it).
You should see a ">>>" prompt. Type in an expression and hit the Return key to execute it:
>>> 5+3 8 >>> (3*2-5*2) -4 >>> 27 % 5 2 >>> (3.2 + 2.1 + 2*5.2)/4 3.9250000000000003 >>> 2**100 1267650600228229401496703205376L
We have used the following basic arithmetic operators:
Variables can be used as in algebra to store values which are later used. A command of the form "x=2" will place the value 2 into the variable named "x" and the expression "3*x" will have the value 6. Additionally, we can recall the result of the last computation with the underscore character, "_". This can be used anywhere within an expression.
Comments can be added to a line of Python by preceding them with the character "#". After this character any code in a line will be ignored.
Python supports numbers of several different types:
A variety of math functions and constants are not in the core of Python, but can be
imported from the Python math library with the import command. These functions include
the logarithmic and trig functions, and are documented in
[http://docs.python.org/lib/module-math.html].
A variety of useful functions are found in other standard Python libraries.
Example:
>>> complex(2,4) # Build a complex number (2+4j) >>> _.real # Extract the real part of the last result 2.0 >>> from math import * # Allow Python to use sqrt(), sin(), atan(), etc >>> exp(2.5) # Compute the exponential function 12.182493960703473 >>> log(_) 2.5 >>> v = 3.0 + 6.0j # Build another complex number >>> sqrt((v.real)**2 + (v.imag)**2) # Compute its length 6.7082039324993694 >>> atan((v.imag)/(v.real)) # Compute its angle 1.1071487177940904 >>> _ * 180/pi # Covert this to degrees (the degrees() function also works) 63.43494882292201
The real value of a programming language comes when you use it to execute many commands. This requires some way to repeat things or control the flow of execution. Python has a variety of flow control functions, most of them similar to what is found in other languages.
>>> fact = 1 >>> for i in [1,2,3,4,5,6,7,8,9,10]: # Compute 10 factorial (1*2*...*9*10) ... fact = fact * i ... >>> fact = 1 3628800
While some of these commands can be written on a single line, they are all thought of as multiple line constructions. The first line is the control struction. Following lines form the body and are indented by a tab. In the body of a control structure the prompt changes from ">>>" to "...". The body ends with a line which is not indented, either a black line, another control line, or a further line in the code.
for x in [a,b,c,d]:" will execute the loop
body four times, first with x taking the value a, then with x taking the value b, and
so on. A useful function to use with for is range(a,b), which produces a list starting
at a and ending at b-1, or range(a,b,step).>>> fact = 1 >>> for i in [1,2,3,4,5,6,7,8,9,10]: # Compute 10 factorial (1*2*...*9*10) ... fact = fact * i ... >>> fact = 1 3628800
One Day of IDLE Toying [http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html]