Python Tutorial 7: Understanding Compound Conditionals in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is paul mcquarter from toptechboy.com and we're here today with lesson number seven in our incredible new tutorial series where you're gonna learn python or you're gonna die trying what i'm going to need you to do is pour yourself a nice strong cup of coffee that would be strong black coffee no sweeteners no sugar no cream none needed and i'm going to need you to fire up your visual studio code and get ready to learn some cool new stuff now as you're getting out your visual studio code as always i want to give a shout out to you guys who are helping me out over at patreon it is your support and your encouragement that keeps this great content coming you guys that are not helping out yet look in the description down below there is a link over to my patreon account think about hopping on over there and hooking a brother up but enough of this shameless self-promotion let's jump in and let's talk about what we're going to learn today now by way of review what we learned last week in the most excellent lesson number six you wrote you learned how to write if statements in python but those if statements had conditionals and the conditionals were pretty simple like if your number is less than seven if your number is greater than seven if your number is equal to seven so it was sort of like the if statement was based on a conditional with one condition what we're going to look at today is we're going to be looking at if statements with compound conditionals and compound conditionals is where you have more than one condition now compound conditions they have more than one condition and those different conditions are separated by either the word and or separated by the word or and so we have to understand logically the difference between the word and in the word or because there's kind of like a big difference and so let's think about what would be true and what wouldn't be true when we're using the difference between the word and in the word or so just for your information i am a male and i am 59 years old so let's think about this conditional what if i said stand up if you are a man and you are over 50 years old well i would stand up because both of those are true i'm a man and i'm over 50 years old and so since both of those are true i would stand up but let's say you had a man and he was 40 years old yes he's a man but he's not over 50 so he would not stand up or let's say that you had a 60 year old woman she's over 60 but she's not a man so she would not stand up okay so the only way that you would stand up is if both conditions are true that is and but now instead of that let's say let's imagine that i had the conditional instead of using the word and i had the conditional word or that means they don't both have to be true only one of them has to be true so if i said if you are a man or you're old older than 50 stand up well a 60 year old woman would stand up because she got the first conditional she or the second one she's over 50 okay how about a man that's 45 well he would stand up because even though he's not over 50 he is a man he hit one of them he would stand up let's say that you are a woman that is 25 years old well you couldn't stand up because you missed on being a man and you missed on being over 50 and so you have to stay sitting down and so the word or is much more inclusive if any of the conditions are true you can stand up and is very restrictive all the conditions have to be true in order for you to stand up and so that's the difference between and and or now i want to kind of show you what the the importance of these conditionals are and so i will come over here and i will just do some simple examples for you so we have our most excellent visual studio code up here and we are still working in our python files and i'm going to come over here and i'm going to create a new file and i'm going to call that com for compound conditional dot py the dot p y is kind of important and i get a fresh new uh blank program coming up here and so let's just do a real simple if statement kind of like what we were doing last week so i'm just going to kind of show that i'm going to say my cup my color is equal to input and then i want the user to please let's always be polite let's always say please please input your color and then a colon and a space just so that it's nicely formatted i should now have the color and then i'm going to say uh if and i'm going to say if my color equal equal don't forget the double equal if my color equal red alright if my color equal red then what do i do i print and i'm going to print your color is red very nice and now that's the only thing that i want in that if statement so i eliminate the tab and that way this will be the first statement after the if clause and it's going to be if my color is not the exclamation not equal to red then i'm going to print your your color is not red like that that looks pretty good let's see if we can run this thing and let's see what happens come up here okay and then do you remember ah you cannot quite see that can you better do a little windows management for you here that looks pretty good always click here because if you don't like if i'm up here and it asks me the question and then i type it doesn't put it down here it edits the code and that's a little annoying so you always need to click here to make sure that you're active please enter your color red and then it says your color is red wow okay let's play again come up here and i'm going to say blue and then when i say blue it says boom your color is not red all right now that's what you knew last week but now i want you to watch this okay so i come down here and i click now i want you to watch very very carefully all right i'm going to say red okay i'm going to say red what's going to happen when i press enter leave a comment down below what's going to happen when i press enter it says your color is not red okay somebody tell me what the problem is i typed in red i typed in red here and then it came back and said your color is not red the problem is the if statements are case sensitive so little red is completely different than up uppercase red uppercase r and lowercase r are tote two totally different characters and so you see the program is doing what i told it to it's not doing what i wanted it to and the programs always do what you tell them to do they don't do what you want them to do and so really to make this work what we need to do is we need to use a compound conditional and so what i want is i want it to print red not just not just for the case uppercase red but what else also little case red so what i could come up here and i could say if my color is equal equal to capital r e d and then or and you've got to put it in again you can't say equal equal again like that you have to type in your variable again or my color equal equal lowercase red and we better just for good measure we might also put or my color or my color equal equal just in case somebody had caps locked on like that okay if my color equal equal red or my color equal equal red or my color equal equal uppercase red any of those conditions any one of those conditions will put me into this line of code now we got to kind of think through this here now if my color is not red now what if i put or my color is not equal to not equal to r uh red now the problem is it would always come into this because it's either not going to be big red or it's not going to be little red so it would always come down here and print your color is not red so we need all of these things to be true and what do we use if we want them all to be true we would use the word and so all of these conditions need to be true in order to get into this print statement so all these things it's got to be not capital red but besides that it also has to not be little red and besides that it has to my color not equal to red now do you see how this works all three of these things have to be true in order for it to say your color is not red because if any one of these is true then it would be in this one so i want you to really think about this because you need to understand the difference between the word and and the word or all right i think we ought to go ahead and run this see what happens right okay let's come up here and let's hit the play button i missed it all right here we go please input your color i am going to zoom in on this a little bit all right we're going to type in re i i always forget to do what i warn you about you've got to click here to make sure that's active so i'm going to say red your color is red all right let's come in play again what if i say capital r e d boom your color is red what if i typed in big r e d boom your color is red and notice it never got into it never inappropriately got into this line of code so the logic is working now let's try a different one if i come in and i say please input your color and i say blue it says your color is not red let's play again what if i came in and said b l u i u e boom your color is not red if i come in and i say big r e d it says your color is red all right so do you understand now the difference between the word or the difference between the word or and the word and and so make sure that you really really understand this okay i'm going to give you a couple of homework assignments in this lesson and the first one i'm going to give you the solution to in this lesson and then the next one i'm going to make you uh you know i'm going to really not give you the solution until next week because man you guys are not going to learn to code if you just sit and you type lines in as i type them in you've got to go away and you've got to struggle and you've got to bang your head against the wall and you got to think through it and you got to get stuck and then you have to achieve the breakthrough if you just don't do anything but just type in code as i'm typing it in you're never going to learn to program so i'm going to give you an assignment and then i need you to pause the video and then i need you to go off and do the assignment and then after you've done it then you can watch me do it and see if you did it the way that i did it okay i need you to ask the user i need you to input a number from the user okay and then i need you to tell the user if his number is between five and ten okay and i you know when i say between five and ten that is somewhat a little bit logically ambiguous and so i'll be very specific if if the number is between 5 and 10 including 5 and 10 tell them that the number is between 5 and 10. if the number is not between 5 and 10 tell them your number is not between 5 and 10. so you guys are going to do this on your own and so what i need you to do is i need you to pause the video go away and do it and then come back and look at my solution so 1 2 3 pause okay guys did you come back how many of you were able to do this program were you able to do it how many of you ch how many of you folded up like a cheap walmart lawn chair fold it up like a cheap launcher i hope you guys got it because you really know enough to do this and so let's come on up here and let's see if we can do this so i'm just going to kind of back over this code and start so i'm going to say my number is equal to now i'm inputting a number what did i better do i better float it so i'm going to say float because when we input it inputs is as a string so i'm going to float the input and then i've got the prompt please always be polite say please please input your number number and then i put a colon in a space just because it formats it nicely that way it has already closed the print it's closed the apostrophe it's closed parentheses close the parentheses so i have one two open parenthesis i have one two close parenthesis i have one open string and i have one closed string all right now this is what i would do i would say if and then my number is greater than or equal to because i want it to include five greater than or equal to five and my number is less than or equal to 10 then what i want to say is print your number is between 5 and 10. in fact i think i'm going to say congratulations your number is between 5 and 10. then what i'm going to do is i'm going to put a separate if statement if my number is less than 5 if my number is less than 5 what is the word here or my my number is greater than 10. okay now why did we use the word oh man i did something very bad there i gotta kill this down here somehow hit run unknown how don't know how man hopefully that'll work i kind of i kind of hit run down here without intending it but hopefully to work okay so why did we use the word or because you got to think or let me go ahead and finish that this out i'll say print sorry your number is not between 5 and 10. all right now we got to understand why did i i think it makes sense why we used and up here because only if it's between 5 and 10 do you want to print this out which means both of these have to be true it needs to be both bigger than 5 and it needs to be less than 10. but then down here we had to switch to or why well let's think of the number four the number four it's not between five and ten so we need to say it's not between five and ten that's all it's gotta be is just less than you know it's all it's got to be is four and we want to print this out but also if it's greater than 10 it doesn't need to be less than 5 and greater than 10 because nothing is less than 5 and greater than 10. it's if either of these is true then it is not between five and ten does that make sense okay let's try this and let's see what happens so we're gonna come up here and i'm gonna run this thing and luckily this came back to life down here please then put your number four sorry your number is not between five and ten okay sorry your number is not between five and ten all right so let's try it again let's play again and let's say the number 11 okay sorry your number is not between 5 and 10 again very good let's run it again what if i put the number seven shazam your number is between five and ten okay guys this is really kind of simple but you would be surprised how uh how much that people struggle with these compound if statements okay so let me uh let me try another one you know again you guys always have got it you know the one of the fundamental flaws of almost all programmers is they don't check their code out enough you've really got to check things so we've gotten an example greater than 5 i mean greater than 10 less than 5 then one between 5 and 10. what do we really need to check we need to kind of check those end points right and the end points would be we better check 5 itself because i said 5 should be counted as between 5 and 10. so i need to put in 5. boom your number is between 5 and 10. what else do i need to check i need to check 10 there you go boom congratulations your number is between 5 and 10. all right so that is working really really good so i think that is pretty cool now this is what your assignment is for next week and i'm going to end the video here but i'm going to give your homework assignment and then we'll go through the solution next week i want you to ask the user to input a number and then when he inputs the number i want you to tell him your number is positive and even or your number is an even positive number your number is an odd positive number your number is an even negative number your number is an even odd let's try it again your number is positive even your number is positive odd your number is negative even your number is negative odd and then just because people could argue about whether zero is a positive or a negative number or people could argue that zero is is positive negative even odd we don't want to get into that and so if the person enters zero you don't want to get in that tar baby you don't want to get in that controversy so if the person puts in zero you just say your number is zero okay you don't do any of that other nonsense and so you've got five possibilities uh positive even positive odd negative even negative odd or zero and that is your assignment for next week guys i hope you're enjoying this i really have a lot of fun with python i really love visual studio code we are going to do some crazy insane stuff in this lesson but this is the series of lessons where no one gets left behind we're building the foundations we're building the fundamentals and then we're going to go on to do some really cool stuff we'll be hooking python up to arduino our python program will be interacting with the real world with the outside world through the arduino there's some really cool stuff coming but we don't want anyone to jump into some complicated stuff before they have learned the fundamentals so you guys do the homework and leave a comment down below whether you were able to do it on your own or not if you like this video give us a thumbs up that really helps us with the old youtube algorithm if you haven't subscribed already hit the subscribe button and then make sure you ring that bell so you'll get notifications when our future lessons come out and go ahead and share this with other people let's see if we can get more people writing code again this is paul mcquarter from toptechboy.com i will talk to you guys later
Info
Channel: Paul McWhorter
Views: 12,510
Rating: undefined out of 5
Keywords:
Id: E-c0w5Latbc
Channel Id: undefined
Length: 23min 4sec (1384 seconds)
Published: Wed Apr 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.