# your code hereDay 2 — Optional Exercises
Control flow, loops, lists, tuples, sets
These exercises practice Day 2 material: branching with elif/else, for and while loops, lists, tuples, sets. Plan on 45–60 minutes.
Solutions are in Day_2_Solutions.qmd. Try first; check after.
Exercise 1 — Nested conditionals
A polling place needs to determine what to do with a person who walks in. Write a script using nested if/elif/else that:
- Takes
is_registered(True/False) andat_correct_precinct(True/False) as variables. - Prints
"Allow to vote"if both are True. - Prints
"Redirect to correct precinct"if registered but at the wrong precinct. - Prints
"Provisional ballot"if not registered.
Test all three scenarios.
Exercise 2 — for loop with a list
Given a list of US presidents:
presidents = ["Washington", "Adams", "Jefferson", "Madison", "Monroe"]- Write a
forloop that prints each president’s name preceded by"President ". - Write a second
forloop that uses a counter variable to print each president with their number, like"#1: Washington".
# your code hereExercise 3 — while loop with a condition
Write a while loop that finds the smallest number greater than 100 that is divisible by both 7 and 11.
(Hint: use the modulo operator % and start at 101.)
# your code hereExercise 4 — Nested while loops
Presidential elections happen every four years, in years divisible by 4 (2020, 2024, 2028, …). Write a nested while loop that, for each year from 2020 through 2030:
- Counts how many years it takes to reach the next presidential election year.
- Prints a sentence like
"2021 reaches a presidential election year (2024) in 3 years."
Years that are already election years should report 0 years.
(Hint: the outer loop iterates years 2020–2030. The inner loop counts steps until the year is divisible by 4. Look back at the multiples-of-13 example in the slides if you’re stuck.)
# your code hereExercise 5 — List operations
Given:
states = ["Ohio", "Florida", "Pennsylvania", "Wisconsin", "Michigan"]- Add
"Arizona"to the end of the list (use a list method, not reassignment). - Insert
"Georgia"at position 2. - Remove
"Florida"from the list. - Sort the list alphabetically.
- Print the final list and its length.
# your code hereExercise 6 — Iterating over two lists
You have a list of senators and their years of service:
senators = ["Sanders", "Warren", "Cruz", "Cornyn", "Schumer"]
years = [34, 12, 13, 23, 26]Write a for loop that iterates over both lists by index, and prints each senator’s name with their years served — but only if they’ve served more than 15 years.
(Hint: use range(len(senators)) to get an index, then access each list with [i].)
# your code hereExercise 7 — Sets
You have two lists of co-sponsors on two pieces of legislation:
bill_a_sponsors = ["Sanders", "Warren", "Klobuchar", "Booker", "Brown"]
bill_b_sponsors = ["Warren", "Booker", "Markey", "Whitehouse", "Klobuchar"]Using sets:
- Find the senators who co-sponsored both bills.
- Find the senators who co-sponsored bill A but not bill B.
- Find the total number of unique senators across both bills.
# your code hereExercise 8 — Putting it together
A short data-cleaning task. You have a list of vote counts that includes some -1 values (meaning “missing data”):
vote_counts = [4250, 3980, -1, 5120, 2890, -1, 6400, 4100]Write a script that:
- Uses a
forloop to compute the mean of only the valid (non-missing) vote counts. - Counts how many missing values there were.
- Prints both numbers in a single f-string sentence, e.g.,
"Mean: 4456.7 (3 missing values dropped)".
# your code here