This language has no
switch
statement.
if
statements in the Python
Tutorial
if
statements in the Python
Language Reference
Comparison for equality must be performed with a
double
=
,
not a
single
=
.
No space between the two
=
’s.
How many electoral votes did Trump get? 304 How many electoral votes did Clinton get? 232 There's a clear winner. Thank you, electorate
How many electoral votes did Trump get? 268 How many electoral votes did Clinton get? 268 They're tied. Please inform the House of Representatives that the Electoral College is hung.
The previous script contained two consecutive,
mutually exclusive
if
statements.
Consecutive
means one comes immediately after the other.
Mutually
exclusive
means that
one of the two
if
’s
will be true, and one will be false.
Here are the five mutually exclusive pairs of
operators that return
True
or
False
.
We have seen the first three of these five.
Remember, the opposite of
“less than”
is not
“greater than”.
The opposite of
“less than”
is
“greater than or equal to”.
== |
< |
> |
in |
|
!= |
>= |
<= |
not in |
|
Please use the following
“else
”
abbreviation in place of a pair of consecutive,
mutually exclusive
if
statements.
else
means
“otherwise”.
It steers the computer in one of two possible directions.
For a given input, the output is the same as in the previous example.
How many electoral votes did Trump get? 304 How many electoral votes did Clinton get? 232 There's a clear winner. Thank you, electorate
How many electoral votes did Trump get? 268 How many electoral votes did Clinton get? 268 They're tied. Please inform the House of Representatives that the Electoral College is hung.
Strand example in Exit Status.
thenif1.py
(performance bug)
How much were our receipts? 100 How much were our expenditures? 100 We're not making any money.
How much were our receipts? 100 How much were our expenditures? 200 We're not making any money. In fact, we're losing money.
For a given input, the output is the same as in the previous example.
How much were our receipts? 100 How much were our expenditures? 100 We're not making any money.
How much were our receipts? 100 How much were our expenditures? 200 We're not making any money. In fact, we're losing money.
Let’s run
thenif2.py
again.
With the following input, we get dead silence.
And that’s correct.
How much were our receipts? 100 How much were our expenditures? 20
What would happen if we forgot the two statements that call the
int
function?
In alphabetical order,
the string
"20"
is greater than (i.e., comes after)
the string
"100"
because the character
"2"
is greater than (i.e., comes after)
the character
"1"
.
How much were our receipts? 100 How much were our expenditures? 20 We're not making any money. In fact, we're losing money.
See the
Teaser
for an example of nested
if
s.
elseif1.py
(performance bug)
How much were our receipts? 200 How much were our expenditures? 100 We're making money.
How much were our receipts? 100 How much were our expenditures? 100 We're not making any money.
How much were our receipts? 100 How much were our expenditures? 200 We're not making any money. In fact, we're losing money.
For a given input, the output is the same as in the previous example.
How much were our receipts? 200 How much were our expenditures? 100 We're making money.
How much were our receipts? 100 How much were our expenditures? 100 We're not making any money.
How much were our receipts? 100 How much were our expenditures? 200 We're not making any money. In fact, we're losing money.
Temporarily wrap
a
while
loop around the part of the script that performs the input and output.
With
if/else
,
we can steer the computer in one of two possible directions.
With
if/elif/else
,
we can steer the computer in one of three possible directions.
Think of this
else
as meaning
“if none of the above”
or
“as a last resort”.
How much were our receipts? 100 How much were our expenditures? 200 We're losing money.
How much were our receipts? 100 How much were our expenditures? 100 We're breaking even.
How much were our receipts? 200 How much were our expenditures? 100 We're making money.
In all of the above receipts/expenditures examples, please change
sys.exit(0)to
if receipts >= expenditures: sys.exit(0) #we're in the black else: sys.exit(1) #we're in the redor to
sys.exit(0 if receipts >= expenditures else 1)
In the macOS Terminal window, the
Bash shell
variable
$?
holds the exit status produced by the most recently run program.
See
?
in the Special Parameters section of the
manual
page
for
bash
.
(The corresponding variable in Microsoft Windows Command Prompt window is
%errorlevel%
.)
Mynames-MacBook-Pro:python myname$ python3 thenif2.py How much were our receipts? 100 How much were our expenditures? 200 We're not making any money. In fact, we're losing money. Mynames-MacBook-Pro:python myname$ echo $? 1
C:\Users\Myname>thenif2.py How much were our receipts? 100 How much were our expenditures? 200 We're not making any money. In fact, we're losing money. C:\Users\Myname>echo %errorlevel% 1
ELSF
ELSE
Steer the computer in one of four possible directions:
if it is possible to remain inside the train: remain inside the train elif you are able to go to next car through end doors: go to next car through end doors elif you are able to open the side door and get out: open the side door and get out else: go out emergency windows
Please type a year: 2000 2000 is a leap year.
Please type a year: 1900 1900 is not a leap year.
Please type a year: 2016 2016 is a leap year.
Please type a year: 2017 2017 is not a leap year.
She loves me,
She loves me not.
The operator
+
and the operator
*
are both adjacent to,
and therefore competing to sink their teeth into, the operand
20
.
Since
*
has higher
precedence
than
+
,
the
20
becomes an operand of the
*
10 + 20 * 30
The operator
or
and the operator
and
are both adjacent to,
and therefore competing to sink their teeth into,
the operand
year % 4 == 0
.
Since
and
has higher
precedence
than
or
,
the
year % 4 == 0
becomes an operand of the
and
.
if year % 400 == 0 or year % 4 == 0 and year % 100 != 0: print(f"{year} is a leap year.") else: print(f"{year} is a not leap year.")
Since
and
has higher
precedence
than
or
,
the above
if
behaves as if we had written
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0): print(f"{year} is a leap year.") else: print(f"{year} is a not leap year.")
For a given input, the output is the same as in the previous example.
import calendar if calendar.isleap(year): print(f"{year} is a leap year.") else: print(f"{year} is not a leap year.")
New York City street names are ordinal numbers. For example, 8th Street or 42nd Street.
Please type a positive whole number: 1 The ordinal form of 1 is 1st.
Please type a positive whole number: 2 The ordinal form of 2 is 2nd.
Please type a positive whole number: 3 The ordinal form of 3 is 3rd.
Please type a positive whole number: 4 The ordinal form of 4 is 4th.
What is the smallest number that the above program handles incorrectly?
Please type a positive whole number: 1 The ordinal form of 1 is 1st.
Please type a positive whole number: 11 The ordinal form of 11 is 11th.
Please type a positive whole number: 12 The ordinal form of 12 is 12th.
Please type a positive whole number: 13 The ordinal form of 13 is 13th.
Please type a positive whole number: 111 The ordinal form of 111 is 111th.
There’s a Python module that does this for you:
pip3 search num2words num2words (0.5.10) - Modules to convert numbers to words. Easily extensible. pip3 install num2words pip3 list num2words --help