Swapping Values

Fill the table showing the values of the variables in this program after each statement is executed.

Command Value of x Value of y Value of swap
x = 1.0      
y = 3.0      
swap = x      
x = y      
y = swap      
Show answer
Command Value of x Value of y Value of swap
x = 1.0 1.0 not defined not defined
y = 3.0 1.0 3.0 not defined
swap = x 1.0 3.0 1.0
x = y 3.0 3.0 1.0
y = swap 3.0 1.0 1.0

These three lines exchange the values in x and y using the swap variable for temporary storage. This is a fairly common programming idiom.

Predicting Values

What is the final value of position in the program below? (Try to predict the value without running the program, then check your prediction.)

initial = 'left'
position = initial
initial = 'right'
Show answer

The final value of position is 'left'.

The initial variable is assigned the value ‘left’. In the second line, the position variable also receives the string value ‘left’. In third line, the initial variable is given the value ‘right’, but the position variable retains its string value of ‘left’.

Choosing a Name

Which is a better variable name, m, min, or minutes? Why?

Hint: think about which code you would rather inherit from someone who is leaving the lab:

ts = m * 60 + s
tot_sec = min * 60 + sec
total_seconds = minutes * 60 + seconds
Show answer

minutes is better because min might mean something like “minimum” (and actually does in Python, but we haven’t seen that yet).


See the notebook.

Back to Python part one.