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, edible simply means it is an object or a collection
that can be iterated over.
So again, an iterable can be a list, can be a dictionary, can be a tuple, can be a set.
It's a collection of items.
We also saw that a string could be iterable.
Now these things are iterable y because 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.
Now many objects in Python, like I said, are iterable, but there's a few special ones that we haven't
covered yet.
Specifically dictionary.
We saw how lists, tuples, sets and strings 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.
We'll have a name of Gollum.
We'll have age of 5000, six columns, pretty old, and then we'll say can swim.
Gollum, 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 got an error because I forgot a semicolon here.
And we don't want eagles.
We want semicolon.
And then obviously this needs to be a string as well.
Now if I click run here.
Look at that.
I get name, age and can't swim.
So I've 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 tuple.
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 that you can do and another common pattern when iterating over dictionaries
is what if you want to print, for example, separately the item's 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's 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 value or you might even see shorthand, K and V.
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.
Name her.
Name Key.
Oh, I get an error here because I haven't changed these, so let's change these back.
That's not the error I was expecting.
The error I was expecting was this.
Type error int object is not iterable.
Now this error 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 you in the next video.
Bye bye.