Outline for January 10, 2018
Reading
: §2, 3
Input
input
,
raw_input
try … except
Simultaneous assignment [
swap.py
]
Simple assignment:
variable
=
expression
Simultaneous assignment:
variableA
,
variableB
=
expressionA
,
expressionB
Decision structures [
if0.py
]
If statement
Executes once, based on condition
Syntax
Conditions
Resolves to boolean value
Literal booleans: True (1), False (0)
Testable as
true
or
false
Relational operators
Use two arithmetic expressions connected with relational operators to create a boolean
Relational operators:
>
,
>=
,
<
,
<=
,
==
,
!=
Precedence: resolved after arithmetic operators
6 > 2 + 3
;
"UCD" == "Sac State"
Two-way decisions [
if1.py
]
if … else statements
One condition, two possible code blocks
Syntax
else very powerful when the positive condition is easy to describe but not the negative
String comparison example
. Multi-way decisions [
if2.py
]
Can execute code based on several conditions
elif
(else if)
Syntax
else
only reached if all previous conditions false
Nested if statements
Matt Bishop
Department of Computer Science
University of California at Davis
Davis, CA 95616-8562 USA
Last modified: January 10, 2018
Winter Quarter 2018
You can get a PDF version of this