Outline for April 29, 2009
Reading
: §7.1–7.3
Guest Lecturer
: Justin Cummins
Decision structures
If statement
Executes once, based on condition
Syntax (added condition to
superkondit.py
)
Conditions
Resolves to boolean value
Literal booleans: True, False
Testable as true or false
d. Relational operators (extended condition in
superkondit.py
)
Use two arithmetic expressions to create a boolean
>, >=, <, <=, ==, !=
Resolved after arithmetic operators
6 < 2; "UCD" == "Sac State"
Python allows all sorts of comparison (even confusing ones)
3. Two-way decisions
If-else statements
One condition, two possible code blocks
Syntax (extended
superkondit.py
)
Else very powerful when the positive condition is easy to describe but not the negative
String comparison example (see
bad_login.py
)
4. Multi-way decisions
Can execute code based on several conditions
elif (else if) introduced
Syntax (extended
superkondit.py
)
elif only reached if all previous conditions false