Python tutorial¶
- official website : http://www.python.org
- official tutorial : http://docs.python.org/tutorial/
- introduction to python programming : http://femhub.com/textbook-python/
- google-style recomendations : http://google-styleguide.googlecode.com/svn/trunk/pyguide.html
- a tutorial in French: http://www.siteduzero.com/informatique/tutoriels/apprenez-a-programmer-en-python
Using python with the interpreter¶
using python with the interpreter:
$ python
[GCC 4.4.4 20100503 (Red Hat 4.4.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
or using the more advanced interactive python interperter ipython:
$ ipython
Python 2.6.6 (r266:84292, Jan 4 2012, 16:09:28)
Type "copyright", "credits" or "license" for more information.
IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
In [1]:
Simple examples:
>>> x = 1
>>> y = 2.
>>> type(x)
<type 'int'>
>>> type(y)
<type 'float'>
>>>
Operations:
>>> x+y
3.0
>>> z = x+y
>>> x==y
False
>>> x!=y
True
>>> x>y
False
Basic python objects¶
strings:
>>> name = "Meylan"
>>> name[0]
'M'
>>> name[2:4]
'yl'
dictionary:
>>> onglet={'observatoire':'obswww.unige.ch','tsr':'www.tsr.ch'}
>>> type(onglet)
<type 'dict'>
>>> onglet['observatoire']
'obswww.unige.ch'
>>> onglet['tsr']
'www.tsr.ch'
>>> onglet['epfl'] = "www.epfl.ch"
>>> onglet.keys()
['epfl', 'tsr', 'observatoire']
>>> onglet.has_key('tsr')
True
lists:
>>> trousse=['gommme', 'crayon', 'plume', 1, 1.2]
>>> type(trousse)
<type 'list'>
>>> trousse[0]
'gommme'
>>> trousse[-1]
1.2
>>> trousse.index('crayon')
1
>>> trousse.pop()
1.2
>>> trousse.pop()
1
>>> trousse.append("compas")
Control Flow Tools¶
- if/else/for/while/range/xrange/break/continue/pass
if:
>>> x = 0
>>> if x==1:
... print "x equal 1"
... else:
... print "x is not equal to 1"
...
x is not equal to 1
for:
>>> for x in trousse:
... print x
...
gommme
crayon
plume
compas
>>> for x in xrange(10):
... print x
...
0
1
2
3
4
5
6
7
8
9
break:
>>> for x in xrange(10):
... print x
... if x==5:
... break
...
0
1
2
3
4
5
continue:
>>> for x in xrange(10):
... print x
... if x==5:
... break
... else:
... continue
...
0
1
2
3
4
5
while:
>>> x = 0
>>> while(x<5):
... print x
... x+=1
...
0
1
2
3
4
Functions¶
Define functions:
>>> def multiply(x,y):
... z = x*y
... return z
...
>>> multiply(2,3)
6
with optional arguments:
>>> def multiply(x,y,verbose=False):
... if verbose:
... print "computing x*y"
... z = x*y
... return z
...
>>> multiply(2,3)
6
>>> multiply(2,3,True)
computing x*y
6
>>> multiply(2,3,verbose=True)
computing x*y
6
>>>
Files¶
Open and write into a file:
>>> fd=open('fichier.txt','w')
>>> fd.write("tu vois comme c'est simple python !\n")
>>> fd.close()
Open and read a file:
>>> fd=open('fichier.txt','r')
>>> lines = fd.readlines()
>>> fd.close()
>>>
>>> print lines
["tu vois comme c'est simple python !\n"]
Miscellaneous operating system interfaces¶
>>> import os
>>> import glob
>>> import shutil
>>> os.mkdir('test')
>>> os.chdir('test')
>>> os.getcwd()
>>> open('file1','w')
>>> os.symlink('file1','file2')
>>> os.mkdir('directory')
>>> glob.glob('*')
>>> os.listdir('.')
>>> os.path.isfile('file1')
>>> os.path.isfile('file2')
>>> os.path.isfile('file3')
>>> os.path.isfile('directory')
>>> os.path.isdir('directory')
>>> os.remove('file1')
>>> os.remove('directory')
>>> os.removedirs('directory')
>>> os.chdir('../')
>>> shutil.rmtree('test')
Scripting python¶
open/create a file:
$ gedit myscript.py
add a header:
#!/usr/bin/env python
add your python commands:
#!/usr/bin/env python print "Hello world !"
save the file
make it executable:
$ chmod a+x myscript.py
launch it !:
$ ./myscript.py Hello world !