Learning Objectives
Through readings, lecture and labs, it is expected that you are doing work and seeking help to keep pace with the below objectives:
- Indefinite (
while
) loops - You should be comfortable with:
- Tracing the execution of programs using a
while
loop - Writing sample
while
loops - Deciding when to use
for
vs.while
- Tracing the execution of programs using a
- Strings
- You should be comfortable with:
- Accessing individual characters of a string using indexing (e.g.
name[1]
) - Using the concatenation (
+
) and repetition (*
) operators on strings - Determining and using the length of a string
- Writing a loop to access each character of a string
- Accessing substrings using string slicing
- Using list member functions like
s.upper()
,s.lower()
,s.isdigit()
,s.islower()
,s.isupper()
- Accessing individual characters of a string using indexing (e.g.
Required Reading
Supplemental Materials
We all need alternative presentations of the concepts from time to time. The following resources are optional. If something seems unclear after class, or the pratice questions seem hard for you, then explore these topics more using the resources below.
- Indefinite (
while
) loops -
- CS Circles lesson on loops
- Zelle, Sections 8.2, 8.3, 8.5
- Strings
-
- CS Circles lesson on strings
- Zelle, Sections 5.1, 5.2
- More advanced reading: Zelle, Sections 5.4, 5.8.1
Practice
For all these exercises, I recommend you try them on paper and then test them out with PythonTutor.
Exercise 1
Write a while loop that is equivalent to the following for loop:
for i in range(2, 22, 3): print(i)Here is a sample answer, when you think you have solved it. (Don't cheat yourself and look early).
Exercise 2
Write what this code produces, exactly. Be very careful with whitespace and the newlines.
i = 1 w = '' done = False while (not done): if i % 2 == 0: w += 'E' else: w += 'O' if i % 4 == 0: w += '4' if len(w) >= 5: done = True else: i += 1 print(w)
Exercise 3
Write a piece of code that prompts the user (repeatedly) for an integer, and stops when the user enters a negative number. At the point a user enters a negative number, the program prints the sum of all positive numbers entered previously.
Here is a sample answer, when you think you have solved it. (Don't cheat yourself and look early).