+ All Categories
Home > Business > Lesson 303 05 jan14-1500-ay

Lesson 303 05 jan14-1500-ay

Date post: 13-May-2015
Category:
Upload: codecademy-ren
View: 6,527 times
Download: 0 times
Share this document with a friend
Popular Tags:
26
Unit 3: Python Lesson 3: Boolean Logic January 5, 2014
Transcript
Page 1: Lesson 303 05 jan14-1500-ay

Unit 3: PythonLesson 3: Boolean Logic

January 5, 2014

Page 2: Lesson 303 05 jan14-1500-ay

2

Lesson 3: Boolean Logic

LoopsDesigning a Game

Working with Files

Lesson 8 Lesson 7 Lesson 6

Data Types

Lesson 5

Functions

Lesson 4

Boolean Logic

Lesson 3

Introduction to Programming

Hardware & Software

Lesson 1 Lesson 2

Putting It All Together

Lesson 12

Navigating the Web (?)

Lesson 11

Sorting and Searching

Advanced Algorithms

Lesson 9 Lesson 10

Page 3: Lesson 303 05 jan14-1500-ay

3

Recap from last time (I)

• Software is electronically stored data that allows us to interact with our devices

• Hardware is the physical device that we need to interact with our computer programs

• You can think of software and hardware as two pieces that come together to make the finished product that we use such as browsing Google, writing a Word document, or playing Angry Birds on our iPhone

Page 4: Lesson 303 05 jan14-1500-ay

Recap from last time (II)

• Our computer hardware needs software called an operating system in order to function

• Updating software occurs much more frequently than updating hardware

• Software communicates with hardware through programming languages

4

Page 5: Lesson 303 05 jan14-1500-ay

5

Computers use a very simple vocabulary

• While computers can be programmed to do some amazing things, they only know a few simple words

• Today, we will look at some of the basic vocabulary that computers rely upon

Page 6: Lesson 303 05 jan14-1500-ay

6

TRUE and FALSE are the most basic words a computer will use

• To a computer, the answer to any question will either be TRUE or FALSE. If a computer asked you if you were hungry, you wouldn’t say “Yes” or “No” – you would say “TRUE” or “FALSE”

• Using TRUE and FALSE is how computers think, and it’s called Boolean logic after George Boole, a 19th Century English mathematician

Are you hungry? TRUE

George Boole

Page 7: Lesson 303 05 jan14-1500-ay

7

Combine words with AND, NOT, and OR

• Computers also love to use the words “AND”, “NOT”, and “OR”, but their meanings can be a little different from how we would use them

• Let’s imagine a restaurant where the menu has only three items

Fish Chips Mushy Peas

Page 8: Lesson 303 05 jan14-1500-ay

8

AND means “both”

• “AND” is used just like we would say “And” in English

• If a computer asked if you were hungry for “Fish AND Chips”, it would want to know if you wanted “Fish and Chips both”

Fish AND Chips?

Fish and Chips

Page 9: Lesson 303 05 jan14-1500-ay

9

NOT means “Everything except”

• “NOT” to computers is a little different from the English “Not”

• If a computer asked if you were hungry for “NOT Fish”, it would want to know if you wanted “Everything except Fish”

• Since there are only three items on the menu, this means “Chips and Mushy Peas”

NOT Fish?

Chips Mushy Peas

+

Page 10: Lesson 303 05 jan14-1500-ay

10

Remember that OR is a little different (I)

• In English, asking if you want “Fish or Chips” is the same as asking if you want:

Fish only Chips only

Either or

Page 11: Lesson 303 05 jan14-1500-ay

11

Remember that OR is a little different (II)

• In English, asking if you want “Fish or Chips” is the same as asking if you want:

• But to a computer, asking if you want “Fish OR Chips” is equivalent to asking if you want:

Fish only

or

Fish only

or or

Fish and Chips

Chips only

Chips only

Page 12: Lesson 303 05 jan14-1500-ay

12

See if you can figure these out on your own

1. Fish AND Chips AND Mushy Peas

2. NOT Mushy Peas

3. Fish OR Mushy Peas

Page 13: Lesson 303 05 jan14-1500-ay

13

Did you get them right?

1. Fish AND Chips AND Mushy Peas

2. NOT Mushy Peas

3. Fish OR Mushy PeasFish and Chips

Fish only

or

Mushy Peas only

or

Fish and Chips and Mushy Peas

Fish and Mushy Peas

Page 14: Lesson 303 05 jan14-1500-ay

14

Let’s see a few more examples

• This time, let’s imagine that the menu has four items for breakfast

TeaToastEggsBacon

Page 15: Lesson 303 05 jan14-1500-ay

15

Combining AND, NOT, and OR can get messy!

1. NOT Tea 2. NOT (Toast AND Tea)

