reference: Python intro section 3.1.3
[]
to set up a listrange()
makes a range but you can turn it into a list with list()
x[0]
) and a one-element list (slicing, x[0:1]
)x = [1,2,3]
y = x
y[2] = 17
print(x)
## [1, 2, 17]
Check it out at Python Tutor
x+y
vs. foo(x,y)
vs. x.foo(y)
x = [1,2,3]
y = [4,5]
x.append(y)
print(x)
## [1, 2, 3, [4, 5]]
x = [1,2,3] # reset x
y = [4,5]
x.extend(y)
print(x)
## [1, 2, 3, 4, 5]
Can use +
and +=
as shortcut for extending:
x = [1,2,3]
y = [4,5]
z = x+y
print(z)
## [1, 2, 3, 4, 5]
x.insert(position,value)
: inserts (or x=x[0:position]+[value]+x[position+1:len(x)]
)x.remove(value)
: removes first valuex.pop(position)
(or del x[position]
or x=x[0:position]+x[position+1:len(x)]
)x.reverse()
(or x[::-1]
)x.sort()
: what it saysx.count(value)
: number of occurrences of value
x.index(value)
: first occurrence of value
value in x
: does value
occur in x
? (or logical(x.count(value)==0)
)len(x)
: lengthNote: pythonicity vs. TMTOWTDI
if
statement (reference)if False:
print("no")
elif
) and else
clausesif (x<=0):
print("what??")
elif(x==1):
print("one")
elif(x==2):
print("two")
else:
print("many")
For example:
x = 17
while x>1:
x = x/2
Maybe we want to know how many steps that took:
x = 17
n = 0
while x>1:
x = x/2
n = n+1
Can you get the same answer using import math
and math.log(x,2)
(and maybe round()
or math.floor
)?
We can use logical operators to combine
x = 17
n = 0
while x>1 and n<3:
x = x/2
n = n+1
n = 0
while n<n_max:
# do stuff
n = n+1
Or we could use a for
loop:
for n in range(0,n_max):
# do stuff
does this repeat n_max
or n_max+1
times? (hint: try it out, and/or use list(range(...))
…)
more generally, we can use for
to iterate over any list.
for
loop examplesAnother example: a change-writing program.
Given an amount of money, return a list of length 5 that gives the (smallest) number of coins of each unit (toonies, loonies, quarters, dimes, and nickels) required to make up that amount.
total=5.73
toonies = 5.73 // 2 ## integer division
total = total - 2*toonies
total = 5.73
res = [] # empty list
denoms = list(2,1,0.25,0.1,0.05)
for d in denoms:
# do stuff
total
, use denoms
aboveBefore coding up a solution, first describe it at a high level and then refine it:
Now let’s look at the prime walk program again …
break
break
is a way to get out of a while
or for
loop early:
for i in range(0,10):
if i>5:
break
for
loopsWe can look at (e.g.) all the combinations of i
and j
via:
for i in range(0,3):
for j in range(0,3):
print([i,j])
We can store matrices as a list of lists: represents a 2 \(\times\) 3 matrix. We can loop over rows and columns to operate on every element, or combine the elements in some way:
## initialization
m = [[1,2,3], [2,7,9]]
nrows = len(m)
ncols = len(m[0])
total = 0
## loop
for i in range(nrows):
for j in range(ncols):
total += m[i][j]
print(total)
## 24
From Secret Weblog: all of the following are equivalent …
i = 0
while i < mylist_length:
do_something(mylist[i])
i += 1 ## or i=i+1
vs.
for i in range(mylist_length):
do_something(mylist[i])
(this form is useful if we need to combine two lists, or otherwise index element i
of several different things …)
vs.
for element in mylist:
do_something(element)