Day 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 integer 10_077_331 (Michigan’s 2020 population)
  • electoral_votes, the integer 15

Use the type() function to confirm the type of each variable. Print the type of all three on three separate lines.

# your code here

Exercise 2 — Type conversion

A polling firm reports a candidate’s vote share as the string "52.3".

  1. Assign that string to a variable called vote_share_str.
  2. Convert it to a float and assign it to vote_share.
  3. 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 here

Exercise 3 — String indexing and slicing

Given the string name = "Senator Bernie Sanders":

  1. Use indexing to print the first character.
  2. Use slicing to print just "Bernie".
  3. Use slicing to print just "Sanders".
  4. Use slicing to print the last 7 characters (without counting).
name = "Senator Bernie Sanders"
# your code here

Exercise 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 here

Exercise 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 sneaky

What’s going on with the last expression? (Write a one-sentence answer in a comment.)


Exercise 6 — if / else

Write a script that:

  1. Asks the user to enter a turnout percentage (use input() and convert to a float).
  2. Prints "High turnout" if the value is greater than 60.
  3. Prints "Moderate turnout" if the value is between 40 and 60 (inclusive).
  4. Prints "Low turnout" if the value is below 40.
# your code here

Exercise 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_245

Write a script that:

  1. Computes the total number of votes cast.
  2. Computes each candidate’s percentage of the total.
  3. Prints each candidate’s name and percentage formatted to one decimal place (use an f-string).
  4. At the end, prints "Adams wins!" if Adams’s share is greater than 50%, otherwise prints "Runoff required.".
# your code here

When 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!