Welcome to PyBeginners β your go-to place to learn Python programming the easy way! π
Today, we're diving into the `python while loop`, a fundamental concept in `python programming`. This `python tutorial` offers clear `python basics` to help beginners grasp this concept, enhancing their `python coding`. Subscribe for more coding exercises! π»π¬
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 π»π¬
Show More Show Less View Video Transcript
0:00
Hey everyone, welcome back to the
0:01
channel. Today we are going to learn
0:03
about something super important in
0:05
Python, the while loop. Well, this Raj
0:07
Lakmi Singum and if you're just starting
0:10
out, don't worry. I'll walk you through
0:12
step by step in a way that makes it
0:14
super easy to understand. So, let's jump
0:16
in.
0:18
So, what's exactly a while loop?
0:27
A while loop in Python allows you to
0:29
repeat a block of code as long as the
0:32
condition is true. Think of it like it's
0:35
like saying keep doing this task until
0:38
the condition is no longer true. Like
0:41
for example, imagine you're drinking
0:43
water from a bottle and you'll keep
0:46
sipping while the bottle is not empty.
0:49
So once it's empty, you stop. So that's
0:53
how a while loop works.
0:56
So here's the basic syntax of while. So
0:58
it's like while condition then this
1:02
while condition is true then this block
1:04
of code will be repeated until the
1:06
condition is true. Once the condition is
1:08
false then it will directly come out of
1:10
the loop.
1:12
So the loop actually checks the
1:14
condition first and if the condition is
1:16
true it runs the block of code. If the
1:19
condition is false then it stops right
1:21
there. Okay, let's write a simple
1:24
counter as an example. So what I'm going
1:27
to do is I'm going to create a counter
1:30
variable and assign a value zero and
1:33
while counter
1:38
less than five
1:41
I will just print
1:46
counter
1:54
counter and then I will directly
1:56
increment counter equal to counter + 1.
2:04
Okay. So if you observe counter equal to
2:06
zero, I created a variable called
2:08
counter and while counter is less than
2:11
five. So when the value of counter is
2:13
less than five, I want the computer to
2:16
print the value of counter and then
2:20
increase the counter + one for every
2:22
loop. Okay, let's check it.
2:26
So at first the counter value is zero.
2:31
Okay. And now the counter less than
2:34
five. So 0 less than five. True. So then
2:38
it will directly print the counter value
2:41
as zero. And now the counter value
2:44
became counter equal to counter + 1. 0 +
2:48
1 will be 1. So now the value of counter
2:51
will be one here. So one less than five
2:54
true. then it will directly print one.
2:58
Okay. And then now here the value 1 + 1
3:04
will be two. Again the computer jumps
3:06
here and checks the now the counter
3:08
value is two. Two less than five. True.
3:11
Then it will directly print the counter
3:14
value as two.
3:16
And now again it increments the counter
3:19
by one. Increases the value by one. That
3:22
means 2 + 1 3. Now again the computer
3:25
goes here 3 less than five true then it
3:28
will directly do counter value as three
3:32
and now the counter 3 + 1 is four is
3:36
four less than five true then again the
3:40
counter comes here and it will print the
3:42
value of counter as four and then
3:45
now the counter value is four + 1 which
3:48
is five and five less than five are you
3:52
guys listening to Is five less than
3:55
five? No. Then the condition is false.
3:58
Then it will come out of the loop. So
4:00
the value 0 1 2 3 4 will be printed as
4:04
output. So this is how loop exactly
4:08
works.
4:10
Let's check
4:14
0 1 2 3 4. You got me?
4:19
So here we start with counter value zero
4:23
and then the condition says while
4:25
counter less than five that means as
4:28
long as the counter is less than five it
4:30
will print the value and then increase
4:33
it by one and the moment counter reaches
4:37
five the loop stops.
4:40
So this is how you will see the printed
4:43
numbers from 0 to 4. Simple right?
4:47
So this is how it works and also if you
4:50
observe counter equal to counter + one.
4:52
So there is a short form of this.
4:55
So you can also write
4:58
counter
5:03
plus equal to 1.
5:07
So it is same as this counter plus equal
5:10
to 1 is same as counter equal to counter
5:13
+ 1. Instead of writing the variable
5:15
twice, you're writing it just once. By
5:17
just adding it plus equal to 1, you're
5:20
saying to the computer that counter
5:22
equal to counter + one. It's a short
5:24
form notation. It's easy even when you
5:26
are coding. So that is why. But there is
5:30
something you need to be careful about
5:32
which is infinite loops.
5:35
What if the condition never becomes
5:37
false? It's always always true. then
5:41
your loop will run forever. So always
5:45
make sure your loop has a way to stop
5:48
like updating a variable inside it.
5:54
Like for example,
5:59
if you see here counter equal to zero
6:05
and then I'm not doing anything. So as
6:08
long as the while counter equal to zero,
6:11
the counter prints.
6:13
Let's see what happens.
6:20
See, it's keep on going. The loop is
6:22
keep on ending. The task is keep on
6:24
loading. So this is really a bad way to
6:27
write a program. So always always update
6:31
the tasks
6:32
whenever you are changing the condition.
6:42
So keep the condition clear and easy to
6:45
understand and at the same time when you
6:47
are tracking it when you're tracing it
6:49
make sure that loop is not infinite not
6:52
in the in the small programs you might
6:54
feel it okay you can do it but in the
6:58
long run when you're giving a very big
7:00
logic to a very big applications when
7:02
you're dealing with complex applications
7:04
and at the time when you give this
7:06
infinite loop then it's going to kill
7:08
the entire application by just you know
7:11
printing the output again and again and
7:13
again that you don't even want to. So
7:16
always trace it. Trace the loops.
7:19
Whatever it is, keep the pen and paper
7:21
with you. Trace it first. Do it
7:23
yourself. Think like a computer.
7:26
Computer is really dumb. You have to
7:28
tell it each and every step to
7:30
understand to make it understand. So
7:32
that is why I tell you please keep pen
7:35
and paper.
7:37
So always be aware of these infinite
7:40
loops.
7:42
I can give you one more example
7:45
like for example while
7:48
just give true here
7:54
and
7:58
if counter equal to if counter equal to
8:03
5 break
8:09
then print
8:11
counter
8:14
and also increment
8:17
counter
8:19
+ = 1. So here if you see whenever you
8:25
give the condition true then it is going
8:28
to take it as infinite loop right
8:30
because while true will be running keep
8:32
on running but look at the magic of this
8:36
break statement now let's check let's
8:38
run it let's trace it first so counter
8:42
value initially zero and now if counter
8:46
equal to five no it is false
8:50
So
8:52
it will skip this step entire step and
8:55
it will print here the value of counter
8:58
zero in the beginning. Okay. And now the
9:01
value of counter is
9:04
counter + 1. 0 + 1 is nothing but 1.
9:09
Okay. And now while it is true, yes, it
9:13
is always true. So here also it will be
9:15
true. If 1 equal to 5, then again this
9:18
condition is false. So this entire
9:21
statement will be skipped. And now it
9:24
will print the value of counter as what?
9:26
One. And now the counter is incremented
9:30
from 1 to two because 1 + 1 equal to 2.
9:35
Now the counter value is two. And again
9:38
while true. Yes. And if 2= 5 false this
9:43
condition is false entire statement is
9:46
skipped and it will just print the
9:48
counter value as two and now the counter
9:52
value 2 + 1 is nothing but three. So
9:57
while true yes if counter that means 3
10:01
equal to 5 false. So it will directly
10:04
print the value three. Now the counter
10:08
value is what? 3 + 1 = 4. Okay. Again
10:13
while true, yes. If counter that means 4
10:17
equal to 5, no, false. So, it will
10:20
directly go here and it will print four.
10:24
Now, the counter value is 4 + 1 which is
10:27
five. And while true, if equal to 5,
10:31
listen to this. If five equal to 5, yes,
10:34
this is true. Then it will directly come
10:37
out of this loop. it will directly break
10:40
this entire loop and the output will be
10:43
0 1 2 3 4. So that's the beauty of this
10:47
break statement.
10:49
So though the entire loop is infinite,
10:52
it just break as soon as it met the
10:55
condition.
10:57
So the moment counter becomes five, we
11:01
break out of the loop. Let's run this.
11:08
0 1 2 3 4. So this is the output that we
11:11
got
11:15
and also let's see the beauty of
11:17
continue statement. So counter equal to
11:20
zero while counter let's give let's
11:23
change the logic a bit. So while counter
11:26
less than zero
11:33
counter
11:36
plus = 1.
11:40
If counter
11:57
and hit continue.
12:00
Print
12:03
counter. So let's trace this also.
12:06
What's happening here? If possible, you
12:09
can pause this video at this moment,
12:11
take a pen and paper and write yourself.
12:14
Anyway, we are going to do it once
12:16
again.
12:17
So counter equal to zero at once. So
12:21
while counter less than I have to give
12:24
it five here. While counter less than
12:26
five 0 less than five true. So now the
12:30
value of counter will be 0 + 1 which is
12:33
1. And now the counter value is one. So
12:37
1 mod 2 equal to zero.
12:42
No. So this condition is false. So it
12:45
will directly come here and it will
12:48
print the value of counter as one. Okay.
12:51
Again now it goes here. Now the counter
12:53
value is 1. One less than 5 true. So
12:57
counter value becomes counter + 1 which
13:00
is two. 2 mod 2 equal to 0 true. So then
13:05
it will just skip this entire
13:08
instructions. This entire thing and then
13:10
again it jumps here. Now the counter
13:13
value is two. Like basically we are
13:16
skipping
13:17
printing the counter when it is an even
13:19
number. So that is what we are doing. So
13:23
it will skip this entire now the counter
13:26
value is 2. 2 is less than 5. True. Now
13:28
the counter value becomes three. 3 mod 2
13:32
equal to0 false. Then it will print the
13:35
counter value as three. And again now if
13:40
you go here now the counter value is
13:42
three. Three less than five true counter
13:46
plus equal to one. So now the counter
13:48
value will be four. And 4 mod 2 equal to
13:52
0 which is true. So then this entire
13:54
condition will be skipped. Entire steps
13:57
will be skipped. And now the counter
13:59
goes here. The counter value is four. So
14:02
four less than 5 is true. So the counter
14:06
plus equal to 1 that means the counter
14:09
value will be five. So if 5 mod 2 = 0
14:14
false then it will print the value five
14:19
and again now it goes here. Now the
14:21
counter value is 6. 6 less than five. No
14:26
it's false.
14:28
So it will come out of the loop and it
14:30
will print to the output as 1 3 and 5.
14:38
So this is the output 1 135. So whenever
14:41
the counter is an even number, it's just
14:43
skips printing it. So the output will
14:46
only show the odd numbers which is 1 3
14:48
and five.
14:52
So that's the beauty of continue.
15:00
And here is a fun one because you can
15:02
even add an else block to a while loop.
15:07
So once this condition becomes false
15:10
then it will directly go to the else
15:12
block and runs the loop has ended or
15:16
whatever the message that you want user
15:18
to see it like let's do one more
15:21
example. So while counter less than
15:23
five.
15:30
So print counter
15:34
and increment count
15:37
plus equal to 1.
15:41
And then you can add else part and you
15:44
can print saying that
15:47
the while loop
15:51
has ended.
15:57
Okay, let's try. So the counter value as
16:01
soon as it reaches five, it just have to
16:03
come out of this loop and it will print
16:05
the entire message to the user. So it
16:08
will just print 0 to 4 0 1 2 3 4 and it
16:12
will say that the while loop has ended
16:17
which is nice and clean. Even user will
16:19
understand that okay the loop came to an
16:21
end.
16:24
So it's not mandatory to use else part
16:27
because most of the time we don't use
16:29
else part but in case if you need it in
16:32
case if you need to do it then you can
16:34
use it. So there's an option that you
16:36
can choose.
16:38
So final wrap-up. So here we worked with
16:41
while loop in Python. So we use a while
16:44
loop when you want to repeat the code as
16:47
long as the condition is true. And also
16:50
be careful with the infinite loop and
16:53
also always use break to exit early and
16:56
also continue
16:59
to skip certain steps. And don't forget,
17:03
you can even use else with a while loop.
17:06
Something like this.
17:09
All right, friends. That's it for
17:11
today's lesson on while loops in Python.
17:13
If this video made loops a little less
17:16
scary for you, then I'd love for you to
17:19
join our little coding family. So, go
17:21
ahead, hit that subscribe button, give
17:24
this video a big thumbs up, and ring the
17:26
bell icon so you don't miss the next
17:28
one. Remember, learning to code is like
17:31
running a loop. You just keep practicing
17:33
until you succeed. See you in the next
17:36
one.

