if Statementsif Statementif-else StatementsifStatementsif Statementsif-elif-else StatementsAre there any questions before I begin?
If you have the textbook you should read Chapter 3, Decision Structures and Boolean Logic, from Starting Out with Python.
I have posted a solution to homework 2 here.
Let's take a look.
I have posted homework 4 here.
It is due this coming Sunday at 11:59 PM.
After I finish talking I will pass out the papers for today's graded quiz.
Today I will talk about the 3 kinds of if
statements.
I will also talk about values that can only be true or false, and the operators used with them.
print statements
print("You will need to invest", present_value, "dollars at", \
rate, "interest for", years, "years")
print("Hello")
print("Hello world")
| \n | Newline | Causes output to be advanced to the next line |
| \t | Tab | Causes output to skip over to the next horizontal tab position |
| \' | Single quote | |
| \" | Double quote | |
| \\ | Backslash |
>>> team = "Red Sox" >>> team 'Red Sox' >>> cheer = "Let's go " + team >>> cheer "Let's go Red Sox"
>>> value = 5
>>> s = "The value is " + value
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
str
>>> s = "The value is " + str(value) >>> s 'The value is 5'
if Statementsif
statement
if statements
if statementsif-elsestatementsif-elif-else statementsif Statementif statement
if CONDITION:
STATEMENT
STATEMENT
...
if statement is an
expression
boolTrueFalse
>>> result = True
>>> result = true
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined. Did you mean: 'True'?
>>> type(True)
<class 'bool'>
>>> type("true")
<class 'str'>
>>> type(False)
<class 'bool'>
>>> type("False")
<class 'str'>
boolbool will convert any number to True if that number
is not zero
>>> bool(5) True >>> bool(-5) True >>> bool(0) False
bool works the same way on float values
>>> bool(5.15) True >>> bool(-6.75) True >>> bool(0.0) False
bool also works on strings
>>> bool("true")
True
>>> bool("false")
True
>>> bool("")
False
>>> bool(" ")
True
if statement consists of the
keyword
if ...
True
or False
True or False
is called a
boolean expression
True or False depending which is the bigger value| Operator | Meaning |
|---|---|
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| == | Equal to |
| != | Not equal to |
>>> 5 > 4 True >>> 5 < 4 False >>> 5 >= 4 True >>> 5 <= 4 False >>> 5 == 4 False >>> 5 != 4 True
a > b + 6
| ** | Exponentiation |
| * / // % | Multiplication, division and remainder |
| + - | Addition and subtraction |
| > < >= <= == != | Relational Operators |
if-else Statementsif-else statement ...
if CONDITION:
STATEMENT
STATEMENT
...
else:
STATEMENT
STATEMENT
...
if are runelse are
run
$ cat freezing.py
# this program demonstrates the if_else statement
temp = int(input("Please enter the temperature: "))
if temp <= 32:
print("Water will freeze at this temperature")
else:
print("Water will not freeze at this temperature")
$ python3 freezing.py
Please enter the temperature: 30
Water will freeze at this temperature
$ python3 freezing.py
Please enter the temperature: 35
Water will not freeze at this temperature
ifStatementsif statment has two parts
if-else statement the
else keyword ...
if keyword
>>> if 4 > 3:
... print ("4 is greater than 3")
... print()
File "<stdin>", line 3
print()
^
IndentationError: unexpected indent
if statement will not work ...>>> "mark" < "marker" True
>>> "Sam" == "sam" False
>>> "abc2"> "abc1" True >>> "abc@" < "abc#" False
if Statementsif statements contain code blocksif statement is a statement if statement can contain another if
statement
if statement
if statements occur frequently
$ cat it443.py
# determines whether a student can take IT 443
# demonstrates nested if statements
print("This program will tell you if you can take IT 443")
answer = input("Have you taken IT 244 (y/n)? ")
if answer == "y":
answer = input("Have you taken IT 341 (y/n)? ")
if answer == "y":
print("You can take IT 443")
else:
print("You must take IT 341 before you can take IT 443")
else:
print("You must take IT 244 before you can take IT 443")
$ python3 it443.py
This program will tell you if you can take IT 443
Have you taken IT 244 (y/n)? y
Have you taken IT 341 (y/n)? y
You can take IT 443
$ cat grade.py
# this programs turns a score into a letter grade
# it demonstrates using nested if statements
# to test for many possible conditions
score = int(input("What is the score? "))
if score >= 90:
print("A")
else:
if score >= 80:
print("B")
else:
if score >= 70:
print("C")
else:
if score >= 60:
print("D")
else:
print("F")
$ python3 grade.py
What is the score? 80
Grade B
$ python3 grade.py
What is the score? 60
Grade D
if statementsif-elif-else Statementsif statement would be way off to the rightif statementif-elif-else statement ...
if CONDITION_1:
STATEMENT
...
elif CONDITION_2:
STATEMENT
...
elif CONDITION_3
STATEMENT
...
[else:
STATEMENT
...]
else clause
indicates that it is optional
elif is short for "else if"if-elif-else statement
$ cat grade3.py
# this program turns a score into a letter grade
# it demonstrates the if_elif_else statement
score = int(input("What is the score? "))
print("Grade", end=" ")
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
elif score >= 60:
print("D")
else:
print("F")
$ python3 grade3.py
What is the score? 74
Grade C
else clause ...
$ cat grade4.py
# this program turns a score into a letter grade
# it demonstrates the if_elif_else statement
# without a final else clause
score = int(input("What is the score? "))
grade = "F"
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
print("The grade is", grade)
$ python3 grade4.py
What is your score? 44
Your grade is F
else clause