0:00
Hey everyone, welcome back to the
0:01
channel. If you're new here, I am your
0:04
Lucky and I simplify programming for
0:06
beginners like you. And in today's
0:08
episode of module 3, we are going to
0:11
work on conditions in Python. We are
0:13
diving into something super important,
0:16
which is the if, LF, and else decision
0:19
structure. In the previous videos, we
0:22
worked on if and else conditions. If you
0:25
guys missed that episode, I'm going to
0:26
keep that in the description. You can
0:28
just go there and try to grab it. If you
0:31
still have any doubts, you can comment
0:32
down and you can start learning from
0:35
here once you complete that episode. So,
0:38
we are going to work on if, lif, and
0:41
else structure. Now, don't worry if that
0:44
sounds like a lot. We'll take it slow,
0:47
break it down with real examples, and by
0:50
the end of this video, I promise that it
0:53
will click for you. And let's go.
0:57
All right. So, we have already seen how
1:02
if and else exactly works and how they
1:05
help our code to make simple decisions,
1:09
right? Like it could be choosing between
1:11
two options. But what if you have more
1:15
than two conditions to check? Let's say
1:17
you're grading the students and based on
1:20
marks and you want to assign grades A,
1:26
C or maybe even an F. I don't want you
1:29
to do that but yeah maybe it happens. So
1:32
that is where we can use if L if else.
1:37
So it lets you to check multiple
1:40
conditions one after the other.
1:45
Like for example, if this condition is
1:47
true, then you can execute it. If this
1:51
then the interpreter goes here and if it
1:53
is true, then it will execute. If this
1:56
is also false, so anything of them is
1:58
not working out. So then it will go to
2:00
the else block and it will just print
2:02
whatever which is present in the else
2:06
So let's I'm going to explain it once
2:09
again. Let's understand what's happening
2:11
here. So first Python checks condition
2:14
one. If that's true, then it runs this
2:17
block of code and skips the rest of
2:20
them. If not, it checks condition two.
2:24
If this is true, then it runs this
2:30
Still the condition is false, then it
2:32
goes it goes and check condition three.
2:36
And finally, it will print this block.
2:39
If none of those conditions are true,
2:41
then it will go to else block and then
2:43
it will print this line. It's pretty
2:47
neat. Python goes step by step until
2:52
something matches. If nothing matches,
2:55
then it will execute this block.
3:00
So let's bring this to life with an
3:06
equal to 75. So this is the marks that a
3:10
student got. Okay, just imagine if marks
3:14
how do you give grades to students? So
3:17
if marks is greater than or equal to 90
3:21
then you will give a grade to that
3:24
person right to that student.
3:39
greater than or equal to 80
3:43
then you are going to give that person
4:03
my bad I'm typing something or the
4:14
greater than or equal to 70?
4:18
Then you'll just print
4:40
What if the person got 60?
5:03
He got a D. He or she got a D-grade. So
5:07
what if the person is less than 60 which
5:09
is none of these things? Then else
5:26
So here's what we have got. So we have
5:29
got marks variable which is 75 and we
5:32
are we just assigned 75 grade
5:36
and now so if the marks are 75 then
5:40
Python will first check whether is it 90
5:43
or more based on score. So it is not 90.
5:47
So next it goes 80 or more. Still no
5:53
75 and 80 is completely different. And
5:56
now it goes here. Marks greater than or
5:59
equal to 70. Yes. So it prints you got C
6:06
So notice how it stops once a condition
6:09
is met. That's the beauty of if l if
6:15
Like think of it like a staircase. like
6:19
Python walks down step by step until it
6:23
finds the right one and it's like yes
6:26
this is what I need and then it will
6:28
leave the rest and it will just print
6:30
the output just for you. If you want you
6:32
can try it right away.
6:38
So you got a C. So that's the beauty of
6:41
it. So if you just skip 90
6:48
So if it's 90 then it will check this
6:51
line itself and then it will check this
6:53
one and then it will just print it
6:55
because it met the condition right
6:58
that's why if you got 50
7:04
I failed for sure see sorry you failed
7:07
so it will go it will check whether 50
7:10
and 90 are same No. Greater than 90? No.
7:14
Greater than or equal to 80? No. Greater
7:16
than or equal to 70? No. Greater than or
7:18
equal to 60? No. Then it will simply
7:21
print this else statement.
7:24
So this is what if, l if and else
7:31
So this is the example that we have
7:32
worked on. So it will check the grade
7:35
value top to bottom, step by step. If it
7:38
meets the condition then it will just
7:39
print and skips the rest of the part of
7:44
So that's how if L if and else works. So
7:48
I just want you to give some quick tips
7:50
when using if L if and else. First tip
7:53
is you can have as many L ifs as you
7:56
want. It depends on the situation and
7:59
depends on the conditions that you want
8:00
to give. And the else block is optional
8:04
but it will be super handy as a you know
8:07
fall back like it is optional. You don't
8:09
have to give this. If you don't give
8:12
this what happens let's see.
8:18
So it will just print nothing I guess.
8:22
See it is not printing anything.
8:30
See it is not printing anything. So
8:34
but if you want to show something to the
8:36
user that there's something wrong even
8:38
in the data that the user given. So then
8:41
you can use this else. I would suggest
8:43
you to use else but it is not necessary
8:46
to do so. Also keep that in mind that
8:48
conditions are checked in order like
8:50
from top to bottom. First this then this
8:53
then this then this and this. If any one
8:56
of its it met then obviously it will
9:00
skip the rest of the code and it will
9:02
print the output that you're expecting.
9:05
So that's how if else works in Python. A
9:11
powerful way to guide your program
9:13
through multiple choices. In the next
9:15
part we'll talk about nested if
9:17
statements. That's when you know one
9:20
condition is inside another. like it it
9:23
is very useful for more complex logic.
9:25
If this helped you to learn something
9:27
new and interesting, give it a thumbs
9:29
up. Subscribe for more beginner friendly
9:31
Python lessons and also drop a comment.
9:34
If you have any questions or want me to
9:36
cover something specific, just comment
9:38
that down. I will make video on that one
9:41
as well. See you in the next one.