1 / 13

Python Quick review

Python Quick review. CS360 Dick Steflik. Starting with Hello World.

merler
Download Presentation

Python Quick review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Quick review CS360 Dick Steflik

  2. 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

  3. 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

  4. 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>

  5. 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

  6. if/else statements • if <test>: <statements> if <test>: <statements> else: <statements> if <test1>: <statements> elif <test2>: <statements> else: <statements>

  7. 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>

  8. 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>

  9. 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

  10. 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

  11. Statements (cont.) • class Defining objectsglobal namespacesdel deleting referencesexec running code strings, like interpreting a string containing python codeassert debugging your codewith/as context managers

More Related