Exercises#

  1. For each of the following expressions, what value will the expression give? Verify your answers by typing the expressions into Python.

    1. 9 - 3

    2. 8 * 2.5

    3. 9 / 2

    4. 9 / -2

    5. 9 // -2

    6. 9 % 2

    7. 9.0 % 2

    8. 9 % 2.0

    9. 9 % -2

    10. -9 % 2

    11. 9 / -2.0

    12. 4 + 3 * 5

    13. (4 + 3) * 5

  2. Unary minus negates a number. Unary plus exists as well; for example, Python understands +5. If x has the value -17, what do you think +x should do? Should it leave the sign of the number alone? Should it act like absolute value, removing any negation? Use the Python shell to find out its behavior.

  3. Write two assignment statements that do the following:

  4. Create a new variable, temp, and assign it the value 24.

  5. Convert the value in temp from Celsius to Fahrenheit by multiplying by 1.8 and adding 32; make temp refer to the resulting value. What is temp’s new value?

  6. For each of the following expressions, in which order are the subexpressions evaluated?

  • 6 * 3 + 7 * 4

  • 5 + 3 / 4

  • 5 - 2 * 3 ** 4

  1. Exercise Create a new variable x, and assign it the value 10.5. Create a new variable y, and assign it the value 4. Sum x and y, and make x refer to the resulting value. After this statement has been executed, what are the values of x and y?

  2. Write a bullet list description of what happens when Python evaluates the statement x += x - x when x has the value 3.

  3. When a variable is used before it has been assigned a value, a NameError occurs. In the Python shell, write an expression that results in a NameError.

  4. Which of the following expressions results in SyntaxErrors?

  • 6 * ———--8

  • 8 = people

  • ((((4 ** 3))))

  • (-(-(-(-5))))

  • 4 += 7 / 2