Learn Python Programming - 19 - Conditionals and Control Flow

**The Importance of Logical Operators in Programming**

In programming, logical operators are used to make decisions based on conditions. One such operator is "or" which is represented by the symbol '|'. In our case, it would go as human equal to rock, it would say true and then would go or computer equal scissors, it would evaluate that to false because bananas do not equal scissors.

The "or" operator allows us to create conditions that are not mutually exclusive. We can use this operator to check if a certain condition is met, and if not, we can take an alternative action. For example, in the game of rock-paper-scissors, we want to determine who wins. If both humans and computers pick rock at the same time, it's a tie. However, if one picks rock and the other does not, the person who picked rock wins.

**Understanding Conditional Statements**

In programming, conditional statements are used to make decisions based on conditions. In our case, we want to create a statement that checks if human equals either of two options: rock or scissors. If both humans and computers pick one of these options, it's a win for the human. However, if neither of them pick rock or scissors, the game should start again.

To achieve this, we can use an "if-else" statement. The statement checks if human equals either rock or scissors, and if so, prints out that the human has won. If not, it moves on to the next condition. However, in our case, we want to use the "or" operator because we don't care who picks what as long as one of them does.

**The Role of Else Statements**

In programming, an "else" statement is used when a condition is not met by the previous conditions. In our case, we have two conditions: if human equals either rock or scissors and if neither human nor computer pick any of these options. If neither of these conditions are met, the game should start again.

Using an "else" statement allows us to specify what action to take when none of the previous conditions are met. In our case, it prints out that the choice is wrong and asks the user to pick again. This ensures that the game continues until a valid move is made.

**Power of Combinator Logical Operators**

In programming, logical operators such as "and" (represented by the symbol '&') and "or" (represented by the symbol '|') are used to make decisions based on conditions. The power of these operators lies in their ability to combine multiple conditions into a single statement.

For example, we can use an "if-else" statement with the "or" operator to check if human equals either rock or scissors and print out that the human has won. We can also use another condition to check if neither human nor computer pick any of these options and print out that the choice is wrong.

**Real-World Applications**

In real-world applications, logical operators such as "and", "or", and "not" are used extensively in programming languages such as Python, Java, and C++. These operators allow us to make decisions based on conditions and perform complex operations with ease.

For instance, in a game of rock-paper-scissors, we can use the "or" operator to check if either human or computer pick a valid move. If not, the game should start again. We can also use an "if-else" statement to determine who wins based on the moves made by both humans and computers.

**Conclusion**

In conclusion, logical operators such as "and", "or", and "not" play a crucial role in programming. They allow us to make decisions based on conditions and perform complex operations with ease. In our example of rock-paper-scissors, we used the "or" operator to combine multiple conditions into a single statement.

By mastering these logical operators, developers can create complex programs that interact with users and make decisions based on input. Whether it's a game of rock-paper-scissors or a complex algorithm, logical operators are essential tools in any programmer's toolbox.

