Instructor: Welcome back.
I kept mentioning this key term, iterable,
but what does it really mean?
Iterable, you're gonna 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, 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.
Why?
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 gonna hear this
over and over when talking to Python programmers.
So you wanna 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 iterable,
but there's a few special ones that we haven't covered yet,
specifically, dictionary.
We saw how lists, tuple, 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
will have a name of Golem,
will have age of 5,006,
Golem's, pretty old.
And then we'll say, can swim Golem,
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 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 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 wanna 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 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 wanna 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 gonna 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 wanna print for example,
separately the item's name and Golem.
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 and value,
or you might even see shorthand KMV.
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 wanna iterate over the number 50.
What do you think will happen then if I click run?
Sorry, name error, 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,
TypeError: "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.