Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ΤΥΠΟΛΟΓΙΟ PYTHON Compact summary of python This is not intended to be the complete language. name your file execute your file on MS Windows only when using Tk, wx, Qt xxx.py python xxx.py pythonw xxx.py execute your file and save output in a file python xxx.py > xxx.out # rest of line is a comment, # sign makes the rest of a line a comment """ """ starts a long comment, can be many lines # ends the long comment Indentation must remain the same on a sequence of statements. (statements starting next indentation, end with a colon : ) Indent after each compound statement such as if, while, for, def ... print "hello from python" # quotes make strings, writes to display print 'hello from python' # apostrophes make strings, no character type print "hello", print "from python" # because of previous comma, writes same one line # see files hello.py and hello.out print x # prints the value of x, any type print "x=", print x # prints one line, e.g. x= 7.25e-27 Strings may use either " or ' (same on both ends) Open a file for reading or writing, f is any variable name f=open("myfile.dat", "r") f=open("myfile.dat", "w") f.close() a=f.read() a=readline() string a f.seek(5) # # # # # open for reading open for writing close the file read entire file into string a read one line, including \ n, into # seek five # see files testio.py and testio.out Reserved words: and elif if as assert else except import in break class exec finally is lambda not [1] continue def del for from global or pass print raise return try while with yield Operators: y=7 z=5.3 x=y+z x=-y x+=1 # the usual assignment statements with normal precedence # in place of x++; the list of operators is: + - * ** / // % arithmetic << >> & | ^ ~ shift logical < > <= >= != and or comparison Delimiters include: ( ) [ ] { } @ grouping , : . ` ; separating = += -= *= /= //= %= operate store &= |= ^= >>= <<= **= operate store Special characters: ' " # \ Not used: $ ? Numbers: 1 123456789012345678 1.3 7.5e-200 0xFF 5.9+7.2j Boolean: Tuples: True False constants x=2, 3, 5, 7, 11 y=(2, 3, 5, 7, 11) Lists: x=[2, 3, 5, 7, 11] An array of 10 elements, initialized to zeros: z=[0 for i in range(10)] z[0]=z[9] # lowest and highest subscripts A 2D array of 3 by 4 elements, initialized to zeros: a=[[0 for j in range(4)] for i in range(3)] a[0][0]=a[2][3] # lowest and highest subscripts a[i][j] import math # see files testmath.py and testmath.out a=math.sin(b) print "b=%f, sin(b)=%f" % (b, a) # formatted output %d, %10.5f, %g ... import decimal # big decimal a=decimal.Decimal("39.25e-200") import random a=random.randint(1,10) import NumPy import Numarray import Numeric # latest numerics package # older numerics package Multiarray ? # very old numerics package [2] Three equivalent ways to call QString: from PyQt4.QtCore import * # all directly visible x=QString() import PyQt4.QtCore as P # any short name x=P.QString() import PyQt4 x=PyQt4.QtCore.QString() # fully named You can have your own program in many files. e.g. file spiral_trough.py has a function spiral def spiral(x,y) in test_spiral.py from spiral_trough import spiral z=spiral(x1,y1) if a<b: statements elif c>d: statements elif e==f: statements else: statements # control statements zero or null is True # anything else is False # <= for less than or equal # >= for greater than or equal # != for not equal # unindent to terminate for i in range(n): # i=0, 1, 2, ... , n-1 for i in range(3,6): # i=3, 4, 5 not 6 for i in range(5,0,-1): # i=5, 4, 3, 2, 1 not 0 while boolean_expression: # || is "or" && is "and" True, False "break" exits loop or "if" statement. "continue" goes to next iteration in a loop. Function definition and call: def aFunction(x, y, z): # inputs of any type # do something, compute a and b return a,b # somewhere later m, n = aFunction(p, q, r) # none, one, or multiple value return one or several functions defined in a file # funct_to_import.py has two functions defined, funct and rabc def funct(n): return n [3] a,b,c=0,0,0 def rabc(): a=1 b="rabc string" c=[3, 4, 5, 6] return a,b,c In another file, call funct and rabc # test_import_funct.py import funct_to_import and run funct and rabc import funct_to_import print funct_to_import.funct(3) print x,y,z print print print print funct_to_import.rabc() = funct_to_import.rabc() x y "z[1]=", z[1] [4]