0:00
Hey everyone, welcome back to Simplified
0:02
by Singum. Welcome to our Python basics
0:05
series. In the last few exercises, we
0:07
learned how to check if numbers are even
0:09
or positive or negative. Now, let's
0:12
bring this into a real world scenario
0:14
that every student can relate to. In
0:17
this exercise, we are going to create a
0:20
Python program to check whether a
0:22
student has passed the exam or not based
0:26
on their average score. Sounds cool,
0:28
right? Let's jump in.
0:31
So here is how the logic flows as step
0:34
by step. Imagine it's like a road map
0:36
for our code. So we start we take three
0:40
from the user and then we try to find
0:42
the average by adding all the three
0:45
numbers and dividing it by three and
0:47
then if the average is greater than 50
0:50
then we say that uh approved and if the
0:54
average is less than 50 then it is false
0:56
then obviously it will say that failed
0:59
and then we will end the program.
1:04
So we take three grades find the
1:07
average. If the average is greater than
1:09
50, the student passed otherwise the
1:15
Okay, let's write program
1:25
and we'll create three variables.
1:27
one is grade one and we'll get input.
1:40
And we need this in float because we are
1:42
doing mathematical calculations.
1:50
Similarly, we'll add grade one uh grade
1:55
Similarly, we'll add grade two and grade
2:03
This will be two and this will be three.
2:13
Okay, now it's time to calculate the
2:20
So, we already know the formula. All you
2:22
have to do is create a variable called
2:24
average and just add grade 1 + grade 2
2:34
and you're just going to divide it by
2:36
three. That's how we'll get the average.
2:43
And the reason I kept brackets here
2:45
because brackets decides the priority of
2:48
what operators like what calculation
2:52
should perform first and later it can go
2:54
to the next calculation. Obviously if
2:57
you observe if you go through our
2:59
operators tutorial there we have already
3:02
learned that the plus operator will have
3:05
less priority compared to the division
3:07
operator. But in order to give the plus
3:10
operator more priority, first we have to
3:13
apply the brackets. So even the computer
3:15
will understand that. So first I have to
3:18
add these three numbers and then I have
3:20
to divide it by three.
3:24
And now it's time to check the condition
3:33
if average. So what is the condition? If
3:37
average is greater than 50 then we say
3:39
the student has approved student passed
3:42
but if the average is less than 50 then
3:44
you will say the student failed. So you
3:47
just print using f strings the student
4:14
same thing but print you will say that
4:37
So let's break it down slowly so it
4:41
First we ask the user to enter three
4:44
grades and we use float
4:47
here instead of int because grades can
4:50
sometimes have decimals like 75.5. So
4:53
that is why using float is the best way
4:56
when you're dealing with the decimals.
4:58
And next we calculate the average with
5:01
grade 1, grade 2, grade three by three.
5:04
And finally we can use if else condition
5:06
here. If average is greater than 50 then
5:10
you say the student passed the exam else
5:12
the student failed the exam. That's it.
5:15
Just a few lines of code and we have got
5:17
ourselves a working grading system.
5:20
Seriously this is a real world system
5:22
realtime application that most of them
5:24
are using in their systems. It's as
5:30
Let's try giving different cases.
5:36
So the first case will be giving the
5:38
students grade 170, grade 280 and grade
5:43
275. Oh my god, this student is so good
5:49
And let's see the average. The student
5:51
passed the exam and the average is 75.0.
5:54
Not bad. And let's try another case.
5:59
And this guy, he got 50, 45, and 40.
6:05
Okay. And the student failed the exam
6:08
and the average is 45.0. Okay. So far so
6:11
good. It's working. So perfect. And now
6:14
let's try one more case.
6:20
and 45 and 60. And the student passed
6:24
the exam. The average is 51. Oh my god,
6:29
it's a very big number. But let's give
6:32
let's round it to 2F.
6:40
2F to get two decimals.
6:52
Let's give same values 50 45 60. Sorry
6:56
guys, my throat is really bad today. I'm
6:59
sorry about that. But I'm trying my
7:06
The student passed the exam and the
7:08
average is 51.67. So this is what we
7:11
need. We want the decimals to be rounded
7:13
to two decimals. And for that we can use
7:16
2F every time. And don't forget to use
7:18
this colon here because that is the way
7:22
you can tell the computer that I want
7:23
this value to be rounded to two
7:28
And yeah, that's how you check if a
7:30
student has passed or failed using
7:32
Python. And the best part is you can use
7:35
this logic to match real exam rules like
7:38
for example changing the passing mark to
7:41
60. You can change the passing mark to
7:44
60. And you can even add more subjects.
7:48
And you can try writing the same program
7:50
but with five grades instead of three
7:53
and calculate the average and see if it
7:55
still works. And if you enjoyed this
7:57
explanation and it made things super
7:59
easy, then don't forget to subscribe so
8:01
you don't miss the upcoming exercises.
8:04
And also drop a like if this helped you
8:06
and share this with a friend who's just
8:08
starting their Python journey. And
8:10
remember, coding is like learning a new
8:12
language. Practice a little every day
8:15
and soon you'll be fluent. See you in