Chapter Review Questions

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.

  1. Python has two main numeric types.

    1. Name them.

    2. How does / differ from //?

  2. In Python, do you need to declare a variable before using it? What happens if you use a variable that has never been assigned?

  3. Consider input().

    1. What type does it always return?

    2. Why does that matter when you want to do arithmetic with user input?

  4. Consider these two lines:

    print("The total is " + total)
    print(f"The total is {total}")
    
    1. Which one will cause a TypeError if total is an integer?

    2. Explain why.

  5. f-strings.

    1. What is an f-string?

    2. Write an f-string that prints a float variable price formatted to exactly 2 decimal places.

  6. The built-in type() function.

    1. What does type(x) tell you?

    2. Give an example of a situation where you might call it.

  7. What is None in Python? Give one situation where it appears automatically without the programmer writing it explicitly.

  8. Python is called dynamically typed. What does that mean?

  9. 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\).

  10. What is the value of each expression?

    1. 17 % 5

    2. 17 // 5

    3. 2 ** 8

    4. int(3.9)

    5. str(100) + "00"