x = [[0, 0, 0], [0, 0, 0]]
x[1] = x[0] # put a line here
x[0][1] = 42
print(x[1][1])
42
def squareList(l: list[float]) -> None:
# wrong: for val in l
for i in range(len(l)):
l[i] = l[i]**2
x = [2.0, 4, 6, 0, 1]
squareList(x)
print(x)
[4.0, 16, 36, 0, 1]
def removeFactorsOfTwo(l: list[int]) -> None:
for idx in range(len(l)):
val = l[idx]
while val%2 == 0 and val > 0:
val = val // 2
l[idx] = val
x = [2, 4, 6, 0, 1]
print(removeFactorsOfTwo(x))
print(x)
<cell>9: error: "removeFactorsOfTwo" does not return a value (it only ever returns None) [func-returns-value]
None [1, 1, 3, 0, 1]
x = [2, 4, 6, 0, 1]
y = x
z = [2, 4, 6, 0, 1]
print(x == y)
print(x == z)
print(x is y)
print(x is z)
y[1] = 8
print(x == z)
True True True False False
import math
def greatest(lst: list[float]) -> float:
assert len(lst) > 0, "No greatest in an empty list"
r = lst[0]
for val in lst:
if val > r:
r = val
return r
print(greatest([math.sqrt(2), math.pi, math.e]))
print(greatest([-math.sqrt(2), -math.pi, -math.e]))
3.141592653589793 -1.4142135623730951
e = [2, 6, 8]
x = e
print(e)
e.insert(1, 4)
print(e)
e.insert(0, 0)
print(e)
print(x)
e.append(10)
print(e)
print(e.pop())
print(e)
print(e.pop(1))
print(e)
[2, 6, 8] [2, 4, 6, 8] [0, 2, 4, 6, 8] [0, 2, 4, 6, 8] [0, 2, 4, 6, 8, 10] 10 [0, 2, 4, 6, 8] 2 [0, 4, 6, 8]
def divisors(x: int, y: int) -> list[int]:
r = []
i = 1
while i <= x and i <= y:
if x%i == 0 and y%i == 0:
r.append(i)
i = i + 1
return r
print(divisors(44100, 48000))
[1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300]
x = [2, 4, 6, 0, 1]
y = x[:]
y[0] = 4
print(x)
[2, 4, 6, 0, 1]
x = [2, 4, 6, 0, 1]
y = x[1:3]
print(y)
[4, 6]
x = [2, 4, 6, 0, 1]
midpoint = len(x) // 2
left = x[:midpoint]
right = x[midpoint:]
print(left)
print(right)
left[0] = 0
print(x)
[2, 4] [6, 0, 1] [2, 4, 6, 0, 1]
x = [2, 4, 6, 0, 1]
print(x[::2])
print(x[::-1])
[2, 6, 1] [1, 0, 6, 4, 2]
x = [2, 4, 6, 0, 1]
y = x + [1, 2, 3]
print(y)
print("Hello, " + "world!")
[2, 4, 6, 0, 1, 1, 2, 3] Hello, world!
list(range(1, 10)) + list(range(0, 3))
print(str([1, 2, 3]))
[1, 2, 3]