Python · basics

Variables & Assignment

Give a value a name with =, then change it, share it and swap it.

Basic assignment


          # variable assignment
          x = 5
          # ans: x is 5
          name = "Alice"
          # ans: name is "Alice"
          pi = 3.14
          # ans: pi is 3.14
        

Read = as "store the value on the right under the name on the left", not as "equals".

Multiple assignment


          # assign multiple variables
          a, b, c = 1, 2, 3
          # ans: a=1, b=2, c=3
          # what are the values?
          x, y = 10, 20
          # ans: x=10, y=20
        

Values on the right are matched to names on the left by position.

Swapping with a temp variable


          # traditional swap needs a temp variable
          x, y = 5, 10
          temp = x
          x = y
          y = temp
          # ans: x=10, y=5
        

Without temp, x = y would overwrite the 5 before anything saved it.

Swapping the Python way


          # python swap
          x, y = 5, 10
          x, y = y, x
          # ans: x=10, y=5
        

Python evaluates the whole right side first, (10, 5), and only then assigns.

Chained assignment


          # assign same value to multiple variables
          x = y = z = 0
          # ans: x=0, y=0, z=0
          a = b = c = "same"
          # ans: all are "same"
        

Exercises

Predict each answer, then press → to run the code and check.

Exercises · part 1


          # what is the value of x?
          x = 10
          # ans: 10
          # what happens after this swap?
          a, b = 5, 15
          a, b = b, a
          # ans: a=15, b=5
          # what is z after this?
          x, y, z = 1, 2, 3
          z = x + y
          # ans: 3
          # can you assign like this?
          x = y = z = 0
          # ans: yes, all are 0
        

Exercises · part 2


          # what is the value of b?
          a = 5
          b = a
          a = 10
          # ans: 5
          # what does this produce?
          x, y = 1, 2, 3
          # ans: Error: too many values to unpack
          # what is y?
          x = y = 5
          x = x + 1
          # ans: 5
        

Exercises · part 3


          # multiple assignment with calculation?
          a, b = 10, 20
          a, b = a+b, a-b
          # ans: a=30, b=-10
          # what happens here?
          x = 1
          x = x
          # ans: x is still 1
          # tricky swap
          x, y = 5, 10
          x, y = y, x+y
          # ans: x=10, y=15
        

Exercises · part 4


          # assignment from right to left?
          a = b = 3
          b = 5
          # ans: a=3, b=5
          # what is x?
          x = 10
          # ans: 10
          # unpacking with extra values?
          x, y = [1, 2, 3]
          # ans: Error: too many values
        

Exercises · part 5


          # what about this?
          a, b, c = "abc"
          # ans: a='a', b='b', c='c'
          # swap three variables?
          x, y, z = 1, 2, 3
          x, y, z = z, x, y
          # ans: x=3, y=1, z=2
          # empty assignment?
          x, y = [], []
          # ans: x=[], y=[]
        

Remember

= stores

The name on the left now refers to the value on the right; the old value is forgotten.

Right side first

The whole right side is evaluated before anything is assigned, which is why x, y = y, x swaps.

Counts must match

Unpacking needs one name per value, or Python raises ValueError.

Copies of the value

b = a gives b the value of a now; changing a later does not change b.