Home turboPC
Python scripts Python tutorial Bouncing dice Diagram editor LRC network simulation
Qt embedded
OS Bootfloppy Go 64 bits Bochs

Python tutorial

This is an attempt to provide a short tutorial for python beginners. Python is an interpreted programming language which is both simple and powerfull.

In linux, you can install the python program using your package manager. In windows there exists the python installer, available from the python website.

Python comes with the interpreter. After starting python, you will see its command prompt:

Python 3.2.2 (default, Sep  5 2011, 04:52:19) 
[GCC 4.6.1 20110819 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Here you can type commands. This is handy to try commands before putting them in a script. As you can see we are using python version 3 here.

Now we can assign variables and do some math with them:

>>> a = 2
>>> b = 44
>>> c = a + b
>>> c
46
>>> a
2
>>> b + 33
77
>>> 

Now with the dir command we can list the currently present variables. With type one can find out what type the variable is. By importing the math module, we gain access to the math functions that ship with python.

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'b', 'c']
>>> type(a)
<class 'int'>
>>> import math
>>> dir(math)
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh',
 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc',
 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma',
 'hypot', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p',
 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
>>> 

Now, say we want to calculate the square root of c, lets do that with the math module:

>>> math.sqrt(c)
6.782329983125268
>>> 

Lets define a function that adds to numbers. A function is defined by the 'def' keyword, following a name, and then a colon. All rows below it with the same indentation belong to the function. Indentation determines the programs structure.

>>> def addup(a,b):
...    return a+b
... 
>>> addup(2,3)
5
>>> type(addup)
<class 'function'>
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'addup']
>>> 

With the builtin functions 'dir' we see that the function 'addup' is now present. With the builtin function 'type' we verify that the object is indeed a function.

To create a list of elements, use brackets.

>>> l = [1,2,3]
>>> l.append(4)
>>> l.pop(0)
1
>>> l
[2, 3, 4]
>>> type(l)
<class 'list'>
>>> l.append(44)
>>> l
[2, 3, 4, 44]
>>> 

A list can contain all kind of objects:

>>> l.append('hello world')
>>> l
[2, 3, 4, 44, 'hello world']
>>> l.append(addup)
>>> l
[2, 3, 4, 44, 'hello world', <function addup at 0x7f64bedfd628>]
>>> l.append(addup(9,11))
>>> l
[2, 3, 4, 44, 'hello world', <function addup at 0x7f64bedfd628>, 20]
>>> 

One can for example call the function addup that was stored in the list:

>>> l[5]
<function addup at 0x7f64bedfd628>
>>> l[5](4,4)
8
>>> 

To demonstrate the use of the builtin function print, we are going to iterate over the elements in our list:

>>> print('hello world')
hello world                                                                                                                          
>>> l                                                                                                                                
[2, 3, 4, 44, 'hello world', <function addup at 0x7f64bedfd628>, 20]                                                                 
>>> for x in l:                                                                                                                      
...    print(x)
...                                                                                                                                  
2                                                                                                                                    
3                                                                                                                                    
4                                                                                                                                    
44                                                                                                                                   
hello world                                                                                                                          
<function addup at 0x7f64bedfd628>                                                                                                   
20                                                                                                                                   
>>>