Contact Us Today ! | canadasteamsyouth@gmail.com

Canada STEAMS Youth Association

It takes a village to raise a child.

Python Unit 4

Unit 4: Asking Questions with if and else

In programming, we often ask yes or no questions, and decide to do something based on the answer. For example, we might ask, “Are you older than 20?” and if the answer is yes, respond with “You are too old!”

These sorts of questions are called conditions, and we combine these conditions and the responses into if statements.

 

  • If Statements

—————————————-

>>> age = 13

>>> if age > 20:

print(‘You are too old!’)

—————————————-

 

—————————————-

>>> age = 25

>>> if age > 20:

▯▯▯▯print(‘You are too old!’)

▯▯▯▯print(‘Why are you here?’)

▯▯▯▯print(‘Why aren\’t you mowing a lawn or sorting papers?’)

—————————————-

 

—————————————-

>>> age = 25

>>> if age > 20:

▯▯▯▯print(‘You are too old!’)

▯▯▯▯▯▯print(‘Why are you here?’)

—————————————-

 

  • Conditions Help Us Compare Things

We use the symbols in Python (called operators) to create our conditions

Examples 1:

>>> age = 10

>>> if age > 10:

print(‘You are too old for my jokes!’)

Examples 2:

>>> age = 10

>>> if age >= 10:

print(‘You are too old for my jokes!’)

Examples 3:

>>> age = 10

>>> if age == 10:

print(‘You are too old for my jokes!’)

  • If-Then-Else Statements

What does If-Then-Else Statements do?

If something is true, then do this; or else, do that”

Examples 4:

>>> age = 12

>>> if age == 12:

print(“A pig fell in the mud!”)

else:

print(“Shh. It’s a secret.”)

 

Examples 5:

>>> age = 8

>>> if age == 12:

print(“A pig fell in the mud!”)

else:

print(“Shh. It’s a secret.”)

  • if and elif Statements

We can extend an if statement even further with elif (which is short for else-if).

 

Examples 6:

>>> age = 12

>>> if age == 10:

print(“What do you call an unhappy cranberry?”)

print(“A blueberry!”)

elif age == 11:

print(“What did the green grape say to the blue grape?”)

print(“Breathe! Breathe!”)

elif age == 12:

print(“What did 0 say to 8?”)

print(“Hi guys!”)

elif age == 13:

print(“Why wasn’t 10 afraid of 7?”)

print(“Because rather than eating 9, 7 8 pi.”)

else:

print(“Huh?”)

 

When you enter this code in the IDLE, it will automatically indent, so be sure to press the backspace or delete key once you’ve typed each print statement, so that your if, elif, and else statements will start at the far-left margin.

 

  • Combining Conditions

You can combine conditions by using the keywords and and or, which produces shorter and simpler code.

 

Examples 7:

>>> age = 12

>>> if age == 10 or age == 11 or age == 12 or age == 13:

print(‘What is 13 + 49 + 84 + 155 + 97? A headache!’)

else:

print(‘Huh?’)

In this code, if any of the conditions on the first line are true (in other words, if age is 10, 11, 12, or 13), the block of code on the next line beginning with print will run.

 

To shrink this example even further, we could use the and keyword, along with the greater than or equal-to operator (>=) and less-than-or-equal-to operator (<=)

 

>>> if age >= 10 and age <= 13:

print(‘What is 13 + 49 + 84 + 155 + 97? A headache!’)

else:

print(‘Huh?’)

  • Variables with No Value—None

Just as we can assign numbers, strings, and lists to a variable, we can also assign nothing, or an empty value, to a variable.

In Python, an empty value is referred to as None, and it is the absence of value. The value None is different from the value 0.

 

>>> myval = None

>>> print(myval)

Assigning a value of None to a variable is one way to reset it to its original, empty state. Setting a variable to None is also a way to define a variable without setting its value.

 

You can check for None in an if statement as well:

>>> myval = None

>>> if myval == None:

print(“The variable myval doesn’t have a value”)

 

  • The Difference Between Strings and Numbers

>>> age = 10

>>> if age == 10:

print(“What’s the best way to speak to a monster?”)

print(“From as far away as possible!”)

 

>>> age = ’10’

>>> if age == 10:

print(“What’s the best way to speak to a monster?”)

print(“From as far away as possible!”)

 

>>> age = ’10’

>>> converted_age = int(age)

 

>>> age = 10

>>> converted_age = str(age)

 

>>> age = ’10’

>>> converted_age = int(age)

>>> if converted_age == 10:

print(“What’s the best way to speak to a monster?”)

print(“From as far away as possible!”)

 

>>> age = ‘10.5’

>>> converted_age = int(age)

>>> age = ‘10.5’

>>> converted_age = float(age)

>>> print(converted_age)

>>> age = ‘ten’

>>> converted_age = int(age)

 

Programming Puzzles

#1: Are You Rich?

What do you think the following code will do? Try to figure out

the answer without typing it into the shell, and then check your

answer.

>>> money = 2000

>>> if money > 1000:

print(“I’m rich!!”)

else:

print(“I’m not rich!!”)

print(“But I might be later…”)

 

#2: Twinkies!

Create an if statement that checks whether a number of Twinkies

(in the variable twinkies) is less than 100 or greater than 500. Your program should print the message “Too few or too many” if the

condition is true.

 

#3: Just the Right Number

Create an if statement that checks whether the amount of money

contained in the variable money is between 100 and 500 or between

1,000 and 5,000.

 

#4: I Can Fight Those Ninjas

Create an if statement that prints the string “That’s too many”

if the variable ninjas contains a number that’s less than 50, prints

“It’ll be a struggle, but I can take ’em” if it’s less than 30, and

prints “I can fight those ninjas!” if it’s less than 10. You might

try out your code with:

>>> ninjas = 5

SEE ALL Add a note
YOU
Add your Comment
 

Canada STEAMS Youth Association

加拿大青少年英才协会

All rights reserved.

X