Lecture 2 - Exercises

2A - Data Types

  1. What is the result of each of the following function calls?
    1. type(7.0)
    2. type("7.0")
    3. type(7)
    4. float(7)
    5. str(7.0)
    6. type(3+4)
  2. If you pass a positive float argument to the int function, which of the following does it do?

2B - Variables and Assignment

  1. Which of the following does not print the same thing as the others?

    Program A:

    Program B:

    Program C:

    Program D:

  2. Consider the following program:

    After this program executes, what values do a and b hold?

  3. What does the following program print?

Problem

Suppose that you are writing a program and you have two variables x and y that contain values. You’d like to swap the values stored in those variables, so what was previously stored in x is now stored in y and what was stored in y is now in x. For example if the following program is completed with your code, it should output 4 10. Note: your solution should not depend on the specific values (e.g., 4 and 10) - it should work regardless of what x and y are set to.

x = 10
y = 4
# your code here
print(x, y)