Skip to content

Aug 28: Formatted Strings; Math: Division and Modulo

Learning Objectives

After today's class, you should be able to:

  • Explain differences between integer and floating-point numbers.
  • Write an f-string with multiple expressions and format specifiers.
  • Evaluate mathematical expressions similar to a calculator.
  • Identify and justify the precedence of arithmetic operators.
  • Describe the function of Python's three division operators.

Lesson Outline

Hoemework Debrief

Are you tired of getting emails from Gradescope?

Some highlights:

  • Inputting an integer: int(input())
  • Escape sequences: \t, \", \\
  • Separate calculations and output

Options at the end of print():

  • sep= separator (default: space)
  • end= ending (default: newline)

F-strings

Example programs

welcome.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
"""Display a welcome message for the website.

Author: CS149 instructors
Version: 08/28/2025
"""

course = input("Enter the course: ")
semester = input("Enter the semester: ")

print(f"Welcome to {course}, {semester}!")

Example format specifiers (after the colon):

numbers.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""Example format specifications using f-strings.

Author: CS149 instructors
Version: 08/28/2025
"""

number = int(input("Please enter an integer: "))

print()
print(f"In decimal (base 10), the number is {number:d}.")
print(f"In binary (base 2), the number is {number:b}.")
print(f"In hexadecimal (base 16), the number is {number:x}.")
print()

number = int(input("Integer between 0 and 99: "))

print()
print(f"Five chars wide, padded with spaces: {number:5d}")
print(f"                  padded with zeros: {number:05d}")
print()

number = int(input("Enter a 7+ digit integer: "))

print()
print(f"With comma separators: {number:,d}")
print()

number = float(input("Enter a floating-point number: "))

print()
print(f"In exponent notation, the number is {number:e}.")
print(f"In fixed-point notation, the number is {number:f}.")
print(f"Rounded to two decimal places: {number:.2f}")
print()

Mini Lecture

POGIL Activity

  • Arithmetic Expressions
    • If you are absent today, complete this activity at home
    • Bring your completed activity to class or office hours

Helpful Video

Please watch this 10-minute video recorded by Dr. Sprague in Spring 2022. The video explains how to interpret the (often confusing) feedback on Gradescope. The video also walks through an in-class exercise that you might see next week.

Your To-Do List

  • Due Friday

  • Due Tuesday Sept 2nd by 3pm

  • Due Wednesday Sept 3rd by 11:00pm

    • Complete and submit HW 2 – ideally:
      • 1 and 2 before Friday
      • 3 and 4 before Tuesday
    • Recall you need to go through Canvas to submit all parts of the homework