Python for Beginners, Part 19: Conditionals and Control Flow by@cleverprogrammer

Python for Beginners, Part 19: Conditionals and Control Flow

image
Clever Programmer HackerNoon profile picture

Clever Programmer

Clever Programmer is a community with over 100,000+ students who are learning to code by building real world projects.

Let's cover conditionals and control flow! This includes your Boolean logic combined with Boolean operators combined with if then type of statements!

In case you missed it, here are the previous parts of the series:

Transcript

0:00 hey guys what is going on my brain is

0:03 about to blow up I've been reading this

0:05 book called the little schemer and it's

0:09 about this programming language called

0:12 scheme and in this programming language

0:16 everything is done really difficult in a

0:20 very difficult way and I spent eight

0:23 hours studying it so my brain is like

0:25 fried but I'm excited to take a break

0:28 from studying it and get on with

0:31 teaching something about computer

0:33 programming in Python so we covered in

0:36 our last video Boolean's

0:39 and we talked more specifically covered

0:42 the Boolean logical operators we talked

0:44 about and and we talked about or and we

0:47 talked about not so another thing I

0:50 wanted to mention to you guys I had it

0:51 open here so I don't freak yet if you

0:53 guys are on my website you can write

0:55 code directly underneath while you're

0:57 watching the video so you can write

0:58 something like print 5 or whatever and

1:02 hit this and it'll run it and actually

1:04 give you the results so if you don't

1:06 know if you're having term if you're

1:08 having problems running your code or you

1:10 just want somewhere easy to run it well

1:12 you can go here and watch the videos and

1:14 run it at the same time back to where we

1:17 were okay so as you can see we covered a

1:21 lot of this stuff right with and or and

1:24 not so now let's do some examples where

1:27 we combine our conditionals control flow

1:30 with our Boolean logic okay so this is

1:35 what's gonna tie it all together this is

1:36 what's gonna allow you to make powerful

1:38 games and do powerful things right now

1:41 okay not in hundred more videos not

1:45 reading one whole another book you can

1:48 start to build games and functional

1:50 programs doing what I'm about to show

1:52 you okay so let's get to it so you saw I

1:57 showed you okay you have Boolean

1:59 operators like you true and false and

2:01 you get some result

2:02 now I also showed you how you can have

2:05 some examples like Johnny homework and

2:08 this and then this code runs here okay

2:12 I showed you that at the top so let me

2:14 just bring that down whoops the whole

2:19 example this is not real code this is

2:21 just you know it is going to give you

2:24 wrong answers for now so now let's try

2:27 to do those things where if you had an

2:29 employee those getting paid over 40

2:31 hours and you want her to give him

2:33 overtime pay again let's tap it all in

2:37 here and let's see how that would work

2:38 so if I did Bob hours work and I said

2:44 okay he work let's say 40 hours and if I

2:48 said if Bob hours greater than 39 then

2:52 print

2:53 he worked overtime for some reason we

2:56 have less than 40 hours as overtime but

3:01 whatever never mind that he worked

3:04 overtime so let's see if that prints out

3:06 if it does print out then that means he

3:07 worked overtime as and as you can see

3:10 Bob hours worked for 44 T is greater

3:14 than 39 so that part evaluated to true

3:17 and then you got back the statement now

3:20 what if we wanted to make it a little

3:21 bit more interesting let's go back to

3:24 the example of the kid where he throws

3:26 the garbage and he does his homework

3:30 right so for example let's say Johnny

3:33 homework aha is equal to true meaning he

3:39 completed his homework assignment now

3:42 let's say I Johnny whoops

3:46 Johnny throw out garbage and let's make

3:51 that also true so he's done both of the

3:54 things now let's see if he gets to play

3:56 a a video game okay let's let's see if

3:59 he gets to play his Xbox 360 so if

4:02 Johnny homework right dad says if Johnny

4:06 does his homework and Johnny throws out

4:10 the garbage whoops I'm sorry then

4:15 um Johnny can play Xbox 360 right so if

4:27 that statement points out he can play

4:29 Xbox 360 and I don't know why I'm

4:30 running Xbox 360 I feel like I'm living

