T4: |a % b = a - b * (a // b) Kelly Du
| oat if: 1.if either operand oat
2. / -> oat
e exponentiationalsofloat
2430258
3. a ** b where b < 0
|can compare int with oat; int(3.9) == 3 302 300.0
|not a -> True if a False, False if a True
|other type of quotes can be freely used in str (anything can be used for
triple quotes) comrawson
|one line if not triple quotes diff types
“p” * 2 -> “pp”; “p”+”p” -> “pp”;
i “p” in “pp” -> True
m
|overloading (di things for operand): from least to greatest A-Z, a-z
T5: |assignment statement: name = expression variables
|if expresses alternation; if expression is true then run; elif: once a
clause’s exp. True, body(statements list) runs, no more further action
|while expresses iteration: Bool exp called condition
as m mpi
T6: |module = single Python le; package = many modules in folders import math -> math.pi
T7: |lists and str = sequences (ordered) of arbitrary values /characters from math importsorpi
* -> pi
|concatenated for str/list: [2.1, 3.2] + [7.8] -> [2.1, 3.2, 7.8]; repeated:
lrespectively
3 * [5] = [5, 5, 5] ||| “A”<“B”/“A”<“AA”/“AA”<“AB”/[1]<[2]/[1]<[1,0]/
[1,1,9]<[1,2,0] ||| len(‘’) == len([]) == 0
|slicing (numbers are indices): start( default = 0, included, doesnt
have to exist) :end(default to the end, excluded, doesnt have to
exist) :step(optional, default=1)
“programiz.pro”[-4:-2] -> .p / "abcdefghij"[2::3] -> “c ”
|list mutable bc value iself can be changed; str immutable
can assign new values to list: ls = [7, 9, 8] negation not
ls[1] = 11 >>>print(ls) -> [7, 11, 8]
| “8” + 2 INVALID/“8” + “2” VALID coming chin
T8:|truth table represent Bool operations; output in rightmost column
and input in other columns (2^n rows for n inputs)
|x >= 'A' and x <= 'Z' #verify if x is uppercase letter
x >= '0' and x <= '9' # ‘‘ digit is str
|avoid comparions to True ex: 5 > 0 == True -> False
i
|precedence: not, and, or
|implicit conversion in Bool context: str,tuple,list of length 0 (empty
container/sequence)/…00/0.00…/ None
are equal to False; rest is True
|explicit conversion: bool(“False”) -> True (Not empty str)
|short-circuit: evaluate right side of Bool exp. only if needed ifa 0 thenerror
ex: A and B, if A is False then dont evaluate B (exp. False no matter
what) A or B, if A is True, dont evaluate B (exp. True “)
T10: |Functions (or procedures) called by another program or
function. When a function completes, it returns a value to the
caller.parameters are names of values passed to the function when it
is called (arguments are the actual exp.) return expression statement
de nes value of function call; None returns when expression omitted
or no return. optional docstring describes function def ssbbs(a, b):
ssbs(11, 22) “aaa”
11 assigned to parameter a, 22”b -> values become local to function
T11: |assert followed by an expression
|if expression True, nothing happens; if exp False, exception is raised ba varcanbecalledbut
ex: assert remaining_fraction(10, 10) == 0.5
AssationError notmodified ifuseglobal
vannameforassignment newlocal
van
iscreated
T9: |augmented assignment: all binary arithmetic operations support operation= -> x = x operation y e.g. +=
|for loop: for name in exp., exp. can be any iterable type (list, str, range)
|name doesnt have to be previously assigned, name will be assigned to value of each element in exp.
|range() type: iterable, immutable, sequence of int; for x in range (5): -> 0,1,2,3,4
range(end): 0 to end-1; range(start, end): start to end - 1; range(start, end, step): skip by step e.g. for i in range(3, 13,
4) -> 3, 7, 11
can be indexed
I
Assertion
Error
T13: |collection type contains multiple values/sequence=ordered collection(ex:list,range,str);items numbered 0to N-1
|dictionary = mutable&iterable collection: map from set of keys to group of values, key-value pair = item
|can be written: dc={‘red’:3,’blue’:2} |access values: print(dc[‘red’]) -> 3
|dict: d1 = dict() same as d2 = {} |add item to dict w/ assignment: d1[‘bass’]=2 \ replace existing value: d1[‘bass’]+=1
|sequence=map from index i to value x[i] \dictionary generalizes this: map from arbitrary set(not ordered int) to values
|construct dict: 1.a=[[‘red’, 0], [‘blue’, 8]] >>> d=dict(a) 2.d=dict(red=0, blue=8) 3.keys=[‘red’, ‘blue’] f
d=dict.fromkeys(keys, 0) -> {'red': 0, 'blue': 0} |operators: 1.comparions (=, !=) item order dont matter 2.check key
present in dict: k in d 3.dict cant +,*,<=, <, >=, >, slices
hgg gyqy
T12: |method: function associated with type, name not global
|immutable values change only by assignment (with = ) e.g. y =
y.upper(); assignment works for all types of values
I |mutable can be changed by functions or methods (return
values might not be
expected)
I can also be modi ed
by assignment to index
C
read
y exp. e.g. x[7-2] += 3
removefirst
immutable types cant fog
lint yggfa.name www.t
versiontype
doesn't modifystr itself
only returns