if Statementsif-else Statementsif-else Statementif 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.
Write your name clearly at the top.
When you finish the Quiz hand it to me.
Then you can work on the Class Exercise and today's ungraded quiz.
print statements
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 statementif statement contains other statements inside it
if CONDITION:
STATEMENT
STATEMENT
...
if statement is an
expression
boolTrueFalse
>>> 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 work we need to create
boolean expressions
| 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 statement will run the indented statements that follows
the first line ...
if-else statement,
if CONDITION:
STATEMENT
STATEMENT
...
else:
STATEMENT
STATEMENT
...
if
clause are executed
else clause are
executed
$ 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
if-else Statementelse in an if-else statement ...if keyword
>>> if 4 > 3:
... print ("4 is greater than 3")
... print()
File "<stdin>", line 3
print()
^
IndentationError: unexpected indent
>>> "mark" < "marker" True
>>> "Sam" == "sam" False
>>> "abc2"> "abc1" True >>> "abc@" < "abc#" False
if Statementsif statements contain code blocksif 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 your score? "))
print("Grade", end=" ")
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 your score? 80
Grade B
$ python3 grade.py
What is your 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 your 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 your 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 your score? "))
grade = "F"
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
print("Your grade is", grade)
$ python3 grade4.py
What is your score? 44
Your grade is F
else clause