Python Cheat Sheet.pdf - Python Cheat Sheet Dominic Thomas 2019 for Python 3 Reserved Words in Python 3 False async del from lambda return None await

Python Cheat Sheet.pdf - Python Cheat Sheet Dominic Thomas...

This preview shows page 1 - 2 out of 2 pages.

Python Cheat Sheet Dominic Thomas 2019 for Python 3 Reserved Words in Python 3: False None True and as assert async await break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield print() Function print(“Hello world!”) print(“one plus one equals”,2) print(“1 + 1 = “ + str(2)) Assignment Statement <variable> = <value or variable> x = 3 y = x z = input(“Type a value to put in z: “) for Loop (definite) for <variable> in <sequence>: <do something with variable!> x = [1,2,3,4,5] for number in x: print(number) while Loop (infinite) w/break while <condition>: (do something during condition!> while True: if <condition>: break Numeric Operators Operator Operation + addition - subtraction * multiplication / division ** exponent // floor division w/o remainder % modulus – returns remainder Built-in Functions (popular) Name Returns abs(n) absolute value of n range(n) ints list from 0 to n-1 range(m,n) ints list from m to n-1 range(m,n,p) ints list from m to n-1 by p type(x)* Python type of x eg. string, etc. int(x)* x’s value converted into integer str(x)* x’s value converted into string float(x)* x’s value converted into float long(x)* x’s value converted into long int round(x)* nearest whole number of x len(x) return length of x eval(x)* numeric value of x; x should be an expression list(x)* x’s value converted into list; x should be an iterable dict(x)* x’s keys and values converted into a dictionary * x can be an expression, the result will be used Module Import import <module_name> from <module_name> import <funciton1>,<function2>, … import <module_name> as <alias> from <module_name> import * Example: #prints random int between 0 & 5

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture