Earlier this week I posted a quiz about how many iterators there are in this code
numbers = [2, 5, 10, 3, 99, 42]
for number in numbers:
for another_number in numbers:
print(number, another_number)
…let's explore with the help of this visual aid
A short series
Yesterday I talked about tuples when they're used as dictionary keys
Tuples are immutable
But their contents don't have to be…
Let's look at this tuple `book_entry` which contains an integer, a couple of strings, and a list as its elements
twitter.com/s_gruppetta_ct/status/1678690650160791552
What data types can you use in a dictionary?
There are two different answers, one for the keys and one for the values of a dictionary
Values can be any type you want
But there are some limitations for keys
There's nothing magical about arithmetic operators
For example:
>>> my_numbers * 2
returns the same as:
>>> my_numbers.__mul__(2)
`.__mul__()` is the special method which defines what multiplication using * does
But, this can be misleading. There's a bit more happening…
"Why do we use a built-in function, `len()` to find the length of a list but a method, `.count()` to find how often a value occurs?
"Why aren't they either both methods or both built-in functions?"
Great question asked in a session I was running this week…
The discussion led to what seems like an inconsistency
List is not the only data type that has a `.count()` method
And `len()` doesn't work with all data types, either
So, why don't we treat them in the same way?
"Why do we use a method to get the index from a value but we use square brackets for the other way round?"
I was asked this question this week while looking at
>>> cities = ["London", "New York", "Berlin", "New Delhi"]
>>> cities.index("New York")
1
>>> cities[1]
'New York'
You hail a black cab in London: "42 Python Street, please" and you get in the cab
This journey will meander through Python mappings and sequences…
but first, how much do you know about London cabbies, as London's taxi drivers are called?
Let's travel back in time to a pre-Python 3.6 era
The order of items was meaningless
After all, what matters is the key-value association
But things changed in Python 3.6, and again in 3.7, and now the order is maintained
But beware, they're still mappings not sequences
We often learn about lists, dictionaries, and other data types early on in our journey to learn to code…
…but it’s tempting to ignore the details of categories of data types such as iterable, sequence, collection, and so on
I wrote a series of threads about this recently
Over the past 2 weeks I published a 7-day series on classes & OOP
I knew I couldn't talk about everything, but I also wanted to make it reasonably (!) comprehensive
Here are all the threads, from Year 1 to 7 at Hogwarts School of Codecraft and Algorithmancy
It's long
0 of 7