4:33 in 2005 Xbox one has been out and I'm

4:36 pretty sure in the next few years I'm

4:37 gonna bring out the new one so let's

4:38 change this Xbox one and let's hit enter

4:41 so it printed it out Johnny can play

4:45 Xbox one how did this work Johnny

4:48 homework evaluated to a true statement

4:50 Johnny throw out garbage evaluator true

4:53 statement the only time you get back a

4:55 true when you combine something with an

4:57 end is when both of the statements are

5:00 true so true true and we got back that

5:03 right now just for some little exercises

5:07 here and there what if I did something

5:09 like not Johnny homework oops

5:14 Johnny homework or Johnny Johnny throw

5:22 out garbage right what do you think I

5:31 would get here as a result hmm let's

5:34 think about it so if you guessed it

5:37 correctly good pause the video or let's

5:39 try to figure out together so not throw

5:42 out garbage not true right throughout

5:44 garbage sure not true gives you false

5:46 and this part is true so you essentially

5:51 get true or true and then not true or

5:56 true gives you back true because for an

5:58 or condition to value to true only one

6:00 of the conditions have to be true and

6:02 then calling not on a true statement

6:04 gives you Back false and hence we get a

6:06 false okay again I'm not going to focus

6:09 so much on these contrived

6:11 force Boolean examples because your

6:14 thing is never going to get like that

6:15 and but it's just good to know how it

6:18 works right it works through the inner

6:21 parentheses inside of the parentheses

6:23 first and then it goes outside of the

6:25 parentheses and starts to evaluate

6:26 things

6:27 okay so you can see we were able to put

6:31 things together now imagine if you were

6:33 making a game of rock-paper-scissors

6:36 right let's rock paper scissors right I

6:42 can say if human picked let's say rock

6:46 and computer picked scissors now I can

6:53 say if now I can say let's see what what

6:56 can I say I can say something like if

7:00 human equal equal Rock and computer

7:05 picked scissors then who wins right who

7:09 gets the point here so I am going to say

7:12 that human score is equal to 1 okay

7:18 so human picked Rock computer pick

7:21 scissors right in rock paper scissor

7:24 just in case you didn't know rock beats

7:25 scissors and now let's check the human

7:29 score and you can see that when I got

7:32 back of one okay so that's why if

7:35 conditions are so important we can also

7:38 start to tack them on okay so let's say

7:42 that let's say if human picked Rock and

7:47 computer pick scissors if this was not

7:50 the case what if there was some other

7:53 case let's say computer pick something

7:55 else so let's say computer is bananas

7:58 right so if I write this line of code

8:01 how can I check for other things so I

8:04 can say oops I can say else if oh I got

8:09 to get outside of the if statement I can

8:11 say else if computer sorry human picked

8:18 Rock and computer picked

8:26 bananas then you can say I don't know

8:31 computer is equal to computer score is

8:35 equal to zero and human score is equal

8:39 to zero because bananas is not really a

8:42 valid thing so you get back both got

8:44 zero and then it says something like

8:47 prints

8:49 you can't pick herbs

8:52 you can't pick anything other than rock

8:58 paper or scissors right something like

9:03 that and let's hit it

9:04 and so you can see that first it check

9:07 this if condition it checked if human is

9:10 equal to rock while human was equal to

9:12 rock the only way this statement will

9:15 run if both of the statements are true

9:17 that's what the end forces you to do if

9:20 we had an order here what it would do is

9:22 it says if human is rocking computer is

9:25 scissors okay

9:27 so in our case it would go as human

9:29 equal to rock it would say true and then

9:32 would go or computer equal scissors it

9:35 would evaluate that to false because

9:37 bananas bananas right there does not

9:41 equal scissors so it would go true or

9:44 false it would evaluate that as true

9:46 because true or false is true and it

9:49 would go okay human score is equal to

9:51 one okay but we don't want that we want

9:56 both of the things to be true if human

9:58 picks rock and the computer pick

10:00 scissors at the same time that's when

10:02 the human wins but in this case computer

10:05 picked bananas and we said if you pick

10:07 bananas as you know if that happened

10:10 where human picked Rock and computer

10:11 picked bananas we can say that you know

