Outline for October 6, 2025
Reading:
§2, 5, 6.1–6.8
Assignments:
Homework 1, due October 15, 2025
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
Conditional expressions [
condexp.py
] % book 2, sec 19.1
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)
While loops [
while.py
]
Contrast with
for
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
continue
and
break
statements in loops [
loop1.py
]
Exception
Keyboard Interrupt
— user hit the interrupt key (usually control-C)
Program: counting to 10 [
toten.py
]
Program: sum the first 10 squares [
sumsq.py
]
Program: Fibonacci numbers [
fib.py
]
import
statement
import math
[
hypotnoex.py
]
Need the “math.” before “sqrt”
from math import sqrt
[
hypotnoex1.py
]
Do not need the “math.” before “sqrt”
Now add in exception handling [
hypotex.py
]
Full version of the hypotenuse program [
pythag1.py
]
Exception
ValueError
— built-in function or operation applied to operator with illegal value
Functions [
hello.py
]
What functions are
Defining them
Using them
Quick look at using them [
quad.py
]
Passing values to functions
Returning values from functions
In more detail: passing values to functions [
args.py
]
Formal parameters in subject definition
Actual parameters (arguments)
Matching arguments to formal parameters
Local variables
In more detail: how Python does function calls [
quad.py
]
Caller suspends execution at point of call, remembers where it left off
Formal parameters assigned values from actual parameters
Execute function body
Return control to where caller left off
Refactoring code
Compute the perimeter of a triangle [
peri0.py
]
Collapse similar statements: make the distance between 2 points a function [
peri1.py
]
Collapse similar statements: make the prompts a function [
peri2.py
]
Refactor for clarity only: make the perimeter computation a function [
peri3.py
]
Add error checking: “peri0.py” done right [
peri-c.py
]
Add error checking: “quad.py” done right [
quad-c.py
]
Sequences
Sequences are a series of values in a particular order
In Python predominantly strings and lists but also sets and tuples
Strings
Sequence of characters (characters are strings of length 1)
Strings are immutable; really important for functions
Basic string operations
+
, concatenation for strings
*
, repetition repeats given value
len()
returns length of sequence
s in str
returns
True
if
s
is a substring of
str
,
False
otherwise
Indexing,
var[position]
Count from 0 to
len(var)
-1
Position can be a negative number to count from right
Assignment with indexing doesn’t work as strings immutable
x = ’hEllo’; x[1] = ’e’
produces an error
Slicing,
var[start:end]
Value at index end not included in slice
If omitted, starting value defaults to 0 and ending value defaults to last index + 1
Can use negative index
Looping over strings:
for i in str
Example program [
strstuff.py
]
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email:
mabishop@ucdavis.edu
MHI 289I, Programming in Health Informatics
Version of October 1, 2025 at 3:54PM
You can also obtain a PDF version of this.