In [23]:
import math

x = [[0, 0, 0], [0, 0, 0]]
x[1] = x[0] # changed line
x[0][1] = 42
print(x[1][1])
42
In [8]:
x = [2, 4, 6, 0, 1]
y = x[:]
y[0] = 4
print("x is", x)
print("y is", y)

y = x[1:3]
print("y is now", y)
x is [2, 4, 6, 0, 1]
y is [4, 4, 6, 0, 1]
y is now [4, 6]
In [9]:
lst = [8, 6, 7, 5, 3, 0, 9]
midpoint = len(lst) // 2
left = lst[0:midpoint]
right = lst[midpoint:len(lst)]
print(lst, "split in half is", left, right)
[8, 6, 7, 5, 3, 0, 9] split in half is [8, 6, 7] [5, 3, 0, 9]
In [10]:
lst = [8, 6, 7, 5, 3, 0, 9]
midpoint = len(lst) // 2
left = lst[:midpoint]
right = lst[midpoint:]
print(lst, "split in half is", left, right)
[8, 6, 7, 5, 3, 0, 9] split in half is [8, 6, 7] [5, 3, 0, 9]
In [12]:
lst2s = lst[::2]
print("lst2s is", lst2s)
lstrev = lst[::-1]
print("lstrev is", lstrev)
lst2s is [8, 7, 3, 9]
lstrev is [9, 0, 3, 5, 7, 6, 8]
In [16]:
print(lst)
print(lst[-1])
print(lst[-2])
print(lst[1:-1])
[8, 6, 7, 5, 3, 0, 9]
9
0
[6, 7, 5, 3, 0]
In [20]:
s1 = "Hello, "
s2 = "world!"
print(s1 + s2)
s = s1 + s2
print(s[1], s[-1])
print([3, 1, 4] + [1, 5, 9])

lst1 = [3, 1, 4]
lst2 = [1, 5, 9]
lst = lst1 + lst2
lst[0] = 0
print(lst1)
Hello, world!
e !
[3, 1, 4, 1, 5, 9]
[3, 1, 4]
In [21]:
def reverseInterleave(deck: list) -> list:
    """
    Returns the reverse interleave of the list deck.
    """
    ret = []
    for idx in range(0, len(deck), 2):
        ret.append(deck[idx])
    for idx in range(1, len(deck), 2):
        ret.append(deck[idx])
    return ret

print(reverseInterleave([1, 2, 3, 4, 5, 6]))
[1, 3, 5, 2, 4, 6]
In [24]:
def reverseInterleave(deck: list) -> list:
    """
    Returns the reverse interleave of the list deck.
    """
    return deck[::2] + deck[1::2]

print(reverseInterleave([1, 2, 3, 4, 5, 6]))
[1, 3, 5, 2, 4, 6]
In [26]:
print(float(5))
print(int(math.pi))
5.0
3
In [30]:
print("Hello!")
print(list("Hello!"))
print(str([2, 4, 6, 0, 1]))
print(str(list("Hello!")))
Hello!
['H', 'e', 'l', 'l', 'o', '!']
[2, 4, 6, 0, 1]
['H', 'e', 'l', 'l', 'o', '!']
In [31]:
print(list(42))
<cell>1: error: No overload variant of "list" matches argument type "int"  [call-overload]
<cell>1: note: Possible overload variants:
<cell>1: note:     def [_T] list() -> list[_T]
<cell>1: note:     def [_T] list(Iterable[_T], /) -> list[_T]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [31], in <cell line: 1>()
----> 1 print(list(42))

TypeError: 'int' object is not iterable
In [32]:
board = [
    [" ", "x", " "],
    [" ", "x", "o"],
    [" ", " ", " "]
]

print(board[1][2])
o
In [33]:
blankRow = [" ", " ", " "]
board[0] = blankRow
board[1] = blankRow
board[2] = blankRow

board[1][1] = "x"
print(board)
[[' ', 'x', ' '], [' ', 'x', ' '], [' ', 'x', ' ']]
In [34]:
blankRow = [" ", " ", " "]
board[0] = blankRow[:]
board[1] = blankRow[:]
board[2] = blankRow[:]

board[1][1] = "x"
print(board)
[[' ', ' ', ' '], [' ', 'x', ' '], [' ', ' ', ' ']]
In [37]:
x = (2, 4, 6, 0, 1)
print(x)
for i in x:
    print(i)
for idx in range(len(x)):
    x[idx] = 0
<cell>6: error: Unsupported target for indexed assignment ("tuple[int, int, int, int, int]")  [index]
(2, 4, 6, 0, 1)
2
4
6
0
1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [37], in <cell line: 5>()
      4     print(i)
      5 for idx in range(len(x)):
----> 6     x[idx] = 0

TypeError: 'tuple' object does not support item assignment
In [38]:
prisoner: tuple[int, str] = (24601, "Jean Valjean")
prisoner.append("Paroled")
<cell>2: error: "tuple[int, str]" has no attribute "append"  [attr-defined]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [38], in <cell line: 2>()
      1 prisoner: tuple[int, str] = (24601, "Jean Valjean")
----> 2 prisoner.append("Paroled")

AttributeError: 'tuple' object has no attribute 'append'
In [40]:
a: list = [1, 2, 3]
a.append(a)
print(len(a))
print(a)
4
[1, 2, 3, [...]]
In [45]:
def longest(strs: list[str]) -> tuple[list[str], int]:
    """
    Returns the longest strings in the list strs, as a tuple containing
    the list of longest strings and their length.
    """
    assert len(strs) > 0, "List cannot be empty"
    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]))

ret = longest(["another", "blue", "duck"])
print("ret is", ret)

(res, length) = longest(["a", "blue", "duck"])
print("res is", res)
ret is (['another'], 7)
res is ['blue', 'duck']
In [47]:
import random

def monteCarloPi(rounds: int) -> float:
    """
    Returns an approximation of pi using the Monte Carlo/dartboard technique for rounds rounds.
    """
    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(20))
print(monteCarloPi(1000000))
3.2
3.140312