if
Statementsif
-else
Statementsif
-else
Statementif
Statementsif
-elif
-else
StatementsYou should read Chapter 4, Repetition Structures, from our textbook, Starting Out with Python, before next week's class.
I have posted homework 4 here.
It is due this coming Sunday at 11:59 PM.
format
function
print("Hello")
print("Hello world")
Escape Character | Effect |
---|---|
\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" >>> print("Let's go", team) Let's go Red Sox >>> cheer = "Let's go " + team >>> print(cheer) Let's go Red Sox
print("The value is" + value)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
str
print("The balance is " + str(balance)) The balance is 1000
if
Statementsif
statementif
statement contains other statements inside it
if CONDITION:
STATEMENT
STATEMENT
...
if
statement must have a value that can only be true or falsebool
True
False
>>> type(True) <class 'bool'> >>> type('true') <class 'str'> >>> type(False) <class 'bool'> >>> type('False') <class 'str'>
bool
bool
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 follow if the condition is trueif
-else
statement,
which has the following format
if CONDITION:
STATEMENT
STATEMENT
...
else:
STATEMENT
STATEMENT
...
if
clause are executedelse
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
Statementif
statement and and if
-else
statement must be indented properlyif
clause and the else
clause must be alignedif
and else
are not exactly aligned
you will get an error
>>> if 4 > 3:
... print("4 is ")
... print("greater than 3")
File "<stdin>", line 3
print("greater than 3")
^
>>> "mark" < "marker" True
>>> "Sam" == "sam" False
>>> "abc2"> "abc1" True >>> "abc@" < "abc#" False
if
Statementsif
statement consists of one or more valid Python statementsif
statement is a valid Python statement so you can have an
if
statement inside 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
-elif
-else
Statementsif
statement would be way off to the rightif
statementif
-elif
-else
statement which has the following format
if CONDITION_1:
STATEMENT
...
elif CONDITION_2:
STATEMENT
...
elif CONDITION_3
STATEMENT
...
...
[else:
STATEMENT
...]
else
clause indicates that it is optionalelif
is short for "else if"if
-elif
-else
statement
$ cat grade2.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 and score < 90: print("B") elif score >= 70 and score < 80: print("C") elif score >= 60 and score < 70: print("D") else: print("F") [551] glenn - ~/workspace-mars/it116/resources_it116/code_it116/examples_it116/3_chapter_examples $ python3 grade2.py What is your score? 74 Grade C
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")
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