= int(); f = float(); a = list()
i = int(7); f2 = float(10); b = [1, 2]
i2 print("i:", i, "f:", f, "a: ", a)
print("i2:", i2, "f2:", f2, "b: ", b)
i: 0 f: 0.0 a: []
i2: 7 f2: 10.0 b: [1, 2]
April 22, 2025
Increase the number of bits dynamically (python): not practical because not easy to implement in hardware.
Error warning: not practical because way too frequent.
Don’t do anything: e.g.: \(255_D + 1_D = 255_D\): Associativity doesn’t hold anymore \(255_D + (1_D - 1_D) = 255_D \neq 254_D = 255_D - 1_D = (255_D + 1_D) - 1_D\)
(Cyclical) Modulo arithmetic, k bits => modulo-\(2^k\) (% in python):
\(255_D + 1_D = 0_D\)
advantages:
Elementary operations are ones that are defined for all data types:
__init__(self, ...)
has to be implemented.==
). Two different equalities:
equal: same value / contents
identical: same object in the memory
comparing the identity of objects with id()
or with is
a = [1, 2]
b = [1, 2]
c = b
print("1:", id(a) == id(b))
print("2:", id(c) == id(b))
print("3:", a == b)
print("4:", id(c) == id(b))
print("5:", c == b and c == a)
print("6:", a is b or a is c)
print("7:", c is b)
1: False
2: True
3: True
4: True
5: True
6: False
7: True
c
and b
refer to the same memory location, different from the location of a
. all of a
, b
, c
have equal values; [1, 2]
.
consequense: value-semantics vs reference-semantics (see below)
Analogy: Accessing a website:
What does it mean for programming languages, specifically for python:
==
is True
and is
is False
)==
and is
are True
)Example
if we want to create another list object with a different identity but same value as a
we use .copy()
operator:
1: True
2: False
3: [1, 2] [-1, 2]
Elementary data-types like numbers or booleans are immutable by default, which gives the ‘illusion’ of reference semantics.
Array (list in python) is the most important / fundamental container data type:
a = new_array(size, initial_value)
interpretation: create an array with size
elements all of which initially have initial_value
as value
axioms:
len(a) == size
and for all i in [0,..., size - 1]: get(a, i) == initial_value
python:
k = len(a)
interpretation: number of elements in a
axioms:
k == size
python:
v = get(a, i )
interpretation: get the element at the position i
axioms:
0 <= i <= size - 1 and v == element of a at position i
len(a) == size
and for all i in [0,..., size - 1]: get(a, i) == initial_value
python:
set(a, i, v)
interpretation:
axioms:
0 <= i <= size - 1
get(a, i) == v
python:
i < 0
: -size <= i <= size - 1
. Then:
i < 0 => get(a, i) == get(a, len(a) - |i|)
Dyanmic arrays: here we simply give the operations
append(a, v)
a.append(v) # efficient in pyton
append(a, other_array)
a.extend(other_array) # efficient
insert(a, i, v)
inserts and elements after position ia.insert(i) # less efficient
pop(a)
(remove last eelment)a.pop() # efficient in python
remove(a, i)
(remove the ith element)a.pop(i)
or del a[i]
clear(a)
delete everythinga.clear() # now len(a) == 0
Associative arrays are called dictionary in python.
students[1234567] => "Alice Smith"
In python lists are automatically also stacks. (They can be used as stacks)
only the operations and python versions:
top()
s[-1]
top()
s[-1]
pop()
s.pop()
push(s, v)
s.append(v)