Outline for October 6, 2025

Reading: §2, 5, 6.1–6.8
Assignments: Homework 1, due October 15, 2025

  1. Decision structures [if0.py]
    1. If statement
    2. Executes once, based on condition
    3. Syntax

  2. Conditions
    1. Resolves to boolean value
    2. Literal booleans: True (1), False (0)
    3. Testable as true or false
    4. Relational operators
      1. Use two arithmetic expressions connected with relational operators to create a boolean
      2. Relational operators: >, >=, <, <=, ==, !=
      3. Precedence: resolved after arithmetic operators
      4. 6 > 2 + 3; "UCD" == "Sac State"

  3. Two-way decisions [if1.py]
    1. if … else statements
    2. One condition, two possible code blocks
    3. Syntax
    4. else very powerful when the positive condition is easy to describe but not the negative
    5. String comparison example

  4. Multi-way decisions [if2.py]
    1. Can execute code based on several conditions
    2. elif (else if)
    3. Syntax
    4. else only reached if all previous conditions false
    5. Nested if statements

  5. Conditional expressions [condexp.py] % book 2, sec 19.1

  6. Iteration
    1. Definite loops: execute a specific (definite) number of times
    2. Indefinite loops: execute until a general condition is false

  7. For loops
    1. General form: for i in iterator
    2. Iterator is either list or something that generates a list
    3. Very common form: for i in range(1, 10)

  8. While loops [while.py]
    1. Contrast with for

  9. range() in detail [for.py]
    1. range(10) gives 0 1 2 3 4 5 6 7 8 9
    2. range(3, 10) gives 3 4 5 6 7 8 9
    3. range(2, 10, 3) gives 2 5 8
    4. range(10, 2, -3) gives 10 7 4

  10. continue and break statements in loops [loop1.py]

  11. Exception Keyboard Interrupt — user hit the interrupt key (usually control-C)

  12. Program: counting to 10 [toten.py]

  13. Program: sum the first 10 squares [sumsq.py]

  14. Program: Fibonacci numbers [fib.py]

  15. import statement
    1. import math [hypotnoex.py]
    2. Need the “math.” before “sqrt”
    3. from math import sqrt [hypotnoex1.py]
    4. Do not need the “math.” before “sqrt”
    5. Now add in exception handling [hypotex.py]

  16. Full version of the hypotenuse program [pythag1.py]

  17. Exception ValueError — built-in function or operation applied to operator with illegal value

  18. Functions [hello.py]
    1. What functions are
    2. Defining them
    3. Using them

  19. Quick look at using them [quad.py]
    1. Passing values to functions
    2. Returning values from functions

  20. In more detail: passing values to functions [args.py]
    1. Formal parameters in subject definition
    2. Actual parameters (arguments)
    3. Matching arguments to formal parameters
    4. Local variables

  21. In more detail: how Python does function calls [quad.py]
    1. Caller suspends execution at point of call, remembers where it left off
    2. Formal parameters assigned values from actual parameters
    3. Execute function body
    4. Return control to where caller left off

  22. Refactoring code
    1. Compute the perimeter of a triangle [peri0.py]
    2. Collapse similar statements: make the distance between 2 points a function [peri1.py]
    3. Collapse similar statements: make the prompts a function [peri2.py]
    4. Refactor for clarity only: make the perimeter computation a function [peri3.py]
    5. Add error checking: “peri0.py” done right [peri-c.py]

  23. Add error checking: “quad.py” done right [quad-c.py]

  24. Sequences
    1. Sequences are a series of values in a particular order
    2. In Python predominantly strings and lists but also sets and tuples

  25. Strings
    1. Sequence of characters (characters are strings of length 1)
    2. Strings are immutable; really important for functions

  26. Basic string operations
    1. +, concatenation for strings
    2. *, repetition repeats given value
    3. len() returns length of sequence
    4. s in str returns True if s is a substring of str, False otherwise

  27. Indexing, var[position]
    1. Count from 0 to len(var)-1
    2. Position can be a negative number to count from right

  28. Assignment with indexing doesn’t work as strings immutable
    x = ’hEllo’; x[1] = ’e’ produces an error

  29. Slicing, var[start:end]
    1. Value at index end not included in slice
    2. If omitted, starting value defaults to 0 and ending value defaults to last index + 1
    3. Can use negative index

  30. Looping over strings: for i in str

  31. Example program [strstuff.py]


UC Davis sigil
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.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh