Commit f7bc096b authored by Gökçe Aydos's avatar Gökçe Aydos 🌈
Browse files

clarifications, typos

parent 468ce3e0
Pipeline #13012 passed with stage
in 6 minutes and 41 seconds
......@@ -353,6 +353,36 @@ contd...
more info: [data structures tutorial - tuples and sequences](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences)
## 3
We said that lists can also be used for unpacking. Let us see some examples:
```py
x = [1, 2, 3]
print(x)
# outputs the list: `[1, 2, 3]`
print(*x)
# outputs print(x[0], x[1], x[2]): `1 2 3`
```
## 4
What about other containers?
```py
d = {1:'a', 2:'b', 3: 'c'}
x, y, z = d
print(x) # outputs `1`
print(*d) # outputs `1 2 3`
s = {1, 2, 3}
x, y, z = s
print(x) # outputs `1`
print(*s) # outputs `1 2 3`
```
Nice!
##
<!-- editing does not work on tuples, tuples can be defined w/o parantheses -->
......@@ -543,7 +573,7 @@ d = {'Mounika' : 'LSI',
'Sriraam' : 'MCS'}
```
A) the names in `d` are the keys of key-value pairs
A) the forenames in `d` are the keys of `d`
A) every key in a dict is unique
A) values can be of every datatype
A) keys can be of every datatype
......@@ -551,8 +581,13 @@ A) I don't know
. . .
Python generates a hash, i.e., a unique number, out of each key which is not allowed to change during the lifetime of a dict. Therefore keys must not be immutable.
Python generates a hash (a unique number) out of each key which is not allowed to change during the lifetime of a dict. Therefore keys must be immutable.
Why can we only use immutable items? Couldn't we create a hash out of a `list`?
It is possible but then we would have to calculate the hash everytime we use the list as a key. You would had to pay attention that the list never changes. A better way is to not allow it at all by using for example `tuple`s.
Practically Python does not allow this [by looking for a method called `__hash__` to see if an object can be used as a key](https://stackoverflow.com/a/25193267).
##
<!-- similarity between dict and set -->
......@@ -633,8 +668,8 @@ Which code deletes all characters which belong to the house Gryffindor?
A) `del v`
A) `del k`
A) `d.pop(k)`
A) `d.popitem()`
A) `character2house.pop(k)`
A) `character2house.popitem()`
A) I don't know
. . .
......@@ -654,13 +689,15 @@ A) I don't know
##
<!-- keys, values, items -->
which iteration does not work?
`d` is a `dict`. Which iteration does not work?
A) `for k in d.keys()`
A) `for k, v in d`
A) `for v in d.values()`
A) `for k, v in d.items()`
<!-- TODO what if k,v are tuples in B) -->
. . .
- `for k in d` iterates over the keys of `d`
......@@ -847,6 +884,9 @@ A) I don't know
##
<!-- | (union), - (difference), & (intersection) -->
Fill the gaps between `a` and `b`:
```py
a = {1, 2, 3}
b = {2, 3, 4}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment