0:00
Hey friends, welcome back to simplified
0:02
by Singum. And in the last exercise, we
0:05
learned how to find the largest of two
0:07
numbers. And today is exercise two. Here
0:10
we are going to solve a very common real
0:12
world problem which is checking if a
0:14
year is leap year or not. Sounds
0:17
interesting, right? Let's dive in.
0:20
So first let's understand the problem.
0:23
So actually a leap year is that special
0:26
year which comes once in every four
0:28
years. And why is it special? Because it
0:32
has 366 days instead of 365 days.
0:38
Like for example, if you observe the
0:40
year 2020, it was a leap year, but the
0:44
year 2022 was not a leap year. So your
0:48
job today is to write a program that
0:50
checks this whether a year is a leap
0:57
So before writing any code, let's break
0:59
the logic down using this beautiful
1:03
So this actually means it starts it will
1:07
get the year and if the year is
1:10
divisible by four then we are
1:12
considering it as a leap year because
1:14
360 because a leap year has 366 days.
1:19
So generally the actual logic in order
1:22
to find a leap year especially when
1:24
dealing with maths. So it is this logic
1:27
which is year mod 4 equal to0. That
1:30
means if the year is divisible by four
1:32
and you get the remainder zero then that
1:34
means it's a leap year. So if this
1:37
condition is true then you will print
1:39
leap year. If this condition is false
1:42
then you will say it is not a leap year
1:43
and you will end the program. So this is
1:46
what we are going to do. Okay. Now let's
1:49
convert our flowchart into Python code.
1:52
So now all you have to do is get user
1:59
and year equal to input
2:05
and you just have to do enter a year. So
2:08
we need the input in inteious because we
2:11
are going to perform mathematical
2:12
calculations. So use int to convert the
2:16
input from string to integer. And now
3:04
and that's it. You will write a
3:06
condition just like that. So what's
3:09
happening here? First we take input from
3:12
the user which is nothing but getting
3:14
year and then we use the modulus
3:20
that means the year is perfectly
3:22
divisible by four or not. So if it is
3:25
divisible by four then you will say it
3:27
is a leap year. If not it is not a leap
3:29
year. Simple rule simple code.
3:33
And if I run this program
3:43
2020 is a leap year. Okay.
3:52
is not a leap year. Perfect.
3:56
So that's how you check whether a year
3:58
is a leap year or not using Python. You
4:01
see how easy it becomes when we first
4:03
break the problem down using this
4:05
flowchart. That's the secret. Don't jump
4:09
straight into coding. Think first and
4:12
then code second. If this explanation
4:14
made your Python journey a little easier
4:16
today, then do me a small favor. Hit
4:19
that subscribe button and give this
4:20
video a like if you enjoyed the
4:22
step-by-step approach because together
4:25
we are building your confidence in
4:26
coding and one small exercise at a time.
4:30
Stay curious, keep practicing, and I'll
4:32
see you in the next exercise.