[no audio]
Friends, here we are going to use your for loop
for your strings, list, tuple and dictionaries.
Already we have seen how to use your loop for string, list, tuple, right,
but here I want to show you once again with small
modifications, right?
Anyway, there is no modification with your string.
Let me write a simple Python script just to give overall
idea with your for loop to use for your different data structures
and data types.
Let me take 'loops_for_data', something like you can take any
script name right, '_structures.py'.
First of all, let me take my string as anything, 'my_string'
"working with
[no audio]
for loop".
Directly you can print your string.
Right. You can get your output.
Let me write 'my_string'.
You're getting your output.
Now, I want to print your string characters one by one,
in different lines actually I want to print. I mean suppose
in first line I want to get first character,
second line second character, something like that.
[no audio]
Right. See, what I am doing is simply
'for each_char',
I can separate character one by one from your given string
with the help of your loop, for loop. By default you can use it.
Yes. You are getting one by one character.
Now, if you remember instead of these two lines,
in case if your intention is just to print your character, one by one
characters, right, no need to use for loop for your strings.
You have a 'join' method, if you remember.
[no audio]
'join', let me write first your string as it is, and see the
output, what you are getting. You're getting your string by adding
space, but I am going to add '\n', new line. Now see the
output. No need to write two lines of output to print your
characters line by line.
Right. Just you can use 'join' method.
Right. Fine. Then, let me comment this.
[no audio]
There is not much work with your strings. In case if you want
to simply print your characters one by one on your output
side you can take either print, I mean for loop or 'join' directly.
That's okay. Now what I am doing is, let me take my list.
I am writing '1,2,3,4,5' something like that.
Now, you know how to take your 'each' value from your list
one by one,
[no audio]
and you can print it. Right. Yes, observe your output.
I'm able to print your values one by one.
That's fine.
But assume that inside of a list I have a
'values as', okay
let me keep this as it is.
[no audio]
I want to comment this.
That's fine.
[no audio]
Now actually, now 'my_list' is in this way guys. 'my_list ='
list of list. but each and every value is again a tuple.
Yes, you may have in this way. No problem.
[no audio]
Something like this. Now for this I'm using, now,
this is not like one value,
it is like some items, right,
I mean for each tuple inside of a list.
I am taking simply, you can take any variable.
I am taking simply 'each_item in my_list'.
Now, I am printing 'each_item'. Now, see the output.
Yeah. Let me save it and see the output.
[no audio]
From your list this entire thing is actually one first value.
That's what I am taking. 'each_item' is nothing but first value
you're taking. Then you are printing second value.
See, finally this entire thing is a first value in your list like
one, this entire value is second value,
third value. Now you're taking one by one and your printing them.
Yes, we are doing that.
Now one more thing. Let me open my command line.
It's very important, try to understand.
See guys, I have a tuple called
[no audio]
Some number maybe, in this way,
and I'm going to assign this like 'x,y=(5,6)'.
'print(y)', and see the result what you have. You're going
to get 6, and in 'x' you are going to get 5.
[no audio]
Now what I am doing here is, with the help of your for loop
this entire thing I am storing into 'each_item' variable.
I am going to store this first value into this 'each_item',
then I am printing, the second value
then I am printing, third value
I am taking and then I am printing with the help of your loop.
But while taking, here because we have two values
the inside of this tuple, I can unpack it with two variables.
I mean see, I am taking something like
first value and then
second value, 'f,s'.
Now what I am doing is, I am going to print only 'f'.
I'm going to print only 'f', that means from each and every
item I am taking first value and then I am printing that.
Actually, I am taking two values, but first value, 1
I am assigning to 'f', 2 I am assigning so 's'. But I am printing
only 'f'. That's why what is the output you're going to expect is,
from first item 1, from here 4, from here 6.
See the output. Let me run it by saving this code.
[no audio]
One second.
[no audio]
One second.
Yeah, let me save it and run it. See the output.
[no audio]
1, 4, 6, you are getting. Only first values. Now, I am printing
's'. Now in each and every item you are going to get second value.
I want to print both.
Yes, you can print it,
no problem. 'f,s'. 1,2. I mean '(1, 2), (4, 5), (6, 7) you are
getting. One and two.
I mean while taking value from your list if that value is
a tuple, or maybe a list also, then you can unpack it into two variables.
Let me take now instead of tuple,
I can also take inside of a list a list,
or maybe combination of your lists and tuples also, no problem.
But the thing is whenever if you are taking two variables
inside of your loop,
the value which you are taking from your list, that value
should have two values, then only you can unpack it.
First value to 'f', second value to 's'. That's it.
But see that, suppose if I have a list.
[no audio]
Let me take it.
'[3, 4]'. It's fine.
But if I don't have, only if I have one value
you can't unpack it because 'x,y' equals to, you are assigning
one value. That's not possible. 'x=y=3', you can assign it. '3' value
you can't unpack it to two variables.
That's what I am saying here.
Whenever you are taking one by one value, if you have
two values inside of that value, you can unpack in terms
of two variables. Now see the result.
I am saving it. I'm running it.
Yes, you are getting. The same way
you can write tuple, and inside of a tuple you can take tuple
also, combination of tuples and list, you can take anything.
Actually, up to this it's not that much important, but the
concept is important. Why I am giving this is, you have one
very, very important situation while working with your real
time using dictionaries.
First of all, let me define my dictionary.
[no audio]
I am taking suppose 'a', I am writing 1. Then 'b' I am writing some 2
let me take. Anything
you can take it, right. Key as 'c' and value is 3.
Now, I am taking 'for each in my_dic'.
Now, let me print.
You can guess, or you can,
can you expect what is the output
I am going to get if I run this code?
[no audio]
Only keys you are going to get by default.
[no audio]
If this is your dictionary,
if you use your dictionary inside of your loop,
you're going to get only keys,
or if you remember one of the dictionary operation is like
'.keys' is also there.That is a list.
Anyway, if you take only 'my_dic', your dictionary in your
loop, by default your loop, for loop is going to take only keys,
otherwise your 'dic.keys' operation.
It's very, very important guys.
Let me run it and see the output. You're going to get keys
only. But I want to get only values. Now,
you can apply here 'values'.
Now see the result. You're going to get 1, 2, 3, values.
Now I want to get key, and then value. If you remember, there
is an operation called 'items'
on your dictionary. Now see the result.
'each' is nothing but now an item which consists of key and
value. Key and value, because now in each and every variable
you have a tuple which consists of two values. Now I can take
here two variables called 'key,value'.
I mean you can take any variable name, but because this is
dictionary, from the dictionary you are getting key and then
value, now I am taking 'key' and 'value'.
Now if you want to print only keys, you can print it, only 'key'.
See the output. Or I want to print only 'value'.
See the output. I want to get both,
'key,value'. Yes.
That's it. So taking keys and values from your dictionary using for
loop is very, very important. Try to execute this logic again
and again, and see the result with all operations.
This is very, very important guys, working with your dictionary
with '.items', and what is the output you are getting?
See, if I run simply on my command line, you have already dictionary,
what is the output of '.items'? You're going to get a list which
consists of tuple as a value, and in the tuple you have one is
the 'key' and second value is 'value'. Now using for loop
you can take one by one value from your loop.
And while taking because you have a tuple which consists
of two values now I am trying to unpack in this way, 'x,y=5,6',
you know? So now what I am taking
is, 'key,value' I am taking because it's very suitable
word for your dictionary. This is a 'key' and this is a 'value'.
That's why I'm taking variables as 'key' and 'value'.
[no audio]
Right. Okay.
Okay guys, Thank you for watching this video.
[no audio]