3. Bacon OR Eggs OR Tea 4. (Bacon AND Eggs) OR Toast

Page 16: Lesson 303 05 jan14-1500-ay

16

How did you do?

1. NOT Tea

Bacon, Eggs, and Toast

2. NOT (Toast AND Tea)

3. Bacon OR Eggs OR Tea 4. (Bacon AND Eggs) OR Toast

Any combination of Bacon, Eggs, and Tea

Bacon and Eggs

Bacon and Eggs

or

Toast only

or

Bacon, Eggs, and Toast

Page 17: Lesson 303 05 jan14-1500-ay

17

This vocabulary is useful in IF statements (II)

• IF statements allow a computer to behave differently under different situations

• In the examples below, you’ll see how IF statements use the vocabulary we just learned

if “you are tired”: “go rest for a while”

If “you are tired” is TRUE, then “go rest for a while”

IF statement Explanation

Page 18: Lesson 303 05 jan14-1500-ay

18

This vocabulary is useful in IF statements (II)

• IF statements allow a computer to behave differently under different situations

• In the examples below, you’ll see how IF statements use the vocabulary we just learned

if “you are tired”: “go rest for a while”

If “you are tired” is TRUE, then “go rest for a while”

IF statement Explanation

if “you are tired” OR “you are sick”: “go rest for a while”

If “you are tired” is TRUE, orIf “you are sick” is TRUE, orIf “you are tired and sick” is TRUE, then “go rest for a while”

Page 19: Lesson 303 05 jan14-1500-ay

19

You can turn an IF into an IF-ELSE (I)

• We can start with a regular IF statement like in the example below

if “the water looks clean”: “drink it”

If “the water looks clean” is TRUE, then “drink it”.

IF-ELSE statement Explanation

Page 20: Lesson 303 05 jan14-1500-ay

20

You can turn an IF into an IF-ELSE (II)

• By adding an ELSE, you can decide what happens when the IF statement is FALSE

if “the water looks clean”: “drink it”

else: “buy bottled water”

If “the water looks clean” is TRUE, then “drink it”.

If “the water looks clean” is FALSE,then “buy bottled water”

IF-ELSE statement Explanation

Page 21: Lesson 303 05 jan14-1500-ay

21

You can even make it an IF-ELIF-ELSE (I)

• Sometimes you will have more than two cases to choose from. In these situations, you first start with a regular IF statement

if “Manchester United scored more goals than Liverpool”: “Manchester United wins!”

If “Manchester United scored more goals than Liverpool” is TRUE, then “Manchester United wins!”

IF-ELIF-ELSE statement Explanation

Page 22: Lesson 303 05 jan14-1500-ay

22

You can even make it an IF-ELIF-ELSE (II)

• Then we can add on an ELIF to check for a second case

if “Manchester United scored more goals than Liverpool”: “Manchester United wins!”

elif “Liverpool scored more goals than Manchester United”: “Liverpool wins!”

If “Manchester United scored more goals than Liverpool” is TRUE, then “Manchester United wins!”

If “Liverpool scored more goals than Manchester United” is TRUE,then “Liverpool wins!”

IF-ELIF-ELSE statement Explanation

Page 23: Lesson 303 05 jan14-1500-ay

23

You can even make it an IF-ELIF-ELSE (III)

• Finally, we can use an ELSE to handle any other cases

if “Manchester United scored more goals than Liverpool”: “Manchester United wins!”

elif “Liverpool scored more goals than Manchester United”: “Liverpool wins!”

else: “Break the tie with penalty kicks”

If “Manchester United scored more goals than Liverpool” is TRUE, then “Manchester United wins!”

If “Liverpool scored more goals than Manchester United” is TRUE,then “Liverpool wins!”

If both statements are FALSE,then “Use penalty kicks to break the tie”

IF-ELIF-ELSE statement Explanation

Page 24: Lesson 303 05 jan14-1500-ay

24

Summary (I)

• Boolean Logic is a phrase used to describe the way computers think only in TRUE and FALSE

• AND, NOT, and OR can be used to combine statements together, but their meanings are a little different from their English meanings

• Remember that OR means one, or the other, or both!

Fish only

or or

Fish and ChipsChips only

Page 25: Lesson 303 05 jan14-1500-ay

25

Summary (II)

• IF statements allow a computer to perform differently when in different situations

• Add ELSE to decide what will happen when the IF statement is FALSE

• Add ELIF when you have more than two cases to choose from

Manchester United wins! Liverpool wins! Penalty kicks

Page 26: Lesson 303 05 jan14-1500-ay

26

What to do on your own

1. Go to URL to complete the Codecademy course online

2. Do the practice set on the material learned

3. Take the follow-up quiz to test your understanding


Recommended