Conditionals |Section 3|Celestial Warrior

1 year ago
16

1

00:00:00,620 --> 00:00:05,720

Hey, hello again. In this lecture you're going to learn

about conditionals. What is a conditional?

3

00:00:07,980 --> 00:00:14,190

Well a conditional is a block of code which

performs different actions depending on

5

00:00:14,190 --> 00:00:19,980

whether a given conditional evaluates to

true or false so let's go ahead and

7

00:00:19,980 --> 00:00:29,340

create a conditional in Python. I'll create the

file conditionals dot py and let me

9

00:00:29,340 --> 00:00:35,280

create a conditional block, and first of

all let me create a variable. Let's say a

11

00:00:35,280 --> 00:00:40,920

equals five and here I'll create a

conditional block. This block, what this

13

00:00:40,920 --> 00:00:48,780

will do is it will perform an action if

a is less than five and another action

15

00:00:48,780 --> 00:00:54,930

if a is equal or greater than five

so let me create that. If this, our

17

00:00:54,930 --> 00:01:04,049

conditional starts if a and let's say

less than five print, let's say less than 5.

19

00:01:04,049 --> 00:01:15,030

Press ENTER and if you want to perform

more actions with this conditional, with

21

00:01:15,030 --> 00:01:19,979

this condition so when this condition is

true this will be executed but if you

23

00:01:19,979 --> 00:01:25,049

want to perform more actions you can add

one there, if not then you can go to the

25

00:01:25,049 --> 00:01:32,159

next check.

So that would be else, so you have a

27

00:01:32,159 --> 00:01:45,060

column here, a column there, so else print

equal or greater than 5. Now let me

29

00:01:45,060 --> 00:01:53,520

execute it. And we get equal or greater

than 5. So why did you get that? Well

31

00:01:53,520 --> 00:01:59,729

because you know if a is less than 5

which is not, so this evaluates to false,

33

00:01:59,729 --> 00:02:05,340

that means with this this will not be

executed so Python will skip this line

35

00:02:05,340 --> 00:02:12,870

and it will go to the next one. This next

line says else, so else means if this

37

00:02:12,870 --> 00:02:18,450

is not true,

this will be executed, so equal or

39

00:02:18,450 --> 00:02:25,769

greater than five, so in this case if a

is not less than five then there's no

41

00:02:25,769 --> 00:02:32,549

other option but a is either equal or

greater than five, so that's an if and else

43

00:02:32,549 --> 00:02:36,959

conditional block. However if you want to

add more conditions there

45

00:02:36,959 --> 00:02:45,569

you want to use elif, so let's do that

the first two lines stay the same. If a

47

00:02:45,569 --> 00:02:57,840

is less than five print less than five

elif a is equal to five print equal to

49

00:02:57,840 --> 00:03:19,019

five else print greater than five. Save

and execute and you get equal to five.

51

00:03:19,019 --> 00:03:21,769

If a was six,

you'd get greater than five and if a was

53

00:03:25,500 --> 00:03:34,500

three you'd get less than five.

That's a conditional block in Python and

55

00:03:34,500 --> 00:03:39,480

yeah, try to get familiar with the syntax

and in the next lecture I'm going to

57

00:03:39,480 --> 00:03:47,569

incorporate a conditional with this

block of code, so see you there.

Loading comments...