x = [[0, 0, 0], [0, 0, 0]]
x[1] = x[0]
x[0][1] = 42
print(x[1][1])
42
x = [2, 4, 6, 0, 1]
z = x
y = x[:]
y[0] = 4
x[1] = 42
print("x:", x)
print("y:", y)
print("z:", z)
x: [2, 42, 6, 0, 1] y: [4, 4, 6, 0, 1] z: [2, 42, 6, 0, 1]
x = [2, 4, 6, 0, 1]
y = x[1:3]
print(list(range(1, 3)))
print(y)
a = "Hello, world!"
b = a[0:4] + a[6:]
print(b)
print(list(a))
[1, 2] [4, 6] Hell world! ['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!']
lst = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3]
midpoint = len(lst) // 2
#left = lst[0:midpoint]
#right = lst[midpoint:len(lst)]
left = lst[:midpoint]
right = lst[midpoint:]
print(left)
print(right)
[3, 1, 4, 1, 5, 9, 2, 6, 5] [3, 5, 8, 9, 7, 9, 3, 2, 3]
lst = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3]
print(lst[::2])
print(lst[1::2])
print(lst[::-1])
print(list(range(0, len(lst), 2)))
[3, 4, 5, 2, 5, 5, 9, 9, 2] [1, 1, 9, 6, 3, 8, 7, 3, 3] [3, 2, 3, 9, 7, 9, 8, 5, 3, 5, 6, 2, 9, 5, 1, 4, 1, 3] [0, 2, 4, 6, 8, 10, 12, 14, 16]
lst = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2]
print(lst[len(lst)-1])
print(lst[-1])
print(lst[-10::2])
#print(lst[1024])
2 2 [6, 3, 8, 7, 3]
x = [3, 1, 4]
y = [1, 5, 9]
z = x + y
print(z)
z[0] = 42
print(x)
a = "Hello"
b = ", world!"
c = a + b
print(c)
[3, 1, 4, 1, 5, 9] [3, 1, 4] Hello, world!
def reverseInterleave(deck: list) -> list:
"""
Return a reverse-interleaved copy of deck.
"""
return deck[::2] + deck[1::2]
print(reverseInterleave([1, 2, 3, 4, 5, 6]))
[1, 3, 5, 2, 4, 6]
print(int(42.0))
print(int("42"))
print(list("Hello!"))
#print(list(42))
42 42 ['H', 'e', 'l', 'l', 'o', '!']
board = [[" ", "x", " "],
[" ", "x", "o"],
[" ", " ", " "]]
print(board)
board[2][1] = "o"
print(board)
for row in board:
print(row)
[[' ', 'x', ' '], [' ', 'x', 'o'], [' ', ' ', ' ']] [[' ', 'x', ' '], [' ', 'x', 'o'], [' ', 'o', ' ']] [' ', 'x', ' '] [' ', 'x', 'o'] [' ', 'o', ' ']
x = (2, 4, 6, 0, 1)
print(x)
(2, 4, 6, 0, 1)
x: tuple[int, str] = (24601, "Jean Valjean")
print(x[0])
y = list(x)
print(y)
<cell>4: error: Argument 1 to "list" has incompatible type "tuple[int, str]"; expected "Iterable[int]" [arg-type]
24601 [24601, 'Jean Valjean']
def longests(strs: list[str]) -> tuple[list[str], int]:
"""
Returns the longest strings in strs as a list, paired with their length, as an int.
"""
r = [strs[0]]
for s in strs[1:]:
if len(s) > len(r[0]):
r = [s]
elif len(s) == len(r[0]):
r.append(s)
return (r, len(r[0]))
res = longests(["a", "blue", "duck"])
print(res)
(strs, length) = longests(["a", "blue", "duck"])
print(strs)
print(length)
(['blue', 'duck'], 4) ['blue', 'duck'] 4
import random
def monteCarloPi(rounds: int) -> float:
"""
Return an approximation of pi, using the Monte Carlo technique with rounds throws.
"""
xs = []
ys = []
for _ in range(rounds):
xs.append(random.random())
ys.append(random.random())
# How many fall in the quarter circle?
inside = 0
for idx in range(rounds):
x = xs[idx]
y = ys[idx]
if x*x + y*y <= 1:
inside = inside + 1
return 4 * inside / rounds
print(monteCarloPi(1000000))
3.142296