Welcome to PyBeginners – your go-to place to learn Python programming the easy way! 🚀
Today, we're diving into the world of Python loops. By the end of this lesson, you’ll understand how to use the for loop in python and while loop in python. Subscribe for more python programming tutorials!
Whether you're starting from scratch or revisiting the basics, this channel is built just for YOU. We break down Python concepts into bite-sized, beginner-friendly videos using real-life examples, everyday language, and fun visuals.
Think of PyBeginners as your coding buddy — here to help you:
✅ Understand the why behind every concept
✅ Build confidence, one small step at a time
✅ Learn coding without feeling overwhelmed
📚 Check out more learning resources:
📝 Blogs: https://www.pybeginners.com/
💼 LinkedIn: https://www.linkedin.com/newsletters/pybeginners-7267881715929415681/
📖 Free eBooks & PDFs: https://www.usandopy.com/livros/
📌 New videos every week — covering Python basics, hands-on exercises, projects, and career tips!
👉 Subscribe, join the PyBeginners family, and let's make Python simple together 💻💬
#LearnPython #PyBeginners #PythonForBeginners #PythonMadeEasy
Show More Show Less View Video Transcript
0:00
Hey everyone, welcome back to our Python
0:02
journey. Well, this side Raja Lakshmi
0:04
Singum and today we are diving into one
0:06
of the most important topics in
0:07
programming which is loops in Python.
0:10
And by the end of this lesson, you will
0:12
understand how to use for loop and while
0:14
loop and even the special commands which
0:17
is break and continue. And you'll also
0:19
see how operators connect to all of
0:22
this. Don't worry, we'll take it slow
0:25
step by step with examples.
0:31
All right. So this is module five Python
0:35
loops. So think of loops as your
0:37
personal assistance. Like they repeat
0:39
tasks for you until the work is done.
0:42
Now let's start with the most popular
0:45
one, the for loop. So in Python, a for
0:49
loop is used when you want to go through
0:51
a sequence. It could be like a list,
0:53
string or even a range of numbers. So
0:56
here is the general syntax. So it is
0:58
like for
1:00
element
1:02
in sequence.
1:07
So this will be the syntax and here you
1:10
will see a block of code.
1:18
That means Python will take each element
1:20
from the sequence one by one and run the
1:23
block of code for each item. Simple,
1:26
right?
1:28
Let's say we have a list of fruits.
1:33
So fruits
1:36
we have apple
1:48
banana
1:52
orange
1:55
and for
1:57
fruit it could be anything. I'm just
1:59
keeping fruit. You can keep x. You can
2:02
pie.
2:03
So for fruit in fruits
2:08
and now you will just print the fruit
2:20
and that's it. So what happens here? So
2:23
Python takes the first fruit which is
2:26
apple and then prints it. Now again for
2:31
the second round it goes to banana it
2:33
will print. Third round it goes to
2:35
orange and it will print it and it will
2:38
do until the entire list is finished.
2:41
You can give a fruit or it could be
2:44
anything. It's like you're creating a
2:45
variable instantly so that you can store
2:49
one value of the list inside this and
2:52
then at that particular loop it will run
2:55
and then again it changes the value of
2:58
like at first the fruit value is apple
3:01
and now you're storing apple and fruit
3:04
and when you print the fruit you will
3:05
see apple in the first round then the
3:08
fruit moves to banana. So now you see
3:12
banana is stored in fruit and then it
3:14
will print banana next round. Now fruit
3:17
goes to orange. It will print orange as
3:20
the fruit. So this is how it's it could
3:23
be anything. The variable can be
3:24
anything. Even the slash also works for
3:27
this.
3:35
See? So that's the difference. It is
3:38
like you're creating a variable.
3:40
That's it.
3:44
And if you give the fruit and you can
3:45
also check. Easy peasy, right?
3:49
Okay. Now, sometimes we don't want a
3:52
list of words but just a range of
3:55
numbers.
4:00
Like for example for
4:03
number
4:05
in
4:06
range of we have range for this 1 comma
4:11
6
4:14
print number.
4:18
Okay.
4:23
So for number in range of 1a 6
4:27
print number. So this will print numbers
4:30
1 through 6. And notice it stops right
4:33
before 6. So that is how the range works
4:37
in Python.
4:42
1 2 3 4 5 and when it reaches six then
4:46
it will just skip the loop and it'll
4:48
come out of the loop.
4:53
So this is what we use to print the
4:57
numbers using the range function.
5:01
Okay. But what if we want both the index
5:04
and value like for example we want the
5:07
first index zero banana 1 and orange
5:12
three or maybe zero apple one banana and
5:16
two orange something like that. So what
5:19
you can do is in order to get it
5:29
but we have a concept called index. So
5:32
I'm using the same thing. Index is
5:34
something like a keyword that will give
5:36
you index of the item in the list. So
5:40
index come value in the fruits. print
5:46
I'm using a strings again
5:48
index
5:51
index
5:53
comma
5:55
or hyphen value
6:00
and when you run this
6:06
see it is throwing some error like too
6:09
many values to unpack. Do you know the
6:11
reason? That is where we use a concept
6:14
called enumerate function which comes in
6:16
handy. So all you have to do is use
6:18
enumerate
6:22
and you just have to give
6:24
enumerate and you just keep fruits list
6:27
inside it. So that is how the index
6:31
value exactly works. So when you give
6:34
index comma value using the enumerate.
6:37
So now it is showing index zero apple
6:40
that means apple is in zero index banana
6:42
first index or in second index.
6:46
So the output looks like this.
6:50
You get the position and the item at the
6:52
same time. So for that you have to
6:54
remember that we have a concept of
6:56
enumerate which will give you index and
6:58
value of the items inside what inside
7:01
the fruits or whatever the list that you
7:03
want.
7:05
All right, let's spice things up with
7:08
two very useful commands which are break
7:11
and continue which is nothing but
7:17
continue
7:19
will
7:20
skip
7:22
this step any it could be any step any
7:26
line and move on.
7:29
Whereas break
7:35
It means stop the loop completely.
7:46
So continue is nothing but skip this
7:49
step and move on. And break is nothing
7:51
but stop the loop completely. But don't
7:53
worry, I'm going to show you a best
7:55
example for this.
8:00
Like for example for number in range of
8:12
six.
8:14
Actually if you observe this range
8:16
function you're giving the initial value
8:19
and the ending value right. But what if
8:22
you give just one value? So then the
8:24
initial value it will take it as zero
8:26
and the final value will be six. So you
8:30
can even give one value in the range
8:33
function. So for number in range of six
8:40
actually we are writing a program where
8:44
the program skips if it is an even
8:47
number. So number mod 2 =0. So when we
8:51
know that a number is even when it is
8:54
divisible by two and when the output is
8:56
zero. Right? So when the remainder is
8:58
zero that's when we say that the number
9:02
is even. So we want to skip when the
9:05
number is even. So how you will check
9:07
when the number is even? By doing mod 2.
9:10
Mod two will give you the remainder.
9:12
Okay. So if you give division
9:17
then it will give dividend but we want
9:21
the remainder. So for that if the
9:23
remainder is zero then we know that the
9:25
number is even. So we will just
9:29
add continue.
9:31
Okay. And then you will print the
9:34
number. Okay. Let's see. Let's see the
9:36
output and then we'll decide what's
9:38
going on.
9:40
So it's 1 3 5 as the output. Right? So
9:43
if you see here, if we go line by line
9:48
for number in range of six. So at first
9:52
it goes to zero
9:54
and then
9:59
0 mod 2 equal to 0 and then it will
10:02
directly just skip this step. Okay. What
10:04
if the number is 1. So number mod 2 = 0.
10:10
Then this condition is false. So it will
10:13
print directly 1 and then again now the
10:17
number value becomes two. So 2 mod 2 0
10:24
and if this condition is true then
10:27
continue. That means it will skip this
10:29
step. It will skip this step and then
10:33
again now the number value is three 3
10:37
mod 2 0 no then it will skip this entire
10:43
if condition because it is false and
10:45
then directly it will print three and
10:49
now the number value becomes four. Four
10:52
in range of six. Yes, it is still in
10:55
range of six. So if four mod 2 equal to
10:58
0. Yes, it's true. Then it will hit
11:02
continue. That means this this will skip
11:05
this printing the number and then again
11:09
now the n value will be five. Five in
11:11
range of six. Yes, it is in range of
11:14
six. Then if five mod 2 equal to zero,
11:17
it is false. Then it will directly print
11:20
this number as five. And now the number
11:24
value will be six. Six in range of six.
11:27
No, because the range should be less
11:30
than six. So it will just come out of
11:32
the loop. So this is what's happening.
11:35
So this is continue is nothing but it is
11:37
just skipping this entire
11:41
logic when it reaches this condition. So
11:44
when the condition is true, it will skip
11:47
printing the number directly. It will go
11:50
to the for loop and it will change the
11:52
value. It will go to the next step. it
11:54
will skip this entire step because once
11:58
you hit continue it's like you're
12:00
skipping this step and it is going to
12:02
the next step something like that
12:05
okay so we know the continue will skip
12:09
this step and moves to the next one and
12:12
what about the break
12:14
so for break also I'll write one logic
12:16
for
12:19
number in range of 10.
12:29
If number equal to five,
12:34
if number reaches five then then just
12:36
break it.
12:38
Else or you can just print the number
12:42
out of the loop out of the if condition
12:45
not the loop. Okay. So what's happening
12:48
here?
12:51
First the number value is zero.
12:53
Initially the number value is zero. And
12:55
for number in range of 10 is zero in
12:57
range of 10. Yes. So when it is range of
13:01
10 it will check this is true. Now it
13:03
will come if number equal to 5. 0 equal
13:06
to 5. No false. So it will just print
13:10
zero at first. So at first it prints
13:13
zero. Okay. And now the number value is
13:16
1 for one in range of 10. Is one in
13:20
range of 10? Yes. And now 10 equal to 5.
13:25
Is 10= to 5? No. So it will just print
13:28
1.
13:30
Okay. Now the number value is two. Is
13:34
two in range of 10? Yes. Is 2 equal to
13:37
5? No. So then it will just print two.
13:42
Now the number value is three. Is three
13:44
in range of 10? Yes. Is three equal to
13:48
five? No.
13:49
then it will come here and it will print
13:53
three. Okay. Now the number value is
13:56
four. Is four in range of 10? Yes. Is
14:01
four equal to 5? No. Then it will just
14:04
print four. Now listen carefully. Now
14:07
the number value is five. Is five in
14:10
range of 10? Yes. Is five equal to five?
14:14
True. Then it will break. That means it
14:17
will directly come out of it. It will
14:19
not even print the number five.
14:24
If you run this
14:27
and the output is
14:30
see 0
14:33
1 2 3 4. See when it reaches five it
14:37
directly come out of loop. Still we have
14:39
five more steps to do. We can do it till
14:42
10. But because of break it came out of
14:45
the entire loop and it breaked the
14:48
entire loop and just came out and just
14:51
printed the output until then.
14:56
So the moment it reaches five, the loop
14:59
ends. No more numbers after that. So
15:02
this is what for loop.
15:07
I hope I'm clear with continue as well.
15:10
So to recap I could say that so for loop
15:14
help us go through the sequences. Range
15:17
gives us both index and value and also
15:21
we have enumerate function to get the
15:24
index and value for that
15:28
and also continue it will skip the
15:30
entire steps but break it will ends the
15:34
loop early. So loops are like the
15:37
heartbeat of programming. They keep
15:39
things running smoothly.
15:42
All right, my friends. Now, here's a
15:44
little challenge for you. Try writing a
15:46
loop that prints numbers from 1 to 20
15:50
but skips the multiples of three. I'm
15:53
going to attach this in the description
15:55
below as well. So, you can use what you
15:59
just learned with continue. And so, this
16:02
is a small clue that I'm giving to you.
16:05
And if you enjoyed this session, why not
16:07
stick around, hit the subscribe button
16:10
like it's your for loop and repeat it
16:13
until it works and turn on the bell icon
16:15
so you don't miss our next Python
16:17
adventure. Until then, keep practicing,
16:20
keep coding, and remember, every loop
16:22
you write makes you a better programmer.
16:25
See you in the next one.
#Programming
#Scripting Languages
#Computer Education

