Python for Beginners, Part 18: Logical AND, OR, NOT by@cleverprogrammer

Python for Beginners, Part 18: Logical AND, OR, NOT

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 logical AND, OR & NOT..

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

Transcript

0:00 So guys what's up this is Qazi from clever

0:01 programmer calm in this video we are

0:04 going to talk about Boolean logical

0:07 operators so in the last video we

0:09 covered the basic jujitsu of Boolean

0:13 algebra kind of like what is the basic

0:17 most fundamental ideas behind Boolean

0:20 algebra and we saw that everything that

0:22 we talked about conversely their true or

0:24 false right so here we have all these

0:28 things converting to true or false

0:30 that's effectively what ends up

0:32 happening at the end so now let's go and

0:37 talk about the operators that I was

0:40 mentioning ok so before I start giving

0:43 you their rules and how they are defined

0:46 in the world of electricity or logic

0:49 gates or programming for that matter I

0:52 want to just give you guys an intuitive

0:54 sense of how it works because that's way

0:57 more important than memorizing these you

1:00 know truth tables for a lot of these

1:02 cases so ATLA so let me get try to give

1:06 you guys an intuition so let's start off

1:09 with and okay we're going to start off

1:13 with this logical operator as you can

1:16 see it's a special word in Python which

1:19 is why it highlights to orange if you

1:21 write and with 2ds it does not highlight

1:23 to orange and let's talk about what it

1:26 does so let's say I did um you know 5 &

1:31 5 it'll give me back a 5 and here it's

1:35 not really making sense

1:36 let's try true and true and let's see

1:42 what it returns it returned true now

1:43 let's try something like true and false

1:45 let's see what that returns that returns

1:48 false so now let me try to give you guys

1:50 an intuition of how it works for and

1:54 let's say there are there two things I

1:57 asked you do you like poison and do you

2:00 like pizza right umm if I asked you that

2:05 question and you could only answer with

2:07 yes or no what would you say

2:12 hopefully you would say false or no

2:15 right and then I would be and if I asked

2:20 you to elaborate then you might say I

2:21 might like pizza but I really don't like

2:24 poison right so the idea behind you not

2:29 liking poison but you liking pizza that

2:33 whole thing when you combine it with an

2:35 end that whole thing becomes false

2:37 because there's one of those things that

2:40 you don't like so you you can't say yes

2:44 to the whole expression that I gave you

2:47 right for and both of the things have to

2:51 be true for you to say yes to it like if

2:54 I asked you do like pizza and ice cream

2:55 you say yeah right but if I tell you

2:59 like jumping off of the cliff or do you

3:01 like jump or do you like doing your math

3:03 homework a lot and you like writing

3:05 essays in that case probably both of the

3:08 things you might not like doing your

3:10 math homework and writing essays and you

3:13 might say no I like doing neither of

3:15 them or for sure you would say false

3:18 right so I can say something like math

3:21 homework let's set it to false meaning

3:26 like you don't like it and let's say

3:29 long essays is equal to false and now

3:34 let's combine the two let's say math

3:36 homework and long essays you get back

3:41 false right math homework is really

3:46 false and long essays or false so false

3:48 and false gives you a false now let's

3:51 try pizza and let's say we like it for

3:54 my friend Danielle he loves pizza so if

3:56 you're watching this video that's who I

3:59 was thinking of when I came up with this

4:00 example let's do ice cream and let's set

4:03 that to true meaning we like both of

4:05 them and let's do pizza and ice cream

4:08 right and you get back true so even

4:12 thinking about it logically

4:13 whenever you combine something with an

4:16 end both of those things are now

4:17 combined so when you're answering you're

4:19 answering for both of them even in real

4:21 life both of those things have to be

4:23 true John

4:25 go throw out your garbage and then you

4:28 get to play right

4:30 so in that case both of those things

4:34 happen to be true if you throw out the

4:39 garbage you know then you kind of get to

4:41 play it's a little bit of a contrived

4:42 example there's more of an if-then

4:44 statement rather than an end okay how

4:48 about this even better

4:50 Johnny throw out your garbage and do

4:52 your homework and then you get to play

4:54 okay in that case the only way Johnny

4:57 will get to play his game is if he has

