name
expression evaluates to val
-
call expression-calls func
performs
~
def
Keyword parameter
function-mini program that def celsius (tmp) :
specific task
return 9/s * temp + 32
statement-line of code -
return value
operators
-
X 3 *
Y Data
Types
=
+,
-
/, *
-
to val
mutable :
, expression-eval Immutable :
lists
11 float div-cuts off decimal
-
-
int() intw/float - float
· modulus-remainder float() any division-float-dictionanes
-
arrays
-
-
Str() A repeated x times
assignment = +=,
= X =
interval)
:
-
S
-
bool() 2 range (start end ,
> points right var to
-
,
mem -
tuples
~ -
left Str split() optional
address of
.
splits into 1st,
-
func
elem separated -
class instances
conditionals stopsato by space
can live inside immutable obj
caka objects)
mutable data
(expression) :
if
(1 , [2 , 3])
ex + =
~ unlimited/
3
.
return
elif (expression) : none elif is instance (array [i] type ,
return
else : ↳ optional Boolean
return = all other values
Truth
" "
False = O , None ,
loops operators
loops/enters if
=,! =, 3 4 =,
While y
=
3 =
expression True , ,
not-inverts , return T/F
(expression] :
while
and-first F, last T
< body> S or -
first T last F
Caka iteration)
for
for x in 1st : iterate over elem
range (len(ist))
for i in
: iterate over index
ex
. whether largest digit
of cant ben in n only appears once (T/E)
ex .
digite a followed by
of ex .
largest factor n.
digits of n in reverse order
def factor (n) :
def unique-large (n) :
top = 0
def pal(n) factor = n -
1
while n:
m= n while factor > 0 : d = n % 10
while m: if n % factor == 0 : n =
n//10
n = nx10 + m % /0 return factor if d > top :
top d
else =
m//10
:
m =
=
unique True
return n factor -= 1
elif d = =
top :
unique = False
return unique
, LISTS
ordered
-
collection of any data type
make list
1st comprehension optional
1st [X , y , , ] -
(filler exp)]
= z
[map expression) for 1st if
...
x in
index list 1st elem last elem Ist transform and/or filter 1st
returns new
Ist[i] >
- [0] >
-
[len(1st) 1) -
or 1st -
17
[map exp) of <filler exp) else <alternate map
exp> for Jelem) in < 1st]]
Ist methods non inclusive index
create newlst w/oldist
Ist [start
: end : Step] 1st [start ] 1st [end]
:
, ,
newlst = oldist +
[elem ... ,
if > I elem nested 1st
Ist . Append (item) ,
- item Ist ok
in 1st
Ist extend (1st) = append eachelem ex .
append extend
vs
doesn't replace, all other
.
Ist Insert (index , item)
.
- elem move
up one 1st = [5, 10, 15]
1st pop (index) .
- removes +return item if no
index , last item
.
1st .
append ([1 , 5 , 6])
23
1st remove (item)
0
I remove first occurence
2/1s6
.
of item 1st -51015
X in s T if ihm x in S
extendsa
X not in s F If item x ins
S
min(s) ,
max
(s)/ equality 1st
1st 2 bool ,True if items order same
+
1st] ==
Ist] is 1st2 bool , True if pt to same obj in
e
identity memory
Dichonaries - any data type
data to value
ordered type that maps key
- must be unique , immutable
make dict create populate dict pop [key]
3 or d = 53 and d() = a
.
Ekey val
find max/min of all val
...
dict= :
, ...
if included
,
access/update val at
key returnsdefaultfadict max (dict , get)
key dict
get(key default)
= .
dict
(key] = val or dict .
, min (dict key ,
= dict .
get)
append ral to key
append (val)
ADT page
- constructor
on
dict[rey] eX . ADT and dict
keys)
dist iterations dict
returns iterable over keys def split-the bill Corders) :
for dist
key
:
in for order in orders :
witerable of values
for value in dict values: .
if customer /order) not in d .
Keys (1 :
witerates over
dict items : key:val
& [customer Cordel] price (order)
for key , value
=
in .
else :
Skey : val for elem in
1st3 a prehension & [customer Corder)] += price Corder)
return d