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 Quick review CS360 Dick Steflik Starting with Hello World • start the python shell type: print "Hello World!" or put into file hello.py with text editor restart the python shell and type: import hello or at OS command prompt type python hello.py Variables, Expressions and Statements • type(<expression>) print <expr>, ..., <expr> <variable> = <expression> +-*/ operators: ** % () > ** > * / % > + - returns the type of the expression print several expressions assign value to variable operators: normal arithmetic operators ** is exponentiation, % is mod precedence (otherwise mostly left-to-right) <var> = raw_input(<prompt>) read a text line of input <var> = input(<prompt>) read a numerical line of input # text comment Functions • int(<float or string>) str(<nonstring expression>) import math math.sin, math.cos, math.sqrt help('math') convert to int convert to string make math functions avail examples of math functions list of all math functions (q to quit help) defining functions: def <name>(<param>, ..., <param>): <statements> return statement (immediately exits function, returning a value): return <expression> Conditionals • type boolean (logical tests): either True or False x != y # x is not equal to y x>y # x is greater than y x<y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to y x and y # x and y are both true x or y # x or y (or both) are true not x # x is not true if/else statements • if <test>: <statements> if <test>: <statements> else: <statements> if <test1>: <statements> elif <test2>: <statements> else: <statements> Strings • strings are immutable; to change one construct a new one s[index] return character at index (0 based) len(s) length of string s[-1], s[-2], s[-3] s[i:j] s[:j] s[i:] <string> + <string> help('str') s.lower() str.isalpha last character, 2nd to last, 3rd to last slice of string: i through (j - 1) slice from beginning through (j - 1) slice from i to end of string concatenate two strings show string functions (q to quit help) returns lowercase version of s is alphabetic character (boolean function) foreach loop over string: for <variable> in <string>: <statements> Lists • [<expr>, ..., <expr>] a list same use of [], len range(n) range(i, j) [] <expression> in <list> <list> + <list> list1 = list(list2) <var> = list(<string>) <list var>.append(<expr>) <list var>.remove(<expr>) del lst[i] foreach loop over list: for <variable> in <list>: <statements> lists are very similar to strings list of integers 0 through (n - 1) list of integers i through (j - 1) empty list test for list membership (boolean) append two lists together to form new list make a copy of list2, store in list1 convert string to list append value to end of list remove given value from list remove value at index i Files • <var> = open(<name>, 'r') <var> = open(<name>) <file variable>.readLine() <var> = open(<name>, ‘w') <file variable>.write(<string>) open a file for input same as above, ‘r’ is default return next line of file as a string return all lines of file as list of strings open a file for output write <string> to the file <file variable>.close() close the file <file variable>.readLines() Statements • assignment function calls print if/elseif/else for/else while/else pass break, continue try/except/finally raise import,from def,return,yield a,b,c = 1,2,3 log.write(“some text\n”) print(“some fun\n”) conditional execution fixed iteration indeterminate iteration empty statement (no-op) force exit a loop catching exceptions trigger an exception make members of a module accessible defining functions Statements (cont.) • class global del exec assert with/as Defining objects namespaces deleting references running code strings, like interpreting a string containing python code debugging your code context managers