5:00 thrown out the garbage and done his

5:02 homework much better now I can now I can

5:06 sleep better all right that's the

5:09 breakdown for end so now let me just

5:12 give you some problems and throw it at

5:14 your face okay false and false quickly

5:17 what do you think is going to be you

5:19 should have said false false and true

5:23 remember one of them is false both of

5:25 them end up being false because we can

5:29 imagine that this is poison and then

5:31 this is pizza and you can't say true to

5:34 both of them because there's one of them

5:36 that you don't like for it to be true

5:39 both of them have to be true what if I

5:42 gave you true and false what if I gave

5:45 you true and true what do you think that

5:47 would be okay hopefully you're trying to

5:49 get a little you're starting to get a

5:51 little bit of sense of this why this

5:53 will be important later on is once we

5:55 get to the conditionals of control flow

5:56 section which we should be getting to in

5:58 the next video and there we're going to

6:01 be talking about how you can combine an

6:03 if statement use an end to check two

6:06 conditions and then the block of code

6:08 runs so if Johnny homework and pizza or

6:15 and I don't know throw out garbage then

6:23 this code here runs okay obviously I'm

6:29 going to hit syntax errors right now

6:30 because I haven't defined Johnny

6:31 homework I haven't defined throw out

6:33 garbage and you can't write stuff

6:35 separated by spaces and I don't know who

6:37 rights capital variables only if it's

6:41 constants but we'll get to that later

6:42 all right so now let's go to the or side

6:48 of things what if we combine two things

6:49 with an or if I asked you do you like

6:53 pizza okay for sanity check assume you

6:58 like pizza even if you don't okay so

7:01 just to play along just imagine you like

7:03 pizza so if I ask you do you like pizza

7:06 or doing homework if you had to answer

7:12 that with the true or false what would

7:14 you say if you like pizza or doing math

7:18 homework if you could only enter the

7:20 true or false or yes or no you should

7:23 say yes or you should say true because

7:27 there's one of those things that you

7:29 like and I asked you or they're not

7:31 connected with each other right A or B

7:34 so let's now get and let me show you

7:38 guys this in code if I do false or true

7:40 check out what you get back you get back

7:42 a true statement right if I do true or

7:47 false I get back a true statement and if

7:51 I replace this with something that we

7:53 understand better let's say we don't

7:55 like poison poison is equal to false and

7:58 let's say we do like pizza and we

8:02 already have pizzas true but I'm just

8:04 going to do that it can and now let's

8:06 write this statement over here using

8:09 pizza and poison sorry

8:14 pizza or poison OH

8:19 I misspelled poison but in one of my

8:23 older videos I had it there so don't

8:25 worry about why it figured out what

8:27 poison was but um

8:29 yeah let's just let's just use this

8:32 poison variable that we wrote here so

8:34 you can see that we get back a statement

8:36 that's true because one of the things is

8:39 true and in this case that's pizza so

8:42 for an or statement to be true only one

8:44 of the things have to be true right if I

8:46 asked you do you like ice cream and

8:51 or pizza you would say true if you could

8:54 only answer that with one thing just

8:57 like if you know again we take Johnny or

9:02 our son Bob and we go Bob either if you

9:08 do your homework

9:09 or you throw out the garbage then you

9:11 get to play so he just goes oh sweet I'm

9:14 just gonna throw out the garbage so this

9:16 way he doesn't have to worry about doing

9:18 homework he decides to only do one of

9:20 the tasks and then he can play the game

9:22 but what if he went and tried to be

9:25 extra nice and threw out the garbage and

9:27 then when it did his homework but he

9:30 still would you know us being his dad or

9:34 mom still allow him to play of course he

9:37 did both of the things great that's

9:38 extra bonus but for him to qualify as a

9:42 player or like go play his games

9:45 he really needs only one thing that's

9:47 what the order does in our real life and

9:49 so that's what it does in programming as

9:53 well so now let's just let me just throw

9:56 some examples at your face and you try

9:58 to figure it out

9:59 true or false what do you think I'm

10:01 going to get quickly I'm going to get

10:03 back a true only one out one of them has

10:05 to be true what if I did true or true

10:06 what am I going to get back true

