Python: A Tutorial



Index

  1. Introduction
  2. Python as a Calculator
  3. Loops & Flow Control
  4. Editing Program Files
  5. Programs & Subroutines

1. Introduction

Some reference problems will be used to guide this tour of the Python programming language:

  1. Matrix Arithmetic
  2. SVG Graphics
  3. Web Crawler


2. Python as a Calculator

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:

+ Add:
- Subtract:
* Multiply:
a*b is a times b
/ Divide:
a/b is a divided by b. If either of a or b is a decimal number this behaves as expected, but if both a and b are integers the result will be an integer, truncating to throw away the remainder. (If the operator "//" is used, it will always produce a truncated integer result.)
% Remainder:
a%b is the remainder left when a is divided by b, producing a truncated integer quotient.
** Exponentiate:
a**b is ab, a raised to the b power.

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


3. Loops & Flow Control

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.

>>> 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


4. Editing Program Files

One Day of IDLE Toying [http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html]


5. Programs & Subroutines

print
Robert Campbell, campbell@math.umbc.edu
19 June, 2006