Outline for October 13, 2022

Reading: §4, 6
Assignment: Homework 2, due October 20, 2022


  1. 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

  2. String methods: methods that change, add, or delete characters do not alter the string to which they are applied; they return a new string that is a copy of the old string, suitably modified

  3. String methods: type of characters in string (return True or False) [strtype.py]
    1. S.isalpha()True if only alphabetics (letters) in S
    2. S.isalnum()True if only alphanumerics (letters or digits) in S
    3. S.isdigit()True if only digits in S
    4. S.isspace()True if only white space (blanks, tabs, newlines) in S
    5. S.isupper()True if all letters in S are upper case
    6. S.islower()True if all letters in S are lower case

  4. String methods: changing case of letters in string (return result of applying method) [strchcase.py]
    1. S.capitalize() — If the first character of S is a letter, capitalize it
    2. S.title() — Capitalize each word in S
    3. S.lower() — Change all upper case letters in S to lower case
    4. S.upper() — Change all lower case letters in S to upper case
    5. S.swapcase() — Change all upper case letters in S to lower case and vice versa

  5. String methods: stripping blanks from strings (return result of applying method) [strstrip.py]
    1. S.lstrip() — Delete all leading white spaces from S
    2. S.rstrip() — Delete all trailing white spaces from S
    3. S.strip() — Delete all leading and trailing white spaces from S

  6. String methods: find characters and substrings (return position or cause exception) [strfind.py]
    1. S.find(s) — Return the index of the first occurrence of s in S; -1 if s not in S
    2. S.index(s) — Return the index of the first occurrence of s in S; ValueError exception if s not in S
    3. S.rfind(s) — Return the index of the last occurrence of s in S; -1 if \sstr\ not in S
    4. S.rindex(s) — Return the index of the last occurrence of s in S; ValueError exception if s not in S

  7. String methods: miscellaneous [strmisc.py]
    1. S.count(s) — Return the number of times s occurs in S
    2. S.startswith(s)True if S starts with s
    3. S.endswith(s)True if S ends with s
    4. S.replace(s,t) — Replace all occurrences of s with t in S

  8. Lists and strings [datecvt.py]

  9. Program to print words in a line [lines.py]

  10. Example of sets [sets.py]

  11. What you can do with lists
    1. Check membership: in, not in
    2. +: concatenation
    3. *: repetition
    4. list[a:b]: slice list from a to b-1
    5. del list[i]: delete element list[i]; i can be a slice
    6. Add elements to, remove elements: L.append(x), L.extend(ls), L.insert(i, x), L.pop(), L.remove(x)
    7. Element ordering: L.reverse(), L.sort()
    8. Other: L.count(x), L.index(x)

  12. Searching a list
    1. Example use: linear search [linsearch.py]

  13. Lists as parameters: can change list elements in function and they are changed in caller [args2.py]

  14. More on parameters: named arguments and variable number of arguments [args3.py]

  15. isinstance(obj,type) function
    1. type is bool, float, int, list, str, tuple

  16. Recursion
    1. n factorial [nfact.py]

  17. Thinking recursively [recfun.py]
    1. First: think of the recursive case (write the problem in terms of something involving a smaller instance of the problem)
    2. Next: think of base case (when to stop)
    3. Example: Find the length of a string
    4. Example: Does the string only have alphabetic characters in it?
    5. Example: Find the maximum element of a list
    6. Example: Construct a string from a list of strings
    7. Example: Reverse a string

  18. Recursion
    1. Palindromes [palindrome.py]
    2. Fibonacci numbers [rfib.py]
    3. Sum of digits [sumdigits.py]
    4. Greatest common divisor [gcd.py]
    5. Nested lists: is an item in a list? [isinlist.py]
    6. Tower of Hanoi [hanoi.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 13, 2022 at 9:46AM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh