Chapter Review Questions#
Note
Source: Adapted from the C# edition data-chapter review questions. Python-specific questions (f-strings, floor division, truthiness) are original additions.
Python has two main numeric types.
Name them.
How does
/differ from//?
In Python, do you need to declare a variable before using it? What happens if you use a variable that has never been assigned?
Consider
input().What type does it always return?
Why does that matter when you want to do arithmetic with user input?
Consider these two lines:
print("The total is " + total) print(f"The total is {total}")
Which one will cause a
TypeErroriftotalis an integer?Explain why.
f-strings.
What is an f-string?
Write an f-string that prints a float variable
priceformatted to exactly 2 decimal places.
The built-in
type()function.What does
type(x)tell you?Give an example of a situation where you might call it.
What is
Nonein Python? Give one situation where it appears automatically without the programmer writing it explicitly.Python is called dynamically typed. What does that mean?
Write a program that reads a temperature in Fahrenheit from the user and prints it in Celsius. The formula is \(C = (F - 32) \times 5 / 9\).
What is the value of each expression?
17 % 517 // 52 ** 8int(3.9)str(100) + "00"