Learn Python Programming - 17 - Boolean Algebra Jiu-Jitsu

**Understanding Comparison Operators**

Comparison operators are used to check if one value is equal to another. In programming, these operators allow us to write conditions that evaluate to true or false. The most common comparison operators are: equals (=), not equal (!=), less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=).

The equality operator (=) checks if two values are the same. For example, `5` is equal to `5` because they represent the same value. On the other hand, `5` does not equal `10` because they do not represent the same value.

In addition to equals, there is also a does not equal sign (!=). This operator checks if two values are not the same. So, `5` does not equal `10`, but `5` is indeed equal to itself.

The less than and greater than operators are used to check if one value is smaller or larger than another. For example, `5` is less than `10`, but it is not less than `3`. Similarly, `10` is greater than `5`, but it is not greater than `3`.

There is also a less than or equal to and greater than or equal to operator. These operators check if one value is either smaller or larger than another, or if they are the same. For example, `5` is both less than or equal to `10` and greater than or equal to itself.

**Using Conditions**

Conditions are used to evaluate a condition in code that returns true or false based on certain criteria. In programming, conditions are often used as part of conditional statements, which allow us to execute different blocks of code depending on the result of the condition.

For example, we can use a condition to check if someone worked more hours than 40. We might create a variable called `JohnnyHoursWork` and set it equal to 40, then write a condition that checks if `JohnnyHoursWork` is greater than 40. If this condition is true, we don't pay Johnny overtime. However, if the condition is false, we do pay him overtime.

Let's try an example with Python code:

```python

# Define the variable JohnnyHoursWork

JohnnyHoursWork = 41

# Write a condition to check if Johnny worked more hours than 40

if JohnnyHoursWork > 40:

# Pay Johnny overtime

print("Pay Johnny overtime")

else:

# Don't pay Johnny overtime

print("Don't pay Johnny overtime")

```

In this example, `JohnnyHoursWork` is equal to 41, which is greater than 40. Therefore, the condition in our Python code is true and we print "Pay Johnny overtime". If `JohnnyHoursWork` were equal to or less than 40, the condition would be false and we would print "Don't pay Johnny overtime".

**Boolean Operators**

In addition to comparison operators, there are also Boolean logical operators. These operators allow us to combine multiple conditions with AND and OR operations.

One of the main Boolean logical operators is AND. The AND operator checks if both conditions before it are true. For example, `5` is equal to `10`, but `5` is less than `7`. If we use the AND operator with these two conditions, we would get a false result because only one of the conditions is true.

Another Boolean logical operator is OR. The OR operator checks if at least one condition before it is true. For example, `5` is equal to `10`, but `5` is also greater than or equal to itself. If we use the OR operator with these two conditions, we would get a true result because at least one of the conditions is true.

Finally, there is also a NOT operator. The NOT operator checks if a condition before it is false. For example, `5` does not equal `10`, but `5` is equal to itself. If we use the NOT operator with this condition, we would get a false result because the condition is actually true.

Let's try an example with Python code:

```python

# Define the variables JohnnyHoursWork and hours_worked

JohnnyHoursWork = 41

hours_worked = 41

# Write conditions to check if Johnny worked more hours than 30 or less than 40

if (JohnnyHoursWork > 30) and (hours_worked >= 40):

# Pay Johnny overtime

print("Pay Johnny overtime")

else:

# Don't pay Johnny overtime

print("Don't pay Johnny overtime")

# Write conditions to check if Johnny worked more hours than 30 or less than 40

if (JohnnyHoursWork > 30) or (hours_worked < 40):

# Pay Johnny overtime

print("Pay Johnny overtime")

else:

# Don't pay Johnny overtime

print("Don't pay Johnny overtime")

# Write a condition to check if Johnny worked more hours than 30 but not less than 40

if (JohnnyHoursWork > 30) and (hours_worked >= 40):

# Pay Johnny overtime

print("Pay Johnny overtime")

else:

# Don't pay Johnny overtime

print("Don't pay Johnny overtime")

# Write a condition to check if Johnny worked more hours than 30 or less than 40

if not (JohnnyHoursWork <= 30) and not (hours_worked > 40):

# Pay Johnny overtime

print("Pay Johnny overtime")

else:

# Don't pay Johnny overtime

print("Don't pay Johnny overtime")

```

