# your code hereDay 1 — Optional Exercises
Variables, types, strings, booleans, if statements
These exercises practice the material from Day 1. These are option, as there are no grades, but working through them is the fastest way to build fluency.
Open a new Colab notebook (or use the one from class) and try each exercise. Solutions are in a separate document.
Exercise 1 — Variables and types
Create three variables:
state, the string"Michigan"population, the integer10_077_331(Michigan’s 2020 population)electoral_votes, the integer15
Use the type() function to confirm the type of each variable. Print the type of all three on three separate lines.
Exercise 2 — Type conversion
A polling firm reports a candidate’s vote share as the string "52.3".
- Assign that string to a variable called
vote_share_str. - Convert it to a float and assign it to
vote_share. - Compute the margin of victory (assume the other candidate got the rest of the two-party vote) and print it formatted as a number.
# your code hereExercise 3 — String indexing and slicing
Given the string name = "Senator Bernie Sanders":
- Use indexing to print the first character.
- Use slicing to print just
"Bernie". - Use slicing to print just
"Sanders". - Use slicing to print the last 7 characters (without counting).
name = "Senator Bernie Sanders"
# your code hereExercise 4 — f-strings
You have these variables describing a hypothetical election:
candidate = "Smith"
party = "Democratic"
vote_share = 0.523
state = "Ohio"Write a single print() statement using an f-string that produces output like:
Smith (Democratic) won Ohio with 52.3% of the vote.
(Hint: f"{vote_share:.1%}" formats a decimal as a percentage with one decimal place.)
# your code hereExercise 5 — Boolean expressions
For each pair below, predict what the expression returns, then run it to check.
# Predict, then run
print(5 == 5.0)
print("Democrat" == "democrat")
print(60 > 50 and 60 < 70)
print(not (50 > 100))
print(0.49 + 0.51 == 1.0) # the last one is sneakyWhat’s going on with the last expression? (Write a one-sentence answer in a comment.)
Exercise 6 — if / else
Write a script that:
- Asks the user to enter a turnout percentage (use
input()and convert to a float). - Prints
"High turnout"if the value is greater than 60. - Prints
"Moderate turnout"if the value is between 40 and 60 (inclusive). - Prints
"Low turnout"if the value is below 40.
# your code hereExercise 7 — Putting it together
You have a list of three candidates and their vote totals from a hypothetical primary:
candidate_a = "Adams"
candidate_b = "Burr"
candidate_c = "Carroll"
votes_a = 4_125
votes_b = 3_980
votes_c = 1_245Write a script that:
- Computes the total number of votes cast.
- Computes each candidate’s percentage of the total.
- Prints each candidate’s name and percentage formatted to one decimal place (use an f-string).
- At the end, prints
"Adams wins!"if Adams’s share is greater than 50%, otherwise prints"Runoff required.".
# your code hereWhen you’re done, open Day_1_Solutions.qmd (or the rendered HTML) to compare. My way may not be the only, or best way, so as long as your code runs and makes sense, you’re good to go!