Welcome back.
I kept mentioning this key term iterable, but what does it really mean, iterable?
You're going to hear that over and over in Python programming and other programming languages as well.
And as we discussed a little bit in the previous video, iterable simply means it is an object or a
collection that can be iterated over so again and iterable can be a list, can be a dictionary, can
be a Toppo, can be a set.
It's a collection of items.
We also saw that a string could be iterable.
Now these things are iterable.
Why?
Because if they can be iterated.
That is iterated means that we can go one by one.
To check each item in the collection.
And you're going to hear this over and over when talking to Python programmers, so you want to make
sure that you have that vocabulary, you're iterating over something, you're looping over something.
So iterable is the noun and iterated or iterate is the action of iterating over an iterable.
I know it's a little confusing now.
Many objects in Python, like I said, are edible, but there's a few special ones that we haven't covered
yet, specifically dictionary.
We saw how lists, tuple sets and strengths can be iterated.
But what about an object?
Let's say we have an object here and this object will be users and users or user will have a name of
GOLOMB.
We'll have age of five thousand six gallons, pretty old, and then we'll say can swim, golomb, let's
say, cannot swim false.
What if we use this object right here?
For item in user and we print.
Right here, the item.
What do you think will happen?
Let's run this.
Well, I get an error because I forgot a semicolon here, and we don't Weigel's we want semi-solid.
And then obviously, this needs to be a strength as well.
Now, if I click run here.
Look at that, I get name age and can't swim, so I printed the keys of the dictionary.
Now this dictionary, we have the keys.
That's great, but hmm.
What if I wanted actually the values?
Is there a way to do this?
Well, dictionaries have three methods that are really, really useful when we want to loop over their
keys and values, the first one is items.
And with items, when I do this and I click run.
You'll see that I get the key value pair in a couple.
And this is a very common pattern that you'll see a lot.
Another one that we have.
Is the values.
And guess what that does?
That gives us the values of.
The dictionary.
And we saw that user just leaving it blank gives us the keys, but we have a method that is more descriptive
and allows us to show exactly what we want to do, which is keys.
And if we run this.
You'll see that I'm able to iterate over the keys.
And these three methods are very common, and you're going to use them a lot in your programming career.
So make sure you take note of them items, values and keys.
It allows us to iterate over dictionaries.
But another interesting thing they can do and another common pattern when iterating over dictionaries
is what if you want to print, for example, separately the items name and column?
So right now we're returning a tuple and we saw that we can do tuple unpacking.
Right?
I can say key value equals item and then just print key value if I run this.
You see that I'm able to print these.
But there's actually a shorthand way of being able to do this in here in the for loop.
I can say key.
Value.
And just completely avoid this entire line.
And if I run this.
There you go.
It still works and this, again, is a very common pattern where if you're collecting the items of a
dictionary, you can separate them into key value or you might even see shorthand.
OK, and the.
Again, these can be any variables that you want, you can name them whatever helps your code be more
readable.
By the way, just for fun, let's say, in the last line here, I want to iterate over the number 50.
What do you think will happen then if I click run?
Sorry, Namir Naki, hope I get an error here because I haven't changed.
So let's change these back.
That's not the area I was expecting, the error I was expecting was this.
Type air int object is not iterable.
Now, this area should make sense now, because I've said it over and over, why is it not iterable?
Well, because it's not an object that can be iterated over.
It's not a collection of items because we learned that in Python, these are iterable I'll see in the
next video, Papy.