7/28/2019 Python QRG
1/6
7/28/2019 Python QRG
2/6
PYTHON:QUICK REFERENCE GUIDE
Page 2
Lists (and Tuples)
a = [5, 15, 24, -3, 7, 18, 24 , 0, 0 ,1]
After this assignment then:
len(a) is 10value of a[0] is 5
value of a[9] is 1 so indices range from 0 till 9!
value of a[1:4] is [15,24,-3] this is called slicing, works also on strings
Adding an element can be done in two ways: with append function or with + operator. Safest
way is append, especially with adding content of variables
a.append(3)
a.append(i)
a = a+[3]
b = [[3,4,5].[4,5,6]] #list of two lists
c = a[3] + b[1][0]
A tuple is a special list, immutable (=cannot be changed) and assignment is done with normal
brackets:
black = (0,0,0)
green = (0,255,0)
then
green[1] is 255 (now with square brackets because it is an index! )
>>>>>By default: use lists!
USING MATH FUNCTIONSYou can use math functions from the supplied math library in two ways:
METHOD 1:
import math (only once at the beginning of your program file)
a = 50.*math.pi/180.
x = math.sin(a)
METHOD 2:from math import pi,sin
a = 50.*pi/180
x = sin(a)
Not recommended (why not?) but also possible:
from math import *
a = 50.*pi/180
x = sin(a)
7/28/2019 Python QRG
3/6
PYTHON:QUICK REFERENCE GUIDE
Page 3
PROGRAM FILE LAY-OUT A lot of time of programming is spent debugging, so make your files readable for yourself and for
others. Use a lot of comment lines, they are marked with a hash character ( # ). Use the hash sign
at the beginning of a comment line or at the end of a program line.
You can continue on the next line with your statement by ending the line with a backslash ( \ )
as a continuation mark. Sometimes it is automatically done (in lists for example). In print
statements just add a comma at the end of a line to continue on the same line with the next
print statement)
Use also plenty of empty lines and spaces to enhance readability as well as clear but not too long
variable names.
Basic statements
print Hello world
print The answer is ,a
print Two solutions are,x1,and,x2
INPUT
Input function recognizes type of variable based on input, use raw_input() for string.
a = input(Give value of a:)
name = raw_input(What is your name?)
IF-statement
Use of ELSE or ELIF (else-if) is optional, always end with : to indicate start of block or statement
Indenting margin determines begin and end of block!
if a < b:
print "a smaller than b"
c=b
elif a == b:print "a and b are equal"
c=a
else:
print "a is larger than b"
c=a
7/28/2019 Python QRG
4/6
PYTHON:QUICK REFERENCE GUIDE
Page 4
FOR-statement
For example to loop 10 times:
for i in range(10):
x = x + i*4
print i = ,i
range function: range(end) range(start,end) range(start,end,step)
default start is zero and end is not included! Examples:
range(5) equals [0,1,2,3,4]
range(3,10) equals [3,4,5,6,7,8,9]
range(10,0,-1) equals [10,9,8,7,6,5,4,3,2,1]
WHILE-statement
Loop while a condition is True, note again use of semicolon and indenting:
WHILE a[i]!=b and i
7/28/2019 Python QRG
5/6
PYTHON:QUICK REFERENCE GUIDE
Page 5
In file myprog.py we put the import statement in the header, the function can then be used
throughout file:
import mytools
f = mytools.faculteit(a)
or:
from mytools import faculteit
f = faculteit(a)
or:
from mytools import faculteit as factorial
f = factorial(a)
or:
import mytools.faculteit as factorial
or:
from mytools import *
CHECK OUT THE MANY MODULES SUPPLIED WITH PYTHON AND
HELP FUNCTIONS There is a saying Python is free as in free beer and in free speech and it comes with batteries
included. This means among other things that there is a whole list of standard modules suppliednext to the basic math module.
To see which ones you have, type the following in the shell:
>>>help()
[welcome text interactive help]
help>modules
and you will see a list of modules. You can also type help(modules) or help(print)
To get more information you can also access a HTML help file via the help function from the pull-
down menu. In this menu you can also add links to your own help files such as to the CHM files
(pronounced chum-files) for Numpy and Scipy and/or a link to the online documentation for
pygame. You can also a link to for examplehttp://www.tutorialspoint.com/python/
For scientific computing we will use the numpy, scipy and matplotlib modules. For
animation and 2D visualization, as well as game programming, we will use the user-friendly
pygamemodule.
http://www.tutorialspoint.com/python/http://www.tutorialspoint.com/python/http://www.tutorialspoint.com/python/http://www.tutorialspoint.com/python/7/28/2019 Python QRG
6/6
PYTHON:QUICK REFERENCE GUIDE
Page 6
See also the file on the blackboard: tutorial.pdf which contains an extensive Python tutorial. (It is
also included in the Help function.) Study these sections for block 1 of the Python course:
3.1.1 Numbers
3.1.2 Strings
3.1.4 Lists
3.2 First steps prgrmng: while
4.1 if-statement
4.2 for-statement
4.3 range() function
(4.4 break, continue,else in loops)
4.6 defining functions
Not 4.7!
5.2 del statement
5.3 Tuples
5.6 More on conditions
6 Modules but not 6.1.1 en 6.1.2
6.2 Standard Modules
7.2.1 Method of File Objects
10.6 math module
App B Floating Point Arithmetic: Issues and Limitations