"WEBVTTKind: captionsLanguage: enhey guys what is going on my brain is about to blow up I've been reading this book called the little schemer and it's about this programming language called scheme and in this programming language everything is done really difficult in a very difficult way and I spent eight hours studying it so my brain is like fried but I'm excited to take a break from studying it and get on with teaching something about computer programming in Python so we covered in our last video boolean's and we talked more specifically covered the boolean logical operators we talked about and and we talked about or and we talked about not so another thing I wanted to mention to you guys I had it open here so I don't freak yet if you guys are on my website you can write code directly underneath while you're watching the video so you can write something like print 5 or whatever and hit this and it'll run it and actually give you the results so if you don't know if you're having term if you're having problems running your code or you just want somewhere easy to run it well you can go here and watch the videos and run it at the same time back to where we were okay so as you can see we covered a lot of this stuff right with and or and not so now let's do some examples where we combine our conditionals control flow with our boolean logic okay so this is what's gonna tie it all together this is what's gonna allow you to make powerful games and do powerful things right now okay not in hundred more videos not reading one whole another book you can start to build games and functional programs doing what I'm about to show you okay so let's get to it so you saw I showed you okay you have boolean operators like you true and false and you get some result now I also showed you how you can have some examples like Johnny homework and this and then this code runs here okay I showed you that at the top so let me just bring that down whoops the whole example this is not real code this is just you know it is going to give you wrong answers for now so now let's try to do those things where if you had an employee those getting paid over 40 hours and you want her to give him overtime pay again let's tap it all in here and let's see how that would work so if I did Bob hours work and I said okay he work let's say 40 hours and if I said if Bob hours greater than 39 then print he worked overtime for some reason we have less than 40 hours as overtime but whatever never mind that he worked overtime so let's see if that prints out if it does print out then that means he worked overtime as and as you can see Bob hours worked for 44 T is greater than 39 so that part evaluated to true and then you got back the statement now what if we wanted to make it a little bit more interesting let's go back to the example of the kid where he throws the garbage and he does his homework right so for example let's say Johnny homework aha is equal to true meaning he completed his homework assignment now let's say I Johnny whoops Johnny throw out garbage and let's make that also true so he's done both of the things now let's see if he gets to play a a video game okay let's let's see if he gets to play his Xbox 360 so if Johnny homework right dad says if Johnny does his homework and Johnny throws out the garbage whoops I'm sorry then um Johnny can play xbox 360 right so if that statement points out he can play xbox 360 and I don't know why I'm running Xbox 360 I feel like I'm living in 2005 Xbox one has been out and I'm pretty sure in the next few years I'm gonna bring out the new one so let's change this Xbox one and let's hit enter so it printed it out Johnny can play xbox one how did this work Johnny homework evaluated to a true statement Johnny throw out garbage evaluator true statement the only time you get back a true when you combine something with an end is when both of the statements are true so true true and we got back that right now just for some little exercises here and there what if I did something like not Johnny homework oops Johnny homework or Johnny Johnny throw out garbage right what do you think I would get here as a result hmm let's think about it so if you guessed it correctly good pause the video or let's try to figure out together so not throw out garbage not true right throughout garbage sure not true gives you false and this part is true so you essentially get true or true and then not true or true gives you back true because for an or condition to value to true only one of the conditions have to be true and then calling not on a true statement gives you Backa false and hence we get a false okay again I'm not going to focus so much on these contrived force boolean examples because your thing is never going to get like that and but it's just good to know how it works right it works through the inner parentheses inside of the parentheses first and then it goes outside of the parentheses and starts to evaluate things okay so you can see we were able to put things together now imagine if you were making a game of rock-paper-scissors right let's rock paper scissors right I can say if human picked let's say rock and computer picked scissors now I can say if now I can say let's see what what can I say I can say something like if human equal equal Rock and computer picked scissors then who wins right who gets the point here so I am going to say that human score is equal to 1 okay so human picked Rock computer pick scissors right in rock paper scissor just in case you didn't know rock beats scissors and now let's check the human score and you can see that when I got back of one okay so that's why if conditions are so important we can also start to tack them on okay so let's say that let's say if human picked Rock and computer pick scissors if this was not the case what if there was some other case let's say computer pick something else so let's say computer is bananas right so if I write this line of code how can I check for other things so I can say oops I can say else if oh I got to get outside of the if statement I can say else if computer sorry human picked Rock and computer picked bananas then you can say I don't know computer is equal to computer score is equal to zero and human score is equal to zero because bananas is not really a valid thing so you get back both got zero and then it says something like prints you can't pick herbs you can't pick anything other than rock paper or scissors right something like that and let's hit it and so you can see that first it check this if condition it checked if human is equal to rock while human was equal to rock the only way this statement will run if both of the statements are true that's what the end forces you to do if we had an order here what it would do is it says if human is rocking computer is scissors okay so in our case it would go as human equal to rock it would say true and then would go or computer equal scissors it would evaluate that to false because bananas bananas right there does not equal scissors so it would go true or false it would evaluate that as true because true or false is true and it would go okay human score is equal to one okay but we don't want that we want both of the things to be true if human picks rock and the computer pick scissors at the same time that's when the human wins but in this case computer picked bananas and we said if you pick bananas as you know if that happened where human picked Rock and computer picked bananas we can say that you know you can't pick that you got to try again now why our statement is useful as imagine if I mean do you care who picks bananas do care if computer picks bananas or human picks bananas should you stop the game if either of those people pick bananas yeah you should right you should stop the game and start it again because Anna should not be one of the options so you don't care who picked something that's not the wrong one you just care that wrong one is picked that's why you would use or you would say if human equal equal bananas or computer equal equal bananas then print blah blah blah blah blah right in this case it won't make sense but then whatever right we print out this line of code elsif notice the else if it's used to connect these if you want to actually write it as i can show you that later but if you essentially want to write it you know what you have to do and you're writing it where these three dots aren't there right your three dots that are showing up here so let me bring down this if-then whatever statement down for you so if you ever have the case where these three dots are three these three greater than signs are there then you have to put the else if not at the same indentation level as an if but if you're doing it and you're writing this code as a script then that's how would be the if and else if or at the same indentation level okay they're not at a different indentation level that's very important otherwise the code is not going to run okay an else--if helps you join an F with an else if statement and another one that you might need is else and else does not care for a condition okay you see your if statement cares for a condition and then it runs the code your else if statement cares for a condition and if that condition is satisfied it runs the code else does not take in condition if none of these guys match then else runs by default okay so if condition all right if this condition evaluates to true then then this block of code runs okay good again or we don't care who did the thing we don't care who committed the crime if any one of them did we just break it realistically when you have this case where a human is bananas or I mean you're not going to put every single option ever that could be wrong right so you can't check for bananas or spaghetti or pizza what you can check for is human did not pick if one of them did not pick let's say rock papers or scissors then it should be wrong right so let's see if we can write that if computer does not equal right we can say a rock or computer does not equal scissors or computer does not equal paper then print wrong choice pick again okay so now let's try it now let's say computer is equal to rock and let's run that piece of code and it says wrong choice pick again okay so you can see how using the or statement here ended up giving me the wrong result what I want to make sure is that the computer is equal to any one of these right so let's try it like this let's say computer is not equal rock and computer does not equal scissors right and computer does not equal paper right let's let me let me just go a little bit here make it bigger then print wrong choice pick again okay so the only way this code will run is if all of these things are true meaning computer is not wrong computers not scissors and computers not paper right so if I made computer is equal to banana and I went up here and I ran this code you can see that it checked his computer rock his computer scissors its computer paper if it's not any of these then print wrong choice okay so you can see how powerful these Combinator's are is that what I would call him like you're a logical operator boolean operators right like and and or you really really need them to be able to do this stuff okay to be able to do really powerful things ok I'm going to cut this video off here hopefully you have a much better understanding of how this stuff works ok in the next video we will cover for loops and I will see you therehey guys what is going on my brain is about to blow up I've been reading this book called the little schemer and it's about this programming language called scheme and in this programming language everything is done really difficult in a very difficult way and I spent eight hours studying it so my brain is like fried but I'm excited to take a break from studying it and get on with teaching something about computer programming in Python so we covered in our last video boolean's and we talked more specifically covered the boolean logical operators we talked about and and we talked about or and we talked about not so another thing I wanted to mention to you guys I had it open here so I don't freak yet if you guys are on my website you can write code directly underneath while you're watching the video so you can write something like print 5 or whatever and hit this and it'll run it and actually give you the results so if you don't know if you're having term if you're having problems running your code or you just want somewhere easy to run it well you can go here and watch the videos and run it at the same time back to where we were okay so as you can see we covered a lot of this stuff right with and or and not so now let's do some examples where we combine our conditionals control flow with our boolean logic okay so this is what's gonna tie it all together this is what's gonna allow you to make powerful games and do powerful things right now okay not in hundred more videos not reading one whole another book you can start to build games and functional programs doing what I'm about to show you okay so let's get to it so you saw I showed you okay you have boolean operators like you true and false and you get some result now I also showed you how you can have some examples like Johnny homework and this and then this code runs here okay I showed you that at the top so let me just bring that down whoops the whole example this is not real code this is just you know it is going to give you wrong answers for now so now let's try to do those things where if you had an employee those getting paid over 40 hours and you want her to give him overtime pay again let's tap it all in here and let's see how that would work so if I did Bob hours work and I said okay he work let's say 40 hours and if I said if Bob hours greater than 39 then print he worked overtime for some reason we have less than 40 hours as overtime but whatever never mind that he worked overtime so let's see if that prints out if it does print out then that means he worked overtime as and as you can see Bob hours worked for 44 T is greater than 39 so that part evaluated to true and then you got back the statement now what if we wanted to make it a little bit more interesting let's go back to the example of the kid where he throws the garbage and he does his homework right so for example let's say Johnny homework aha is equal to true meaning he completed his homework assignment now let's say I Johnny whoops Johnny throw out garbage and let's make that also true so he's done both of the things now let's see if he gets to play a a video game okay let's let's see if he gets to play his Xbox 360 so if Johnny homework right dad says if Johnny does his homework and Johnny throws out the garbage whoops I'm sorry then um Johnny can play xbox 360 right so if that statement points out he can play xbox 360 and I don't know why I'm running Xbox 360 I feel like I'm living in 2005 Xbox one has been out and I'm pretty sure in the next few years I'm gonna bring out the new one so let's change this Xbox one and let's hit enter so it printed it out Johnny can play xbox one how did this work Johnny homework evaluated to a true statement Johnny throw out garbage evaluator true statement the only time you get back a true when you combine something with an end is when both of the statements are true so true true and we got back that right now just for some little exercises here and there what if I did something like not Johnny homework oops Johnny homework or Johnny Johnny throw out garbage right what do you think I would get here as a result hmm let's think about it so if you guessed it correctly good pause the video or let's try to figure out together so not throw out garbage not true right throughout garbage sure not true gives you false and this part is true so you essentially get true or true and then not true or true gives you back true because for an or condition to value to true only one of the conditions have to be true and then calling not on a true statement gives you Backa false and hence we get a false okay again I'm not going to focus so much on these contrived force boolean examples because your thing is never going to get like that and but it's just good to know how it works right it works through the inner parentheses inside of the parentheses first and then it goes outside of the parentheses and starts to evaluate things okay so you can see we were able to put things together now imagine if you were making a game of rock-paper-scissors right let's rock paper scissors right I can say if human picked let's say rock and computer picked scissors now I can say if now I can say let's see what what can I say I can say something like if human equal equal Rock and computer picked scissors then who wins right who gets the point here so I am going to say that human score is equal to 1 okay so human picked Rock computer pick scissors right in rock paper scissor just in case you didn't know rock beats scissors and now let's check the human score and you can see that when I got back of one okay so that's why if conditions are so important we can also start to tack them on okay so let's say that let's say if human picked Rock and computer pick scissors if this was not the case what if there was some other case let's say computer pick something else so let's say computer is bananas right so if I write this line of code how can I check for other things so I can say oops I can say else if oh I got to get outside of the if statement I can say else if computer sorry human picked Rock and computer picked bananas then you can say I don't know computer is equal to computer score is equal to zero and human score is equal to zero because bananas is not really a valid thing so you get back both got zero and then it says something like prints you can't pick herbs you can't pick anything other than rock paper or scissors right something like that and let's hit it and so you can see that first it check this if condition it checked if human is equal to rock while human was equal to rock the only way this statement will run if both of the statements are true that's what the end forces you to do if we had an order here what it would do is it says if human is rocking computer is scissors okay so in our case it would go as human equal to rock it would say true and then would go or computer equal scissors it would evaluate that to false because bananas bananas right there does not equal scissors so it would go true or false it would evaluate that as true because true or false is true and it would go okay human score is equal to one okay but we don't want that we want both of the things to be true if human picks rock and the computer pick scissors at the same time that's when the human wins but in this case computer picked bananas and we said if you pick bananas as you know if that happened where human picked Rock and computer picked bananas we can say that you know you can't pick that you got to try again now why our statement is useful as imagine if I mean do you care who picks bananas do care if computer picks bananas or human picks bananas should you stop the game if either of those people pick bananas yeah you should right you should stop the game and start it again because Anna should not be one of the options so you don't care who picked something that's not the wrong one you just care that wrong one is picked that's why you would use or you would say if human equal equal bananas or computer equal equal bananas then print blah blah blah blah blah right in this case it won't make sense but then whatever right we print out this line of code elsif notice the else if it's used to connect these if you want to actually write it as i can show you that later but if you essentially want to write it you know what you have to do and you're writing it where these three dots aren't there right your three dots that are showing up here so let me bring down this if-then whatever statement down for you so if you ever have the case where these three dots are three these three greater than signs are there then you have to put the else if not at the same indentation level as an if but if you're doing it and you're writing this code as a script then that's how would be the if and else if or at the same indentation level okay they're not at a different indentation level that's very important otherwise the code is not going to run okay an else--if helps you join an F with an else if statement and another one that you might need is else and else does not care for a condition okay you see your if statement cares for a condition and then it runs the code your else if statement cares for a condition and if that condition is satisfied it runs the code else does not take in condition if none of these guys match then else runs by default okay so if condition all right if this condition evaluates to true then then this block of code runs okay good again or we don't care who did the thing we don't care who committed the crime if any one of them did we just break it realistically when you have this case where a human is bananas or I mean you're not going to put every single option ever that could be wrong right so you can't check for bananas or spaghetti or pizza what you can check for is human did not pick if one of them did not pick let's say rock papers or scissors then it should be wrong right so let's see if we can write that if computer does not equal right we can say a rock or computer does not equal scissors or computer does not equal paper then print wrong choice pick again okay so now let's try it now let's say computer is equal to rock and let's run that piece of code and it says wrong choice pick again okay so you can see how using the or statement here ended up giving me the wrong result what I want to make sure is that the computer is equal to any one of these right so let's try it like this let's say computer is not equal rock and computer does not equal scissors right and computer does not equal paper right let's let me let me just go a little bit here make it bigger then print wrong choice pick again okay so the only way this code will run is if all of these things are true meaning computer is not wrong computers not scissors and computers not paper right so if I made computer is equal to banana and I went up here and I ran this code you can see that it checked his computer rock his computer scissors its computer paper if it's not any of these then print wrong choice okay so you can see how powerful these Combinator's are is that what I would call him like you're a logical operator boolean operators right like and and or you really really need them to be able to do this stuff okay to be able to do really powerful things ok I'm going to cut this video off here hopefully you have a much better understanding of how this stuff works ok in the next video we will cover for loops and I will see you there\n"