Python QRG
Python QRG
Python QRG
Assignment statement:
Uses simply the = sign. Expression on right side determines type of variable (this is called implicit declaration). Examples: a=4 i = i + 1
Variable Types
TYPE INTEGER FLOAT STRING BOOLEAN/LOGICAL CONTAINS Whole numbers All real numbers Text Switch/condition EXAMPLES 24 -2 12.5 0.5 .5 1. 1.0 1e6 Hello world True False OPERATIONS EXAMPLES 7/3 25%4 2+3 2-3 6*4 2**3 7./3. 25.%4. 2.+3. 2.-3. 6.*4. 2.**3. Hell + o world chr(84) + chr(85) + chr(68) ord(A) a>b a<=b a==b a<>b a!=b (a==b) or (a==c) swfound and not swcorrect
Conversion of types
int(x)
converts a float to an integer (truncated), so int(3.7) is 3, int(-2.5) gives -2 float(i) to convert integer to a float variable type str(a) or repr(a) converts a number to a string, normal or resp. maximum precision eval(txt) to get the value of the content of the string (can contain variable names)
Naming of variables
Variable names can contain numbers, characters and underscore character ( _ ), Names cannot start with a number. Though not required, by convention i, j, k, l, m, n are often used of integers as well as names starting with these characters. (e.g. ncount meaning number of counts)
Page 1
U SING
MATH FUNCTIONS
You can use math functions from the supplied math library in two ways:
M ET HO D 1:
import math a = 50.*math.pi/180. x = math.sin(a) (only once at the beginning of your program file)
M ET HO D 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)
Page 2
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
Page 3
range function: range(end) range(start,end) range(start,end,step) default start is zero and end is not included! Examples:
WHILE-statement Loop while a condition is True, note again use of semicolon and indenting:
D EFINING
def faculteit(n): fac = 1 for i in range(1,n+1): fac = fac*i return fac a = input(Give number for factorial:) print Factorial ,a, = ,faculteit(a)
D EFINING
In file mytools.py: def faculteit(n): fac = 1 for i in range(1,n+1): fac = fac*i return fac
Page 4
P YTHON
AND
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 supplied next 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 pulldown 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 example http://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 pygame module.
Page 5
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!
Page 6