Getting started.

>>> 1 + 2 3 >>> 3.4 * 5.6 - 7.8 11.239999999999998 >>> 2 ** 10 # 2^10 1024 >>> a = 12.3 - 45.6j # complex number 12.3-45.6i (i=sqrt(-1)) >>> a (12.300000000000001-45.600000000000001j) >>> a.real # the real part of a 12.300000000000001 >>> a.imag # the imaginary part of a -45.600000000000001 >>> from cmath import * # we will use 'cmath module' >>> sqrt(a) # the function 'sqrt' is defined in cmath module (5.4557198227511341-4.179100236218277j) >>> abs(a) # the function 'abs' is defined in cmath module 47.229757568719329 >>> c = 'a' # the character 'a' >>> c 'a' >>> s = "abc" # string "abc" >>> s 'abc' >>> s = 'abc' >>> s 'abc' |

A tuple consists of a number of values separated by commas.

>>> point = (300, 400) # a tuple >>> point[0] 300 >>> point[1] 400 >>> point[0] = 500 Traceback (most recent call last): File "&lang pyshell#111&rang", line 1, in &lang module&rang point[0] = 500 TypeError: 'tuple' object does not support item assignment >>> point = (100, 200) |

A list consists of a number of values.

>>> a = [2, 3, 5, 7, 11, 13] # a list >>> a[0] 2 >>> a[1] = 4 >>> a [2, 4, 5, 7, 11, 13] |

To define a function, write "def function_name (parameters):". The body of the function must be indented.

>>> def T(z): ... w = (2*z+3)/(4*z+5) ... return w ... >>> T(7.0) 0.51515151515151514 >>> >>> def norm(z): ... return z.real*z.real + z.imag*z.imag ... >>> def nextstx(st, x, ac = 1): # default value for 'ac' is 1 ... st = st + x[0]/(x[1]*x[2]) ... x = (x[1]*x[2] - x[0], x[2], x[1]) ... return st, x # returns a tuple ... >>> a,b = nextstx(2, (3,4,5), 6) >>> a 2 >>> b (17, 5, 4) |

To open a window and draw lines and circle on it, we use Canvas in Tkinter module. See http://effbot.org/tkinterbook/. for more information.

>>> from Tkinter import * # we will use 'Tkinter module' >>> c = Canvas(width=800, height=600) # create a canvas >>> c.create_line(100, 200, 300, 400) # draw a line segment from (100, 200) to (300, 400) >>> c.create_oval(100, 100, 300, 300) # draw a oval upper left=(100,100), lower right = (300, 300) >>> c.focus_set() >>> c.pack() >>> c.mainloop() |

Capturing mouse move in a canvas:

>>> def handler_b1(event): ... print "mouse is at (", event.x, ",", event.y, ")" ... return ... >>> c = Canvas(width=800, height=600) >>> c.focus_set() >>> c.pack() >>> c.mainloop() >>> c = Canvas(width=800, height=600) >>> c.bind("&lang Button-1&rang", handler_b1) # See (*) '44804936handler_b1' >>> c.focus_set() >>> c.pack() >>> c.mainloop() mouse is at ( 166 , 127 ) mouse is at ( 109 , 138 ) |

Global variable and local variable. A variable which is defined outside the functions is a "global vriable". A variable which is defined inside a function is a "local vriable". To put some data into a global variable "x" in a function, you have to write "global x". Example 1:

>>> x = 10 # this "x" is global >>> def f(): ... print x # get the value of the global variable "x" ... >>> f() 10 >>> def f(): ... x = 20 # this "x" is local and different from the above "x" ... print x ... >>> f() 20 >>> x # this "x" is global 10 |

>>> x = 10 # this "x" is global >>> def f(): ... global x ... x = 20 # this "x" is global ... print x ... >>> x 10 >>> f() 20 >>> x # the value of the global variable "x" is changed. 20 |

"if" statements. The body of the function must be indented. "elif" and "else" part is optional.

>>> x = 3 >>> if x > 0: ... print "x is positive" ... print x ... elif x == 0: ... print "x is zero" ... else: ... print "x is negative" ... print x ... x is positive 3 >>> tr = 2+3j >>> if tr == 2 or tr == -2: ... print "parabolic" ... elif tr.imag == 0 and (-2 < tr.real and tr.real < 2): ... print "elliptic" ... else: ... print "loxodromic" ... loxodromic |

>>> 2 > 1 True >>> 1 > 2 False >>> 1 == 1 True >>> 1 != 1 False >>> 2 >= 2 True >>> 3 <= 2 False >>> [1 > 0, 0 > 0, -1 > 0] [True, False, False] |

"for" statements

>>> for x in [0, 1, 2, 3]: ... print x ... 0 1 2 3 >>> for i, j, k in [(0,1,2), (1,2,0), (2,0,1)]: ... print "i=", i ... print "j=", j ... print "k=", k ... i= 0 j= 1 k= 2 i= 1 j= 2 k= 0 i= 2 j= 0 k= 1 >>> range(-3, 3) [-3, -2, -1, 0, 1, 2] >>> for i in range(-3, 3): ... print i ... -3 -2 -1 0 1 2 |

"while" statements

>>> x = -3 >>> while x < 3: ... print x ... x = x+1 ... -3 -2 -1 0 1 2 >>> x = -3 >>> while x < 3: ... print x ... if x == 0: ... break ... x = x+1 ... -3 -2 -1 0 >>> def isprime(n): ... i = 2 ... while i*i <= n: ... if n % i == 0: ... return False ... i = i+1 ... return True ... >>> for a in range(2, 1000): ... if isprime(a): ... print a ... 2 3 5 7 (omitted) |

For scipy, see http://docs.scipy.org/doc/scipy/reference/

>>> from scipy import * >>> a = matrix( [[1,2],[3,4]] ) >>> a matrix([[1, 2], [3, 4]]) >>> b = matrix( [[5,6],[7,8]] ) >>> b matrix([[5, 6], [7, 8]]) >>> a*b matrix([[19, 22], [43, 50]]) |

Ternary operator --- an operator that takes three arguments.

(value_when_true) if (condition) else (value_when_false)

>>> discrete = True >>> "discrete" if discrete else "indiscrete" 'discrete' |