0:00
Hey everyone, welcome to Simplified by
0:02
Singum. Welcome back to our Python for
0:04
beginners series. Today's exercise is
0:06
super exciting because we are going to
0:08
print the first n even numbers. If you
0:12
have just started coding, this is a
0:13
perfect way to understand loops and how
0:16
Python can generate sequences of numbers
0:20
And here is what our flowchart tells us.
0:23
So first we ask the user to enter a
0:26
number and let's call it n. And this
0:29
number tells us how many even numbers we
0:31
want. And we start counter at i = 2. And
0:37
because 2 is the first even number and
0:40
then we enter a loop and we print i and
0:44
we increase the i by two. So we always
0:48
get the next even number.
0:51
It's like saying keep adding two numbers
0:54
and printing the number until we have
0:57
reached the count of the numbers that
1:02
It's like for example user gave value
1:05
six. So it will give you the values for
1:09
the first six even numbers. That means
1:11
it will add two six times and it will
1:14
give you the first six even numbers. 2 +
1:17
2 + 2 + 2 + 2 and + 2.
1:23
Simple, right? Now, let's write the
1:25
Python code to make it happen. Always
1:36
Let's call it n. And we need this
1:38
integer this time. Input.
1:42
Enter the value of n.
1:49
Okay. And you will print.
2:09
So we'll create a variable called i and
2:11
we'll give the initial value two. And
2:14
we'll also create one more variable
2:15
called count. And we also give it zero.
2:44
And then also you will increment count
2:53
count equal to count + 1.
2:58
Let's break this down. So what's
3:01
happening here? Here we are taking the
3:03
user value n as input. So user will give
3:07
you how many even numbers he or she
3:09
want. And we are printing the first n
3:13
even numbers like you gave me to get six
3:15
even numbers. So this is what this print
3:18
statement is for. And then we created
3:21
two variables. One is i because two is
3:24
the first even number. We gave the value
3:26
two. And we gave the count value zero
3:28
because this will keep track of how many
3:31
numbers we have printed so far. And
3:34
while count less than n, this loop keeps
3:38
running until we have printed n numbers.
3:42
Like for example, the n value is maybe
3:46
four. Okay. And you and you will tell to
3:50
the user that first four even numbers
3:52
are something like that. And then you
3:56
gave I value two, counter value zero.
3:58
And now initially count value is zero
4:00
and n value is four. Is 0 less than
4:03
four? true then it will come here it
4:06
will print two as output.
4:09
Okay. And now the I value becomes
4:12
initially it was two and now the I value
4:14
becomes four because I = I I + 2 which
4:19
is nothing but 2 + 2 is nothing but four
4:21
and now the count value changes to 1.
4:26
Initially it's zero and now it became 1.
4:28
Now again the program jumps here. Is 1
4:31
less than four? Yes. Now again you will
4:36
print I which is nothing but four this
4:40
time. So this will be four. And now
4:44
again I value increments by two. And
4:47
this time the I value will be six and
4:51
count value incremented by one. And now
4:53
the count value becomes two. And now
4:55
again the program jumps here. Count
4:57
value is two. Is two less than four?
5:00
True. So it will print the i value which
5:06
And again the i value increments by two
5:09
and this time it becomes 8 and count
5:12
value also will be incremented by one
5:15
and this time it will be three. And
5:17
again now the program jumps here is
5:19
count less than three less than four
5:22
true. And now again it will print i
5:26
value. Now it will print the i value and
5:28
the i value is 8. So it will print 8
5:33
and again now I value will be plus equal
5:35
to 2. So this time the i value will be
5:40
and the count plus equal to 1
5:44
and this time the count value becomes
5:47
And is four less than four? Listen to
5:49
me. Four less than four false. So it
5:53
will come out of the loop and you will
5:55
see these outputs 2 4 6 8. So it is
5:59
printing the first four even numbers.
6:12
maybe four. So the first four even
6:15
numbers 2 4 6 8. But instead of getting
6:20
in you know each output in one line I
6:22
want all of them something like this.
6:25
something like 2 4 6 8 and I want the
6:32
output something like this. So how can I
6:35
get that? So for that all you have to do
6:43
So it will just print everything in the
6:48
So it actually prints the current even
6:50
number on the same line. That means the
6:53
entire output will be in the same line.
7:02
The first four even number is 2 4 6. You
7:05
can also give comma here. You can give
7:06
any digit that you want to end
7:16
something like this. I feel this is
7:19
easier. So that's why I gave this.
7:26
So it is giving the first six even
7:31
So this print so it actually prints the
7:34
current even number on the same line and
7:37
this will jump to the next even number
7:40
and this increments for every loop and
7:42
yeah that's it. A perfect combination of
7:45
loops and arithmetic to generate numbers
7:50
Awesome guys. You just learned how to
7:51
use a while loop to generate sequences
7:53
in Python. Loops like this save you from
7:56
writing repetitive code and makes your
7:59
program much smarter. And here is a
8:02
challenge for you. Try modifying this
8:04
program to print first n odd numbers
8:07
instead. And also comment below if you
8:10
can do it. And hey, if you're loving
8:12
these step-by-step exercises, make sure
8:14
to like, subscribe, and hit the bell
8:16
icon so you don't miss any future Python
8:18
lessons. Remember, every line of code
8:21
you write makes you a stronger and more
8:23
confident programmer. Keep coding, keep
8:25
experimenting, and I'll see you in the