Aug 27: Types, Formatted Strings
Learning Objectives
After today's class, you should be able to:
- Create email filters that move unwanted messages into a folder.
- Explain differences between integer and floating-point numbers.
- Write an f-string with multiple expressions and format specifiers.
HW1 Debrief¶
[15 min]
Are you tired of getting emails from Gradescope?
- See instructions on shared website
Some highlights:
- Inputting an integer:
int(input())
- Input with a prompt:
input("Name: ")
Options at the end of print()
:
sep=
separator (default: space)end=
ending (default: newline)
print("Today is Monday.")
print("Monday string beans.")
print("Today", "is", "Monday")
print("Today", "is", "Monday", sep="...")
print("Today is Monday, ", end="")
print("Monday string beans.")
print("Today", "is", "Monday", sep="? ", end="!!")
print("Monday string beans.")
Mini Activity¶
[15 min]
- Integers and Floats
- If you are absent today, complete this activity at home
- Bring your completed activity to class or office hours
Sample code:
integer = 3
type(integer)
type("integer")
pi = 3.1415
type(pi)
word = str(pi)
word
number = float(word)
print(word * 2)
print(number * 2)
print(word + 2)
print(number + 2)
euler = 2.7182
int(euler)
round(euler)
F-Strings¶
[15 min]
Example program from the front page:
welcome.py | |
---|---|
1 2 3 4 5 6 7 8 9 10 |
|
Example format specifiers (the part 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 |
|
Exercise
Write a program that prompts the user for two integers and displays their product. The output must match the example below (with user input as shown). Your solution may assign only two variables. You must use an f-string for the last line of output.
Enter a number: 2
Enter another number: 3
2 times 3 equals 6
Reminders¶
[5 min]
- Due Tonight
- Syllabus Quiz on Canvas (must get 5/5 = 100%)
- HW1 Reflection on Canvas
- Read Week 2 – ideally:
- 1st half before Friday
- 2nd half before Monday
- Submit HW 2 – ideally:
- 1 and 2 before Friday
- 3 and 4 before Monday