Friends, here
we are going to discuss about loops.
First of all loops are useful to repeat some blocks, to execute
a block of code many times. Why we need to execute a block
of code many times? Block of code is nothing but maybe single
line or multiple lines.
But why we need to execute again and again this block of
code? What is the need, right?
Let me explain this with the first one simple example.
Right. See I am going to save a program named called
'introduction_to_loops.py', and here
what I am doing is, I am taking some list, suppose assume that I
have a list of some values. Now here actually, I have a small
requirement. That requirement is, I need to print the values
of your list one by one, not at a time all your entire list.
I mean, I don't want to print your values
in this way. You are getting output as a list.
No, I don't want to print in that way.
I want to print first 4, then 5, then 6. Then what you are doing?
To get that, first you have to take first value from your
list. With index
I am taking first value.
Then I want to take second value,
then I want to print it.
So taking second value is nothing but 'my_list[1]'. Then
you're printing. Then take third value and then print it.
[no audio]
See the output. Yes, you are getting one by one value.
Okay, for this list suppose you are having three 'print' statements.
Just assume that in your list
you have some this many number of values.
Now, I need to write how many number of 'print' statements?
How many values you have in your list that many number of
'print' statements I need to write. So finally, what is the purpose to write
your 'print' statement that many number of times? Simply you're
taking one by one value from your list and you're printing
that. Taking second value, print; third value' fourth value; fifth value
Likewise, you are taking one by one and your printing it.
So if you have more number of values in your list, you
need to write more number of 'print' statements.
I don't want to write in that way. Because Python is providing,
not only Python in all languages
we have this concept.
Anyway, Python is providing a simple logic.
Just to take one by one value from your list
just to take one by one value from your list. How you can
take one by one value from your list automatically, right?
See that, as of now for your understanding purpose, directly
I am writing syntax called 'for each_value in', your list.
So you can write your variable here or you can also write
directly your list. Both will be fine. Okay. For time being I am writing
directly your variable, sorry your values.
Then after that you have to write ':'. Then 'Enter'.
First of all, what is your health,
what is the action of your 'for' here? Means, because of this
'for', what it will do means, it will take one by one value from your list.
First it will take 4, number 4, and it will store automatically into
your variable called 'each' variable. After that, in case if
you write any number of lines inside of your 'for', these lines
will execute once.
Then after execution of this part once, your Python will
automatically take second value and this value will go and
store into that. Then whatever the lines you have under
your 'for', those lines will execute.
So as of now, my intention is I want to print all the values.
Right. That's why what I am doing is simply 'print(each_value)'.
[no audio]
So guys this 'print' is nothing but, just you are printing your
'each_value' variable, 'each_value' a variable. In this you have
some value. And I am writing this 'print' statement under your
for loop, under your for loop.
So because of this what will happen means, observe that. Whenever
if you run your Python script assume that there is no first
line. As of now assume that there is no first line.
Okay. Then whenever there is a keyword at very
first in a given line, Python will immediately go and look
after 'in'. Whatever
it is there first
your Python will observe this.
Yes, it is a list.
That's why, in list whatever the value we have, first value,
that value will automatically store into that.
[no audio]
Now very first time your 'each_value', 'each_value' variable is
number 4, and after taking that whatever the number of
lines you have inside of your for, those lines will execute
once. But as of now I'm taking only one line. So while executing
that line what I am doing? I am printing the variable of
'each_value', whatever you have.
in this. So that what you will get? You will get number 4.
Right. Then after printing, after the execution of this line,
your Python will automatically take second value, and this
value will store into this variable. After storing your second
value into this variable,
this block will execute once again, but while executing what
we are doing? This variable value we are printing. That's
why you're going to get second value. Likewise,
your Python will take one by one value from here and that
value will store into this variable
first. After that only this
block will execute.
So because of that see what it is doing.
You're getting all values one by one after running your code.
So now, guys actually what you are doing,
I mean your Python? Taking first value, storing into that, executing
this block, then taking second value, storing into this variable,
then executing this block, taking third value, storing
into this variable, and then executing this block. You're repeating
here itself, right?
That's why this is like a loop, and that's why this is called
loop. And one more thing, executing your block of code, right,
with the help of your loop, how many number of times? How many
number of values you have inside of a list, that many number
of times you are repeating, and each repetition is called
iteration. Iteration.
[no audio]
Right. So suppose assume that in 'my_list'
I have four values. Just assume that. Then you are going to
iterate, iterate, this logic, this line, you're going to repeat
this line 4 number of times, or 4 iterations will
happen if you have 4 values.
Not only this, simply your variable, see
I can also do something like 'print' in this way.
Just I'm doing this one.
Let me write three 'print' statements,
not only 'print' statements, you can write anything.
But as of now just for our understanding purpose
I am writing simple lines.
Now, let me remove this list.
Okay. Very first line itself our loop.
I don't want to write this many number of values,
let me write, suppose three values,
same action will be repeated for all values, in case if you
have any number of values. Now what will happen? First thing,
whenever if you are executing, your Python will look very
first word, right. If it is 'for' then 'for' is a loop,
that's why your python will immediately look in the same
line after 'in' whatever you have,
your Python will look that place first.
Then what you are having here? List, which consists of
three values. Then Python will immediately take first value, and
this value will store into this variable. After storing that
the number of lines which you have inside of your loop, those
lines will execute once. While executing, in case if you want
to use this variable you can use it, otherwise no problem.
As of now three 'print' statements. First 'print' statement, simply
some string you're printing. Second 'print' statement, you are
printing some variable value. That value is nothing but the value
which is there currently in this variable. Then after that
again you are printing this.
Right. Now, let me write a different symbol
so that it will be clear for you.
[no audio]
Now, let me run it and see the output.
[no audio]
First time, second time, third time; you are repeating your
logic three times.
This is for first time, this is for second time,
and this is for third time. While repeating, while executing
this first time, 'value', this variable is 4, and we are executing
that. That's why you are getting output as a 4. Now,
I don't want to use this 'each' variable inside of my loop.
Yes, no need to use that.
It's not mandatory. Based on requirement we will use it.
I am writing simply,
"This is a iteration". Any message also I can write it.
[no audio]
Let me save it and observe the output. I am running.
While executing first time, three 'print' statements,
"--This is a iteration", and then some line.
That is for 'each' value 4. Then second time, 5 value, with
5 you are repeating this, six you're repeating that,
that's why you are getting three times.
So guys here, not only take, I mean you can take any variable.
Not only 'each_value',
I can take simply 'x' also. No problem. See the output.
[no audio]
That's it. Okay. So this is called simply your for loop.
So finally you have to understand that this for loop is useful
to repeat some block of code or some set of lines
based on your requirement how many times you want to repeat.
So that is decided by the value which you are providing here.
Not only list, you can take some other things as well.
I will show you that. Right. Fine.
So why loops are useful guys?
So in all programming languages we have a requirement to
execute some block of code number of times, many times.
So that's why we are using loops concepts. Now in Python
we have two types of loops - for loop and while loop.
[no audio]
Okay. Now, for loop. For loop in Python is used to iterate or execute
many number of times over a sequence. That sequence maybe
a list or tuple or string or any other iterable objects.
As of now, don't worry about iterable objects.
[no audio]
You are going to repeat some block of code using loop, and
while repeating, see previously we used somewhere here list,
right. Not only list,
you can also provide a list, tuple, or a string.
Let me show you that.
[no audio]
This is using list, right?
I can also use in your loop a tuple also, no problem.
[no audio]
Let me do that now with the help of tuple.
'for value in', this is a variable guys,
you can take anything based on your requirement, in your tuple,
let me write directly my tuple. '(4,5,6', then ', "hi")'.
Now how many values are there in your tuple?
Four values. This is a string,
but this string is one value for your tuple. Four values.
Right. Now, simply
I am writing, 'print("hi")', or simply
I'm writing this "=". Then because of your loop, for loop
you are repeating this line, you're printing this line,
you are repeating this line.
How many values you have? Four values.
So four times you are going to execute this line again and again
immediately. Now see the output. Four times you are getting.
But while executing the block which is there under your loop
at that time in case if you need a variable value of, 'value'
you can use it. Now see that. That's it.
So whether you want to use your variable, whatever you have
here, that variable you can use it or you can skip it.
Right. Now, let me give one simple example.
[no audio]
Assume that. Yeah, before going to that example
I will take one more thing, string. As of now
we worked with list, tuple, then let me take string.
After that, I will go with one example.
For, let me take, yeah, 'for each_char in', let me take 'python scripting',
space is also a string, right, then ':'.
Then what I am doing is, 'print(each_char)',
or if you don't want to do that,
[no audio]
let me simply do simply "python".
Okay. Then simply print, I am printing hyphens.
So now you are going to repeat this line based on how many
characters are there in a given string.
So how it is going to repeat means, immediately, whenever if
you run your Python script, because this is a loop, 'for', your
Python will take first character from here.
If it is a list it will take first value from your list, or
if it is a tuple it will take first value from your tuple.
But now this is a string, so it will take first character
automatically and that character it will store into this variable.
After storing that, whatever the logic, whatever the block,
whatever the lines you have under your loop, those lines will
execute once. While executing if you want to
use this variable you can use it,
otherwise you can skip it. As of now, I'm not using. Simply
what I am doing? See the output, you are getting hyphens.
How many times? Six times, because you have six characters.
Now what I am doing is, along with your hyphens,
I'm also going to print the characters which is stored in
your 'each_char' variable. Now see the result.
'p-y-t-h-o-n'. See, first time your Python is taking letter 'p', character
'p', and it is storing into this variable. After storing into this variable,
this line will execute once, first iteration.
This is called first iteration. Then because of that you are getting
'--', and then 'p'. Then Python will immediately go and take next
character 'y'. Then 'y' is going to store into this variable. After
that your Python will execute this line once. So while executing
now you're printing this variable. Currently the value which
is there in your 'each_char' variable is 'y', that's why
you're getting 'y'. Likewise it should go to up to 'n'. Right.
Assume that you just now printed 'n',
I mean with the help of your loop, after that again your
Python will try to get next character from your string but
you don't have anything. All characters are over.
That's why your Python will come back, come out from your
loop, and after loop if you have any lines, those lines will execute.
Right. That's it.
So guys, 'print' statement,
I want to repeat. That's why that line should be there under
this for loop.
That's why I am providing some space. Suppose
if I write one more line, 'print()'
[no audio]
"we are working with loops". Now see that. This line will execute
only once. When it is going to execute that one time is after
complete execution of
your loop. Because this 'print' statement is outside of your
loop, there is no relation between this line and your for
loop. So in case if you have a relation between this for
loop and this line, this line should be there inside of your
loop. If you want to write this last line inside of your
loop, you have to provide some space, and the lines which
are there under your loop should have same space for each
and every line. First
let me run it and see the output.
[no audio]
See this last line you're
getting only once because that is not a part of your
loop. It is outside of your loop. Only the lines which are
there under your loop, those lines will execute, right. Now
I want to write that line also inside of my loop
then write for all your lines same space,
right, so that these two lines are there now under your loop.
Now your loop will repeat these two lines based on how
many characters you have in your string.
So six characters, now these two lines are going to repeat
six times. See that. That's it.
[no audio]
Right. Fine.
Now, let me go with one simple example as of now.
[no audio]
Assume that
I have a list which consists of some values.
Otherwise, you can also read your list from your command
line, right. Using your 'eval(input(
"Enter your list of values: "))'
you can read it. As of now
I am taking 'my_list'.
I don't know what are the values there in 'my_list',
[no audio]
okay. Maybe any number of values.
Now my requirement is,
[no audio]
I want to
print all the values from
your loop, from your list.
Okay, first let me print that. 'for each in my_list', then
let me 'print(each)'.
[no audio]
Yes, I'm printing. Now
I'm able to get one by one value. But before printing I want
to decide whether it is a even number or odd number. If it
is even number I want to print your number, then "even". If it
is odd number, I want to print your number and then "odd". Right.
So that's why what I am doing is, before printing I am finding
remainder, remainder with two. So 'each
%2', and if remainder value, if it is equal to 0,
I can say that that is an even number.
Now guys, this 'print' is under your 'if'. Be clear.
If this condition is satisfied then only I want to print this line,
that's why I'm writing this line under your 'if' condition.
So if it is 0 then I want to write '--'. Let me write 'f' string, that's better.
So whatever the variable you have, " is even". That's it.
'else', 'else' means remainder is not equal to 0, then I can say
your number, then
"is odd". Guys, simple example, but it is useful.
Why means, I am writing number of lines inside of your loop,
now all these lines are going to repeat based on how many
values you have in your list. As of now
this is a list. It may be a list, or string, or tuple, for time being
Now run it and see the output. 3 is odd, 4 is even, 34 is even. Yes,
it's working perfectly. Right. Guys, try to understand this
loop concept by practicing again
and again same code. See while writing you have to think what
we are doing, what is the step
your loop is performing, right?
If you are able to think about it, then you can easily understand your loop.
See in real time loops are very, very important.
Right. Suppose assume that you are going to write some hundred
lines of some script.
So if you are going to write some 100 lines or 200 lines
of Python script, then definitely you are
going to use 'if' conditions and loops,
max. Okay.
Okay, guys, thank you for watching this video.
And one more thing guys, we have while loop as well, but this
we'll discuss later.
So actually while loop is used to execute a block of statements
repeatedly until a given condition is satisfied. As of now
don't go with your while loop,
just think about loop, for loop.
Okay. Anyway I will discuss about your while loop later.
Okay. Thank you. Bye.
[no audio]