Calvin (Deutschbein)
W8Fri: 18 Oct
gw.get_element_at(x, y)
>>> id(7)
140732622441080
>>> id(8)
140732622441112
>>> x = 7
>>> id(x)
140732622441080
>>> x = 8
>>> id(x)
140732622441112
>>> x = 1
>>> y = x
>>> x = 2
>>> print(y)
1
>>> id(x), id(y), id(1), id(2)
(140732622440920, 140732622440888, 140732622440888, 140732622440920)
>>> x = [1]
>>> y = x
>>> x = [1]
>>> y
[1]
>>> id(x), id(y)
(2199204204928, 2199208697472)
>>> x = [1]
>>> y = x
>>> y[0] = 2
>>> x
[2]
>>> x = [1]
>>> y = [1]
>>> x == y
True
>>> x is y
False
>>> x = y
>>> x is y
True
>>> x = [0]
>>> def embiggen():
... x[0] = x[0] + 1
...
>>> x
[0]
>>> embiggen()
>>> x
[1]
>>> x = [1,2]
>>> y = x
>>> id(x), id(y)
(2199208697472, 2199208697472)
>>> x += [3]
>>> x, y
([1, 2, 3], [1, 2, 3])
>>> y = x[:]
>>> x, y
([1, 2, 3], [1, 2, 3])
>>> id(x), id(y)
(2199208697472, 2199204204928)
>>> x += [4]
>>> x, y
([1, 2, 3, 4], [1, 2, 3])
Method | Description |
---|---|
x.copy() |
Returns a new list whose elements are the same as the original, same as x[:] |
x.append(val) |
Adds 'val' to the end of the list, same as x += [val] |
x.insert(loc, val) |
Inserts 'val' at the specificed index 'loc' in `x`, pushing the rest of `x` to have an index/location that is one greater. |
x.remove(val) |
Removes the first instance of 'val' from `x` (or error) |
x.reverse() |
Reverses the order of the elements in the list. Like x[::-1], but x[::-1] creates a new list (with a new id/reference) |
x.sort() |
Sorts the elements of the list. Returns `none`, but alters the order of the elements in `x` |
>>> y = x.sort()
>>> y
>>> x
[1, 2, 3, 4]
>>> x = [4,2,3,1]
>>> y = sorted(x)
>>> y
[1, 2, 3, 4]
>>> x
[4, 2, 3, 1]
div6 = [...]
div6 = [... for num in range(40,60)]
div6 = [num % 6 for num in range(40,60)]
>>> div6 = [num % 6 for num in range(40,60)]
>>> div6
[4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5]
>>> div6 = [num % 6 == 0 for num in range(40,60)]
>>>
>>> div6
[False, False, True, False, False, False, False, False, True, False, False, False, False, False, True, False, False, False, False, False]
>>> div6 = [[num,num % 6 == 0] for num in range(40,60)]
>>>
>>> div6
[[40, False], [41, False], [42, True], [43, False], [44, False], [45, False], [46, False], [47, False], [48, True], [49, False], [50, False], [51, False], [52, False], [53, False], [54, True], [55, False], [56, False], [57, False], [58, False], [59, False]]
>>> [print(num,num % 6 == 0) for num in range(40,45)]
40 False
41 False
42 True
43 False
44 False
[None, None, None, None, None]
>>> [print(num) for num in range(40,45) if num % 6 == 0]
42
[None]
[print(num) for num in range(40,61) if (num % 6 == 0) != (num % 7 == 0)]
48
49
54
56
60
[None, None, None, None, None]
>>> def div_6_or_7(lo, hi):
... return len([print(num) for num in range(lo,hi+1) if (num % 6 == 0) != (num % 7 == 0)])
...
>>> count = div_6_or_7(40,60)
48
49
54
56
60
>>> print(count)
5
gw.get_element_at(x, y)