0:00
Hey everyone, welcome back to simplified
0:02
by Singum. Welcome to our Python series.
0:04
In the last exercise, we checked if a
0:06
number is positive, negative or zero. So
0:09
now in this exercise, we are going to
0:10
write a small but very useful program
0:13
that is to check whether a number is
0:15
even or odd. Trust me, this is one of
0:18
the most common beginner friendly coding
0:19
problems and even once you learn it, you
0:22
will see it pop up everywhere. So let's
0:26
So before coding let's quickly look at
0:30
the flowchart. Always remember a
0:32
flowchart is like your Google maps for
0:34
coding. It shows you the path clearly.
0:37
So here if you observe the flowchart
0:39
we'll start the program and then we'll
0:40
get the number and if the number mod 2
0:43
equal to 0 then we consider it as true
0:46
and if it is not zero then we consider
0:48
it as false. So if it is true we'll
0:50
print the number is even else the number
0:52
is odd and then we will end the program.
0:56
Simple, right? Let's code this now. So,
1:00
all you have to do is always
1:17
enter an integer. So, as we need
1:22
I'm going to keep it int.
1:31
checking the condition.
2:10
So let's slow down and explain every
2:13
part. First we take input from the user
2:17
and convert it into integer using int.
2:20
And then comes the magic which is the
2:22
modulus operator. So num mod 2 gives the
2:26
remainder when we divide the number by
2:28
two. And if the remainder is zero, it
2:31
means the number is perfectly divisible
2:34
by two. So it is even otherwise it's
2:37
odd. So how do you calculate whether a
2:40
number is even or not? Obviously in
2:42
mathematics if we consider a number is
2:45
true when it is divisible by zero. So
2:47
how do you check whether a number is
2:49
divisible by 0 when you get the
2:52
remainder zero then you say that the
2:54
number is divisible by 0. So how do you
2:56
check that? By using this modulus
2:59
operator. Always remember this is
3:01
different. This will give you the
3:03
dividend of the calculation and this
3:07
will give you the remainder of the
3:11
So if the remainder is zero then you
3:13
will say that the number is even. If the
3:15
remainder is not zero then obviously you
3:17
will say that the number is odd. And
3:20
that's it. Just a few lines of code to
3:23
solve a real world problem. Now let's
3:26
test it out with some examples.
3:36
So enter an integer. Let's give it six.
3:40
You say the number is even. Okay. And
3:43
what if the integer is 9?
3:48
You say the number is odd. It works
3:51
perfectly. And yeah, that's how you
3:53
check if a number is even or odd in
3:55
Python. And the best part, the same
3:58
logic is used in many real world
4:00
situations like for example deciding
4:03
turns in a game or scheduling tasks on
4:08
even days or simply checking whether a
4:10
seat number is odd or even. So my little
4:14
challenge for you is try this program
4:16
with different numbers and see the
4:18
results. And if this video made things
4:20
super clear for you, then do me a favor,
4:24
hit that subscribe button. So you can
4:26
continue this Python journey with me and
4:28
also smash the like button if you
4:30
learned something new today. Remember,
4:32
every simple program you write is a step
4:34
toward mastering coding. Keep
4:36
practicing. Stay curious.