10:09 obviously what if I do true or or sorry

10:13 let's do false or false - false

10:16 statements you like poison or homework

10:21 you're gonna say no you know none of

10:27 them there you go so that's a little bit

10:31 of a breakdown of or and if I wanted to

10:34 break down not for you not is very

10:36 simple not is just something that

10:40 doesn't happen okay so it's it's just

10:43 the negation of something so if I said

10:45 not true what do you think I'd get back

10:46 I get back false if I did not false what

10:50 would I get back true it's just a

10:53 negation so if I do something like not

10:55 true or false what do you think I'm

10:59 going to get back what is this

11:01 expression going to evaluate to true

11:04 or Falls remember for something to be

11:07 true in the or but combined with the

11:09 oral boolean logical operator only one

11:12 of the statements has to be true so here

11:14 we do have a true so true or false

11:16 evaluates to a true and then right this

11:20 pretty much becomes true and then

11:22 calling not untrue gives us false so now

11:26 let's just run it okay and as you can

11:29 see we get back false so what I'm going

11:32 to do for you guys on clever programmer

11:34 comm is if you guys go to submit your

11:37 exercises here what I'm going to do is

11:40 I'll have or sorry if you go to do the

11:42 watch the video course on my website I

11:44 will have exercises here for you where

11:47 you can kind of drill and kill how true

11:49 and false and the stuff Boolean

11:51 operators work that's going to be very

11:53 very useful for you guys so then when

11:57 you're writing your if-then statements

11:59 you'll understand what you're doing okay

12:02 but this is kind of you know we just

12:04 need a basic understanding of it some

12:07 places go really crazy in depth into it

12:10 like creating things like not true or

12:12 false or true you know just just

12:18 combining it and making it into it like

12:23 these can be good exercises for you to

12:25 figure out but realistically when you're

12:27 writing code you're not going to be

12:28 doing stuff like this so if you can

12:30 figure this out in your head great but

12:33 um you know like you're not writing code

12:36 like this most of the time so I wouldn't

12:38 want to worry about it too much but if

12:40 you want to go ahead and give yourself

12:41 some crazy exercises like this to do or

12:44 come to my website where I'll have some

12:46 exercises for you guys to do that would

12:47 be great and before I conclude this

12:51 video let's go back to here I have put

12:55 together a truth table for you guys or

12:59 rather it's on Wikimedia @ org so I

13:04 didn't put this together but I have an

13:06 image I'll link you guys the image and

13:07 you can see how the truth tables work so

13:12 in the a column we have some statement

13:15 imagine it being P

13:17 it's a-- right we have that statement

13:20 and be say some other statement so we

13:23 don't really know what these statements

13:25 are for now but you can imagine them to

13:27 be whatever you want so let's say we

13:29 have some statement and some statement B

13:31 if a is false and B is false then a and

13:35 B you get back false right remember and

13:40 so if you have again a being poison and

13:45 B being homework poison and homework you

13:50 get back false you like neither of them

13:52 and if we have a or B since both of them

13:55 are false it's still false and not a is

13:58 just the opposite of that you get back

14:02 true and same thing over here we have

14:06 false and true false and true gives you

14:10 back a false false and true you only

14:14 need one thing to be true in or

14:15 statements you get back a true and then

14:17 not a which is saying you know opposite

14:21 of a since a was false you get back a

14:24 true ok so one thing I need you want you

14:28 to notice about and operator and or

14:32 operators if you look down the column of

14:34 the end operator you'll only see one

14:36 true case and the reason and the only

14:40 reason why it's true is because both of

14:43 the statements are true okay that's the

14:46 key thing to remember both of the things

14:49 are true and if you go and look in the

14:50 or column you'll see the or being false

14:55 only in one case and the only case is

14:57 false is when both of the things are

14:58 false okay so notice the key distinction

15:01 between the two okay

15:04 that's kind of all you have to know for

15:06 a Boolean stuff and I'm not going to go

15:08 into the next video with the really long

15:10 nested Boolean statements it's kind of

15:12 useless we don't really think that way

15:14 anyways and we're gonna get into

15:18 conditionals and control flow so I will

15:20 see you guys there

Comments

Signup or Login to Join the Discussion

Tags

Related Stories