Chapter Review Questions

Chapter Review Questions#

Note

Source: Adapted from the C# edition (decisions/reviewdecisions.rst). Questions 7–10 are original additions.

  1. Two similar-looking operators.

    1. What does = do in Python?

    2. What does == do?

    3. Give a one-line example of each.

  2. Python’s Boolean operators.

    1. Name all three.

    2. How do they differ from the C# equivalents (&&, ||, !)?

  3. What is printed?

    x = 7
    if x > 5:
        print("big")
    if x > 10:
        print("very big")
    else:
        print("not very big")
    
  4. Short-circuit evaluation.

    1. What does short-circuit evaluation mean?

    2. Give an example where it prevents a runtime error.

  5. Rewrite this nested if using a single condition with and:

    if age >= 18:
        if has_id:
            print("Entry allowed.")
    
  6. What is the bug in this code? Fix it.

    score = 85
    if score >= 90:
        grade = "A"
    if score >= 80:
        grade = "B"
    if score >= 70:
        grade = "C"
    print(grade)
    
  7. Write a function absolute_value(x) using an if/else statement that returns the absolute value of x without using abs().

  8. Write a function sign(x) that returns 1 if x > 0, -1 if x < 0, and 0 if x == 0.

  9. Write a function is_leap_year(year) that returns True if year is a leap year. A year is a leap year if it is divisible by 4, except centuries (divisible by 100) are not leap years, unless they are also divisible by 400.

  10. What does this expression evaluate to, and what type does it produce?

    "pass" if 75 >= 60 else "fail"