130 likes | 139 Views
Python Quick review. CS360 Dick Steflik. Starting with Hello World.
E N D
Python Quick review CS360 Dick Steflik
Starting with Hello World • start the python shell type: print "Hello World!" orput into file hello.py with text editor restart the python shell and type: import hello orat OS command prompt type python hello.py
Variables, Expressions and Statements • type(<expression>) returns the type of the expression print <expr>, ..., <expr> print several expressions <variable> = <expression> assign value to variable operators: + - * / normal arithmetic operators 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>) convert to intstr(<nonstring expression>) convert to string import math make math functions availmath.sin, math.cos, math.sqrt examples of math functions help('math') 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 ones[index] return character at index (0 based) len(s) length of string s[-1], s[-2], s[-3] last character, 2nd to last, 3rd to last s[i:j] slice of string: i through (j - 1) s[:j] slice from beginning through (j - 1) s[i:] slice from i to end of string <string> + <string> concatenate two strings help('str') show string functions (q to quit help) s.lower() returns lowercase version of s str.isalpha is alphabetic character (boolean function) foreach loop over string: for <variable> in <string>: <statements>
Lists • [<expr>, ..., <expr>] a list same use of [], len lists are very similar to stringsrange(n) list of integers 0 through (n - 1)range(i, j) list of integers i through (j - 1) [] empty list <expression> in <list> test for list membership (boolean) <list> + <list> append two lists together to form new list list1 = list(list2) make a copy of list2, store in list1 <var> = list(<string>) convert string to list <list var>.append(<expr>) append value to end of list <list var>.remove(<expr>) remove given value from list del lst[i] remove value at index iforeach loop over list: for <variable> in <list>: <statements>
Files • <var> = open(<name>, 'r') open a file for input <var> = open(<name>) same as above, ‘r’ is default <file variable>.readLine() return next line of file as a string <file variable>.readLines() return all lines of file as list of strings <var> = open(<name>, ‘w') open a file for output <file variable>.write(<string>) write <string> to the file <file variable>.close() close the file
Statements • assignment a,b,c = 1,2,3function calls log.write(“some text\n”)print print(“some fun\n”)if/elseif/else conditional executionfor/else fixed iterationwhile/else indeterminate iterationpass empty statement (no-op)break, continue force exit a looptry/except/finally catching exceptionsraise trigger an exceptionimport,from make members of a module accessibledef,return,yield defining functions
Statements (cont.) • class Defining objectsglobal namespacesdel deleting referencesexec running code strings, like interpreting a string containing python codeassert debugging your codewith/as context managers