Memo
A mental map on the Python Programming Language
Resources
Websites
Python.org
Docs
English
French
La doc. de G. Swinnen
map info
created 2008-07-05 nliebeaux@free.fr
PythonModules
os
current working dir
dummyDir = getcwd()
change dir
chdir('/tmp')
from os import *
math
trigonometry
number = sin(2)
square root
number = sqrt(2)
from math import *
Oriented-ObjectProgrammation
Class Definition
class myNewClass: def __init__(self,...):
PythonLibraries
Misc.
Py2Exe (for Win only)
I/O Ports
Pyparallel (PC parallel port)
Pyserial (PC serial port)
Multimedia & Gaming
Graphics
Scientific computing
User Interface libs
TKinter
website
SyntaxRules
Respect indentation
No ";" line ending
Variables
Operators
Logical op.
not
or
and
power : **
5**2
Useful variablesfunctions
Tuples
conversion
str(myNumber)
dummyStr = 'Steelers vs Dolphins'
Dictionnaries
dummyDic3 = {[1,5]:"metro", [12,5]:"bus", [4,4]:"truck"}
dummyDic2 = {1:"abc", 2:"def".71}
dummyDic1 = {"a":5, "b":11, "c":24}
Lists
dummyList = [5, 'toto', 2.71]
list inversion
dummyList.reverse()
list search
dummyList.index()
list sort
dummyList.sort()
list append
dummyList.append()
list go through
for each x in dummyList
list element check
x in dummyList
list repeat
dummyList*5
lists concatenation
List1+List2
list length
len(dummyList)
an powerful array-like structure
CommonDeclarations
global variable declaration
global fieldGoal
Strings
special methods avalaible
string uppercase
dummyStr.upper()
littleText[-3]
Negative index means "backward"
littleText[3]
"Go Patriots !"
Starting index = 0
littleText = "Go Patriots !"
Floats
number = 3.14159
Integers
number = 5
no type declaration needed
Files
close a file
idFile.close()
write into a file
idFile.write('my text into my file')
open a file
idFile = open('myfile.ext', 'w')
Functions
TKinter basics
print type(number)
print number
Usefulfunctions
range
range(1,10,2)
range(5,10)
range(5)
user input
answer = raw_input("what's your name ?")
Exceptions
except ValueError:
try: ... break
Control Strutures
While () ...
while (condition2):
If () elif() else...
if (condition1):elif(condition2):else:
for ()...
dummyList = ['cat', 'dog', 'horse']for animal in dummyList: print len(animal)
for x in range(a,b):
Function Definition
Returned variables usethe keyword returnat the end of the function def.
All variables within thefunction core are local.
def functionName(param1="value1", param2="value2", ...):
Allows default values definitions.
def functionName(param1, param2, ...):
def functionName():