In this example, we use the AND operator to combine two conditions that check if `JohnnyHoursWork` is greater than 30 and `hours_worked` is greater than or equal to 40. We also use the OR operator to combine two conditions that check if `JohnnyHoursWork` is greater than 30 or `hours_worked` is less than 40.

Finally, we use the NOT operator to negate a condition that checks if `JohnnyHoursWork` is greater than or equal to 31 and `hours_worked` is greater than 40.

"WEBVTTKind: captionsLanguage: enhey guys what's up this is kazzy this is my third time making this video last two recordings got deleted and they were really long I'm going to try to make it short and sweet right it only lets me make him better so that's what I'm actually excited for um anyways in this video we're going to talk about Boolean algebra jiujitsu okay uh before we get to that I'm going to just take a second to show you guys check it out the official clever programmer premium short sleeve t-shirt custom designed let me know what you guys think and I'll put in the link for you guys below so if you guys want to actually get it you can order it um some feedback would be great if you think it sucks or if you think it's good let me know I would love to know that let's get on with what we are trying to cover for today okay so uh I want to talk about pulling algebra all right so what is it and how does it work and why do we need to know it it's one of the most important things in programming that's it period in all of computer programming languages anything that you use even in electricity it's super important at the end of the day everything turns to zero one or true or false okay so we have false or true notice how there special statements in Python which is why they highlight into the color orange and P and then it doesn't give you an error python like understands what true and false is for example if you do true like this or false like this uh it's going to give you back an error saying I have no idea what you're talking about so there are like pre-existing things that are built into python now why would we need to ever use this right why would we need to use this well let me show you guys something that foreshadows a little bit into maybe our one of our next few videos like conditionals and control flow where I'm going to talk more about if statements but let me just foreshadow that and show you guys so you guys can see from a bigger picture perspective how it works and in turn that would make you want to learn what it is and then we can get down into the nitty gritt details cool so for example uh an if statement works like this you have an if statement followed by some condition which does not need to be in parenthesis but I'm just putting in parenthesis and that condition evaluates to a true or a false statement okay and based on that right based on this then this so it's like if that then this okay so if Johnny made more if Johnny worked overtime and he worked more than 40 hours then I want to pay him overtime okay so for example um the only time this block of code runs is if Johnny made more than 40 hours which means it needs to evaluate to true you need to have a true here okay so let's say if true print hello okay and notice that this block of code runs because the statement is true and then this block of code runs right this is the if condition uh it's not very smart what I'm doing here it's kind of stupid I'm just hardcoding true right in there which is not what you're going to be doing most of the times but I'm just doing this to like illustrate how it works in its uh barebone skeleton so to speak and let me show you if false and then see what happens print high and you can see that this part of the code did not run because it goes oh false it's false so I'm not going to run it not going to do this right if falls then you know don't pay him over time or whatever right I could have anything else here I just have print hello but the implications of this are far more than printing stuff it's doing actual stuff right now another reason why this is very important for you guys to learn is imagine if you were hired and you working in a company and you had to design a system for paying employees well if you do your logic wrong then you might be paying every employee over time which means that the company is paying way more money uh to its employees like for example you might write the logic wrong and employee that works 3 hours might be getting paid over time for all of those 3 hours the company has to pay a lot more money and then the company has to fire a lot of people cuz they're like whoa there's a lot of money going out of our pockets right on the other hand you could have the case where no employees getting paid over time even though they worked over time over 40 hours so with great power right as being a programmer comes great responsibility so that's why you want to learn this stuff really really well so you don't make these M uh big mistakes and you can help out um companies and you can help out clients or whoever right so we have this case false print high that's what happens but now let's do something more interesting so I'm going to introduce you guys to something called comparison operators okay so you guys might know the equal equal sign it Compares two things together not one equal sign that makes something something two equal signs check if one thing is equal to another thing okay so I have two equal signs I have less than or uh less than I have a greater than sign I have a less than or equal to to sign I have a greater than or equal to sign and I have a does not equal sign okay these are your comparison operators uh so what do I mean five is uh equal to five it's going to give me back a true right five is five how about if I said five does not equal five what do you think is going to give me false right that doesn't make any sense how about five is greater than five does that make sense no it's not five is less than five nope 5 is less than or equal to 5 it's should give me back a true five is greater than or equal to five and it also gives me back a true because it's not greater than but it is certainly equal to five okay so that's how this works and look at the bottom it breaks down into true false true false right now let's say we wanted to pay Johnny more if he worked going back to our original example let's say we wanted to pay Johnny more if he worked extra hours right if he worked 40 hours or something so how would we check that condition if you worked more than 40 hours how would we check that we have to make that condition we have to check that condition so these are all called conditions because at the end of the day they evaluate to a true or a false okay so this is a condition and then evaluates to a false here so let's make a variable called Johnny hours worked and let's set that equal to 40 and now let's check it Johnny hours work is greater than 40 so I'm like asking my computer a question and they'll say false okay so I know I shouldn't pay him overtime then right cuz I got back a false what if I did is he making is he doing more hours than 30 okay good so at least he's working right he's not just not doing anything okay so he's worked more than 30 hours but he has not worked greater than 40 hours okay has he worked greater than or equal to 40 hours M it says true since I know he hasn't worked greater than 40 hours hours then in this statement greater than or equal to 40 I know that he's worked equal to 40 but let's just double check and say equal equal 40 Okay cool so we now know that Johnny has worked exactly 40 hours so we can't pay him over time in this case but um let's just try it out anyways let's do if Johnny let's turn it into like a like a conditional statement which again we're going to get more into later is greater than 40 right then print pay him overtime oh looks like we're not going to pay him overtime cuz he has worked exactly 40 hours now let's make Johnny's hours 41 hours let's say he's worked 41 hours right so overtime now let's run this and you can see that it says pay him overtime so how could this translate for you oh first of all let's just break down exactly what the this turns into right so we have this statement uh if Johnny worked greater than 40 hours how does this actually work well what is the variable Johnny hours work we made it 41 right 41 is greater than 40 is that true it certainly is 41 is greater than 40 and we get true and then we get into its most bare bone skeleton structure which I showed you guys up at the top right here and it's simple it's simply just a true at the end of the day and then this block of code runs okay and if the same way the reason why this line of code uh for example like let's say I do this right if you work greater than 42 hours why does this line of code not work well again Johnny ARS work is 41 is 41 more than 42 of course not so this turns to false and when this turns to false we get back we actually get back nothing because this line of code does not run okay that's the barebone like that's the main reason why we use Boolean operators there are lots of other reasons that you'll see as well in the next video we're going to get down more into how Boolean logic works so for now I showed you guys comparison operators in the next video we're going to talk about Boolean um logical operators okay so for example we're going to talk about about and and we're going to talk about or and we're going to talk about not all right and how all of those things work in sync with each other that's it for this video I'll see you guys in the next videohey guys what's up this is kazzy this is my third time making this video last two recordings got deleted and they were really long I'm going to try to make it short and sweet right it only lets me make him better so that's what I'm actually excited for um anyways in this video we're going to talk about Boolean algebra jiujitsu okay uh before we get to that I'm going to just take a second to show you guys check it out the official clever programmer premium short sleeve t-shirt custom designed let me know what you guys think and I'll put in the link for you guys below so if you guys want to actually get it you can order it um some feedback would be great if you think it sucks or if you think it's good let me know I would love to know that let's get on with what we are trying to cover for today okay so uh I want to talk about pulling algebra all right so what is it and how does it work and why do we need to know it it's one of the most important things in programming that's it period in all of computer programming languages anything that you use even in electricity it's super important at the end of the day everything turns to zero one or true or false okay so we have false or true notice how there special statements in Python which is why they highlight into the color orange and P and then it doesn't give you an error python like understands what true and false is for example if you do true like this or false like this uh it's going to give you back an error saying I have no idea what you're talking about so there are like pre-existing things that are built into python now why would we need to ever use this right why would we need to use this well let me show you guys something that foreshadows a little bit into maybe our one of our next few videos like conditionals and control flow where I'm going to talk more about if statements but let me just foreshadow that and show you guys so you guys can see from a bigger picture perspective how it works and in turn that would make you want to learn what it is and then we can get down into the nitty gritt details cool so for example uh an if statement works like this you have an if statement followed by some condition which does not need to be in parenthesis but I'm just putting in parenthesis and that condition evaluates to a true or a false statement okay and based on that right based on this then this so it's like if that then this okay so if Johnny made more if Johnny worked overtime and he worked more than 40 hours then I want to pay him overtime okay so for example um the only time this block of code runs is if Johnny made more than 40 hours which means it needs to evaluate to true you need to have a true here okay so let's say if true print hello okay and notice that this block of code runs because the statement is true and then this block of code runs right this is the if condition uh it's not very smart what I'm doing here it's kind of stupid I'm just hardcoding true right in there which is not what you're going to be doing most of the times but I'm just doing this to like illustrate how it works in its uh barebone skeleton so to speak and let me show you if false and then see what happens print high and you can see that this part of the code did not run because it goes oh false it's false so I'm not going to run it not going to do this right if falls then you know don't pay him over time or whatever right I could have anything else here I just have print hello but the implications of this are far more than printing stuff it's doing actual stuff right now another reason why this is very important for you guys to learn is imagine if you were hired and you working in a company and you had to design a system for paying employees well if you do your logic wrong then you might be paying every employee over time which means that the company is paying way more money uh to its employees like for example you might write the logic wrong and employee that works 3 hours might be getting paid over time for all of those 3 hours the company has to pay a lot more money and then the company has to fire a lot of people cuz they're like whoa there's a lot of money going out of our pockets right on the other hand you could have the case where no employees getting paid over time even though they worked over time over 40 hours so with great power right as being a programmer comes great responsibility so that's why you want to learn this stuff really really well so you don't make these M uh big mistakes and you can help out um companies and you can help out clients or whoever right so we have this case false print high that's what happens but now let's do something more interesting so I'm going to introduce you guys to something called comparison operators okay so you guys might know the equal equal sign it Compares two things together not one equal sign that makes something something two equal signs check if one thing is equal to another thing okay so I have two equal signs I have less than or uh less than I have a greater than sign I have a less than or equal to to sign I have a greater than or equal to sign and I have a does not equal sign okay these are your comparison operators uh so what do I mean five is uh equal to five it's going to give me back a true right five is five how about if I said five does not equal five what do you think is going to give me false right that doesn't make any sense how about five is greater than five does that make sense no it's not five is less than five nope 5 is less than or equal to 5 it's should give me back a true five is greater than or equal to five and it also gives me back a true because it's not greater than but it is certainly equal to five okay so that's how this works and look at the bottom it breaks down into true false true false right now let's say we wanted to pay Johnny more if he worked going back to our original example let's say we wanted to pay Johnny more if he worked extra hours right if he worked 40 hours or something so how would we check that condition if you worked more than 40 hours how would we check that we have to make that condition we have to check that condition so these are all called conditions because at the end of the day they evaluate to a true or a false okay so this is a condition and then evaluates to a false here so let's make a variable called Johnny hours worked and let's set that equal to 40 and now let's check it Johnny hours work is greater than 40 so I'm like asking my computer a question and they'll say false okay so I know I shouldn't pay him overtime then right cuz I got back a false what if I did is he making is he doing more hours than 30 okay good so at least he's working right he's not just not doing anything okay so he's worked more than 30 hours but he has not worked greater than 40 hours okay has he worked greater than or equal to 40 hours M it says true since I know he hasn't worked greater than 40 hours hours then in this statement greater than or equal to 40 I know that he's worked equal to 40 but let's just double check and say equal equal 40 Okay cool so we now know that Johnny has worked exactly 40 hours so we can't pay him over time in this case but um let's just try it out anyways let's do if Johnny let's turn it into like a like a conditional statement which again we're going to get more into later is greater than 40 right then print pay him overtime oh looks like we're not going to pay him overtime cuz he has worked exactly 40 hours now let's make Johnny's hours 41 hours let's say he's worked 41 hours right so overtime now let's run this and you can see that it says pay him overtime so how could this translate for you oh first of all let's just break down exactly what the this turns into right so we have this statement uh if Johnny worked greater than 40 hours how does this actually work well what is the variable Johnny hours work we made it 41 right 41 is greater than 40 is that true it certainly is 41 is greater than 40 and we get true and then we get into its most bare bone skeleton structure which I showed you guys up at the top right here and it's simple it's simply just a true at the end of the day and then this block of code runs okay and if the same way the reason why this line of code uh for example like let's say I do this right if you work greater than 42 hours why does this line of code not work well again Johnny ARS work is 41 is 41 more than 42 of course not so this turns to false and when this turns to false we get back we actually get back nothing because this line of code does not run okay that's the barebone like that's the main reason why we use Boolean operators there are lots of other reasons that you'll see as well in the next video we're going to get down more into how Boolean logic works so for now I showed you guys comparison operators in the next video we're going to talk about Boolean um logical operators okay so for example we're going to talk about about and and we're going to talk about or and we're going to talk about not all right and how all of those things work in sync with each other that's it for this video I'll see you guys in the next video\n"