Python for Beginners, Part 17: Boolean Algebra Jiu-Jitsu by@cleverprogrammer

Python for Beginners, Part 17: Boolean Algebra Jiu-Jitsu

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 talk about the most important thing in programming... BOOOOOOOLEAN ALGEBRA. Hmm... Is that a True statement OR False? Watch the video AND find out... Or NOT... Up to you.

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

Transcript

0:00 hey guys what's up this is Qazi this is

0:03 my third time making this video last two

0:05 recordings got deleted and they were

0:07 really long I'm gonna try to make it

0:09 short and sweet right it only lets me

0:11 make him better so that's what I'm

0:13 actually excited for anyways in this

0:16 video we're going to talk about Boolean

0:18 algebra jiujitsu. Okay before we get to

0:23 that I wanna just take a second to show

0:26 you guys check it up the official clever

0:31 programmer premium short sleeve t-shirt

0:36 custom designed, let me know what you

0:39 guys think and I'll put in the link for

0:42 you guys below so if you guys want to

0:44 actually get it you can order it um some

0:48 feedback would be great if you think it

0:50 sucks or if you think it's good let me

0:52 know I would love to know that let's get

0:56 on with what we are trying to cover for

0:58 today, okay so I want to talk about

1:04 Boolean algebra alright, so what is it

1:07 and how does it work and why do we need

1:10 to know it. It's one of the most

1:13 important things in programming that's a

1:18 period in all of computer programming

1:22 languages anything that you use even in

1:24 electricity it's super important at the

1:27 end of the day everything turns to zero

1:29 one or true or false

1:32 okay so we have false or true notice how

1:36 their special statements in Python which

1:38 is why they highlight into the color

1:40 orange and pipe and then it doesn't give

1:44 you an error Python like understands

1:46 what true and false is for example if

1:47 you do true like this or false like this

1:50 it's going to give you back an error

1:51 saying I have no idea what you're

1:53 talking about

1:54 so they're like pre-existing things that

1:57 are built into Python now why would we

2:01 need to ever use this right why would we

2:04 need to use this well let me show you

2:06 guys something that foreshadows a little

2:08 bit into maybe are one of our next few

2:10 videos like condition

2:11 the control flow where I'm going to talk

2:14 more about if statements but let me just

2:15 for shout out that and show you guys so

2:17 you guys can see from a bigger picture

2:19 perspective how it works and in turn

2:22 that would make you want to learn what

2:23 it is and then we can get down into the

2:26 nitty-gritty details, cool so for example

2:29 an if statement works like this you have

2:32 an if statement followed by some

2:35 condition which does not need to be in

2:38 parenthesis but I'm just putting in

2:39 parentheses and that condition evaluates

2:42 to a true or a false statement okay and

2:46 based on that right based on this then

2:52 this so it's like if that then this okay

3:00 so if Johnny made more if Johnny worked

3:03 overtime and he worked more than 40

3:05 hours then I want to pay him overtime

3:09 okay so for example the only time this

3:14 block of code runs is if Johnny made

3:16 more than 40 hours which means it needs

3:18 to value it to true you need to have a

3:20 true here okay so let's say if true

3:25 print hello okay and notice that this

3:32 block of code runs because this

3:35 statement is true and then this block of

3:38 code runs right this is the if condition

3:41 it's not very smart, what I'm doing here

3:43 is kind of stupid I'm just hard coding

3:45 true right in there which is not what

3:47 you're going to be doing most at times

3:48 but I'm just doing this to like

3:50 illustrate how it breaks in it's a bare

3:52 bones skeleton so to speak and let me

3:55 show you if false and then see what

3:57 happens print hi and you can see that

4:01 this part of the code did not run

4:03 because it goes Oh false it's false so

4:06 I'm not going to run it not going to do

4:08 this right it falls and you know don't

4:10 pay him overtime or whatever right I

4:12 could have anything else here I just

4:14 have print hello but the implications of

4:17 this are far more than printing stuff

4:19 it's doing actual software right now

4:23 another reason why this is very

4:25 important for you guys to learn is

4:27 imagine if you were hired, you're working

4:29 in a company and you had to design a

4:31 system for paying employees well if you

4:32 do your logic wrong then you might be

4:35 paying every employee overtime which

4:38 means that the company is paying way

4:40 more money to its employees like for

4:44 example you might write the logic wrong

4:46 and employee that works three hours

4:49 might be getting paid overtime for all

4:50 of those - the hours the company has to

4:52 pay a lot more money and then the

4:54 company has to fire a lot of people

4:55 because they're like well there's a lot

4:58 of money going out of our pockets right

4:59 on the other hand you could have the

5:02 case where no employees getting paid

5:06 overtime even though they worked

5:08 overtime over 40 hours so with the great

5:12 power right as being a programmer comes

5:14 great responsibility so that's why you

5:15 want to learn this stuff really really

5:17 well so you don't make these MIT big

