if Statementsif-else Statementsif-else Statementif Statementsif-elif-else StatementsAre there any questions before I begin?
The UMB IT Club and Boston Linux User Group will hold a Linux InstallFest on Wednesday, September 24th, from 4 to 8 in the University Hall Room 3-3540.
If you have a machine on which you would like to install Linux and would like some help in doing this, bring it the InstallFest.
Volunteers from the Boston Linux User Group with be on hand to help with the installation.
They will also help you install the Window Subsystem for Linux (WSL) on your machine or install Linux as a dual boot.
You can also bring your questions about Linux or Unix to the InstallFest.
The Boston Linux and Unix User Group counts among its members some of the most knowledgeable Linux and Unix people in the Boston area.
You can connect to Gradescope to take weekly graded quiz today during the last 15 minutes of the class.
Once you start the quiz you have 15 minutes to finish it.
You can only take this quiz today.
There is no makeup for the weekly quiz.
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.
print statements
print("Hello")
print("Hello world")
| \n | Causes output to be advanced to the next line |
| \t | Causes output to skip over to the next horizontal tab position |
| \' | Causes a single quote mark to be printed |
| \" | Causes a double quote mark to be printed |
| \\ | Causes a backslash character to be printed |
>>> 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
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