10:15 you can't pick that you got to try again

10:18 now why our statement is useful as

10:20 imagine if I mean do you care who picks

10:24 bananas do care if computer picks

10:26 bananas or human picks bananas should

10:30 you stop the game if either of those

10:31 people pick bananas yeah you should

10:34 right you should stop the game and start

10:37 it again

10:37 because

10:38 Anna should not be one of the options so

10:40 you don't care who picked something

10:42 that's not the wrong one you just care

10:45 that wrong one is picked that's why you

10:48 would use or you would say if human

10:52 equal equal bananas or computer equal

10:58 equal bananas then print blah blah blah

11:03 blah blah

11:04 right in this case it won't make sense

11:05 but then whatever right we print out

11:09 this line of code elsif notice the else

11:12 if it's used to connect these if you

11:15 want to actually write it as i can show

11:21 you that later but if you essentially

11:22 want to write it you know what you have

11:26 to do and you're writing it where these

11:27 three dots aren't there right your three

11:30 dots that are showing up here so let me

11:32 bring down this if-then whatever

11:34 statement down for you so if you ever

11:37 have the case where these three dots are

11:40 three these three greater than signs are

11:42 there then you have to put the else if

11:46 not at the same indentation level as an

11:49 if but if you're doing it and you're

11:51 writing this code as a script then

11:54 that's how would be the if and else if

11:57 or at the same indentation level okay

12:00 they're not at a different indentation

12:02 level that's very important otherwise

12:03 the code is not going to run okay an

12:05 else--if helps you join an F with an

12:08 else if statement and another one that

12:11 you might need is else and else does not

12:12 care for a condition okay you see your

12:15 if statement cares for a condition and

12:17 then it runs the code your else if

12:20 statement cares for a condition and if

12:23 that condition is satisfied it runs the

12:25 code else does not take in condition if

12:27 none of these guys match then else runs

12:30 by default okay so if

12:37 condition all right if this condition

12:41 evaluates to true then then this block

12:49 of code runs okay good

12:57 again or we don't care who did the thing

13:01 we don't care who committed the crime if

13:03 any one of them did we just break it

13:06 realistically when you have this case

13:08 where a human is bananas or I mean

13:11 you're not going to put every single

13:12 option ever that could be wrong right so

13:17 you can't check for bananas or spaghetti

13:19 or pizza what you can check for is human

13:22 did not pick if one of them did not pick

13:27 let's say rock papers or scissors then

13:31 it should be wrong right so let's see if

13:34 we can write that if computer does not

13:37 equal right we can say a rock or

13:43 computer does not equal scissors or

13:48 computer does not equal paper then print

13:54 wrong choice pick again okay

14:01 so now let's try it now let's say

14:03 computer is equal to rock and let's run

14:07 that piece of code and it says wrong

14:11 choice

14:12 pick again okay so you can see how using

14:16 the or statement here ended up giving me

14:19 the wrong result what I want to make

14:20 sure is that the computer is equal to

14:25 any one of these right so let's try it

14:27 like this let's say computer is not

14:30 equal rock

14:32 and computer does not equal scissors

14:37 right and computer does not equal paper

14:46 right let's let me let me just go a

14:49 little bit here make it bigger then

14:52 print wrong choice pick again okay so

14:59 the only way this code will run is if

15:01 all of these things are true meaning

15:06 computer is not wrong computers not

15:07 scissors and computers not paper right

15:10 so if I made computer is equal to banana

15:13 and I went up here and I ran this code

15:17 you can see that it checked his computer

15:20 rock his computer scissors its computer

15:22 paper if it's not any of these then

15:24 print wrong choice okay so you can see

15:27 how powerful these Combinator's are is

15:31 that what I would call him like you're a

15:33 logical operator boolean operators right

15:36 like and and or you really really need

15:39 them to be able to do this stuff okay to

15:42 be able to do really powerful things ok

15:45 I'm going to cut this video off here

15:47 hopefully you have a much better

15:49 understanding of how this stuff works ok

15:52 in the next video we will cover for

15:55 loops and I will see you there.

Comments

Signup or Login to Join the Discussion

Tags

Related Stories