5:19 mistakes and you can help out companies

5:23 and you can help clients or whoever

5:25 right so we have this case false print

5:29 hi that's what happens but now let's do

5:31 something more interesting so I'm going

5:32 to introduce you guys is something

5:33 called comparison operators okay

5:38 so you guys might know the equal equal

5:40 sign it compares two things together not

5:42 one equal sign that makes something

5:44 something two equal signs check if one

5:47 thing is equal to another thing okay so

5:51 I have two equal signs I have less than

5:54 or less than I have a greater than sign

5:58 I have a less than or equal to sign have

6:00 a greater than or equal to sign a but

6:01 does not equal sign okay these are your

6:04 comparison operators so what do I mean

6:08 five is equal to five it's going to give

6:11 me back a true right five is five how

6:16 about if I said five does not equal five

6:18 what do you think is going to give me

6:19 false right that doesn't make any sense

6:21 how about five is greater than five does

6:24 that make sense no it's not five is less

6:26 than five nope five is less than or

6:28 equal to five it should give me back a

6:30 true five is greater than or equal to

6:32 five and it also gives me back a true

6:35 because it's not greater than but it is

6:37 certain

6:38 two-five okay so that's how this works

6:41 and look at the bottom it breaks down

6:43 into true-false true-false right now

6:47 let's say we wanted to pay Johnny more

6:49 if you worked going back to our original

6:50 example let's say we want to pick Johnny

6:52 more if you work extra hours right if

6:55 you work 40 hours or something so how

6:58 would we check that condition if you

7:01 work more than 40 hours how would we

7:02 check that we have to make that

7:04 condition we have to check that

7:05 condition so these are all called

7:07 conditions because at the end of the day

7:10 they evaluate to a true or false okay so

7:12 this is a condition and then evaluates

7:14 to false here so let's make a variable

7:17 called Johnny hours work and let's set

7:20 that equal to 40 and now let's check it

7:23 Johnny

7:24 hours work is greater than 40 so I'm

7:27 like asking my computer question and

7:29 it'll say false okay so I know I

7:32 shouldn't pay him overtime then write

7:34 because I got back a false what if I did

7:35 is he making is he doing more hours than

7:37 30 okay good so at least he's working

7:40 right he's not just not doing anything

7:43 okay so he's worth more than 30 hours

7:45 but he has not worked greater than 40

7:48 hours okay has he worked greater than or

7:53 equal to 40 hours mmm it says true since

7:57 I know he hasn't worked greater than 40

7:59 hours then in this statement greater

8:01 than or equal to 40 I know that he's

8:03 worked equal to 40 but let's just double

8:07 check and say equal equal 40 okay cool

8:11 so we now know that Johnny has worked

8:14 exactly 40 hours so we can't pay him

8:16 over time in this case but let's just

8:19 try it out anyways let's do if Johnny

8:21 let's turn it into like uh like a

8:24 conditional statement which again we're

8:26 going to get more into later is greater

8:29 than 40 right then friend pay him

8:34 overtime

8:35 Oh looks like we're not going to pay him

8:39 overtime because he has worked exactly

8:41 40 hours now let's make Johnny two hours

8:44 41 hours let's say he's worked 41 hours

8:48 right so overtime now let's run this and

8:50 you can see that it says pay

8:52 over time so how could this translate

8:55 for you Oh

8:57 first of all let's just break down

8:58 exactly what this turns into right so we

9:01 have this statement if Johnny were

9:04 greater than 40 hours how does this

9:05 actually work well what is the variable

9:08 Johnny hours work

9:09 we made it 41 right 41 is greater than

9:13 40 is that true it certainly is

9:15 41 is greater than 40 and we get true

9:18 and then we get into its most barebone

9:21 skeleton structure which I showed you

9:23 guys up at the top right here and it's

9:25 simple it's simply just a true at the

9:27 end of the day and then this block of

9:29 code runs okay and if the same way the

9:36 reason why this line of code for example

9:39 like let's say I do this right if you

9:41 were greater than 42 hours why does this

9:43 line of code not work well again Johnny

9:47 hoursworked is 41 is 41 more than 42 of

9:51 course not

9:52 so this turns to false and when this

9:54 turns to false we get back we actually

10:00 get back nothing because this line of

10:01 code does not run okay that's a bare

10:05 bone like that's the main reason why we

10:07 use Boolean operators there are lots of

10:10 other reasons that you'll see as well in

10:11 the next video we're gonna get down more

10:14 into how boolean logic works so for now

10:19 showed you guys comparison operators in

10:20 the next video we're going to talk about

10:22 Boolean logical operators okay so for

10:28 example we're going to talk about and

10:29 and we're going to talk about or and

10:32 we're going to talk about not all right

10:35 and how all of those things work in sync

10:38 with each other that's it for this video

10:41 I'll see you guys in the next video.

Comments

Signup or Login to Join the Discussion

Tags

Related Stories