Outline for October 13, 2020
Reading
: §3, 5
Due
: Homework 1, due October 15, 2020
Simultaneous assignment [
swap.py
]
Simple assignment:
variable
=
expression
Simultaneous assignment:
variableA
,
variableB
=
expressionA
,
expressionB
Decision structures [
if0.py
]
If
statement
Conditions
Resolves to boolean value
Literal booleans:
True
,
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
else very powerful when the positive condition is easy to describe but not the negative
Multi-way decisions [
if2.py
]
Can execute code based on several conditions
elif
(else if)
else
only reached if all previous conditions false
Compare to nested if statements
Iteration
Definite loops: execute a specific (definite) number of times
Indefinite loops: execute until a general condition is false
For loops
General form:
for i in
iterator
Iterator
is either list or something that generates a list
Very common form:
for i in range(1, 10)
range()
in detail [
for.py
]
range(10)
gives 0 1 2 3 4 5 6 7 8 9
range(3, 10)
gives 3 4 5 6 7 8 9
range(2, 10, 3)
gives 2 5 8
range(10, 2, -3)
gives 10 7 4
While loops [
while.py
]
Repeats until condition is false
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email:
mabishop@ucdavis.edu
MHI 289I, Programming for Health Informatics
Version of October 12, 2020 at 4:15PM
You can also obtain a PDF version of this.