Variables and assignment statements

The script

variable.py

The
f"The sum of {i} and {j} is {sum}."
is a formatted string literal.

The output

10 20 30
The sum of i and j is sum.
The sum of 10 and 20 is 30.

Variables

A variable is a container that can contain a value. We put a value into a variable by writing an assignment statement. To the left of the equal sign, write the name of the varible. To the right of the equal sign, write the expression whose value should be copied into the variable. A value always travels from right to left through an equal sign.

I’m afraid that the official documentation defines a variable to be a mutable object. This is like Plato’s definition of a man as a featherless biped.

The name of a variable

The name of a variable must begin with a letter (upper or lowercase) or an underscore (_). The remaining characters (if any) in the name can be letters (upper or lowercase), digits, or underscores. Do not use one of the keywords of the language (e.g., import) as the name of a variable. And remember that variable with the funny name __class__? Well, for the time being, don’t start and end your variables names with double underscores. See Identifiers and keywords.

Things to try

  1. We said
    print(f"The sum of {i} and {j} is {sum}.")
    
    The sum of 10 and 20 is 30.
    
    Before they invented formatted string literals, I had to set the print separator to the empty string "" to prevent a space from being output before the period at the end of the sentence.

    print("The sum of ", i, " and ", j, " is ", sum, ".", sep = "")
    

    Other ways to get the same output are

    print("The sum of", i, "and", j, "is", sum, end = "")
    print(".")
    
    print("The sum of " + str(i) + " and " + str(j) + " is " + str(sum) + ".\n")
    
  2. Put values of type string into the variables.
    i = "cup"
    j = "cake"
    compound = i + j
    plural = compound + "s"
    
    print(f"The sum of {i} and {j} is {compound}.")
    print(f"The plural of {compound} is {plural}.")
    
    The sum of cup and cake is cupcake.
    The plural of cupcake is cupcakes.
    
  3. One way to put a double quote into a double-quoted string is to precede the double quote with a backslash.
    i = "cup"
    j = "cake"
    compound = i + j
    plural = compound + "s"
    
    print(f"The sum of \"{i}\" and \"{j}\" is \"{compound}\".")
    print(f"The plural of \"{compound}\" is \"{plural}\".")
    
    The sum of "cup" and "cake" is "cupcake".
    The plural of "cupcake" is "cupcakes".
    
    Another way to put a double quote into a string is to surround the string with 'single quotes'.
    i = "cup"
    j = "cake"
    compound = i + j
    plural = compound + "s"
    
    print(f'The sum of "{i}" and "{j}" is "{compound}".')
    print(f'The plural of "{compound}" is "{plural}".')
    
    The sum of "cup" and "cake" is "cupcake".
    The plural of "cupcake" is "cupcakes".
    
  4. Multiple assignment:
    #Make sure you have the same number of items on each side of the =
    i, j, k = 10, 20, 30
    print(i, j, k)
    
    10 20 30
    
  5. What goes wrong when you try to swap the contents of two variables like this?
    i = 10
    j = 20
    
    #Try to swap the values of i and j.
    i = j
    j = i
    
    print(f"Now i contains {i} and j contains {j}.")
    
    Now i contains 20 and j contains 20.
    

    One way to swap is by using a third variable:

    i = 10
    j = 20
    
    #Swap the values of i and j.
    temp = i
    i = j
    j = temp
    
    print(f"Now i contains {i} and j contains {j}.")
    
    Now i contains 20 and j contains 10.
    

    In Python, a simpler way to swap is by using a multiple assignment:

    i = 10
    j = 20
    
    #Swap the values of i and j.
    #Make sure you have the same number of variables on each side of the =
    i, j = j, i
    
    print(f"Now i contains {i} and j contains {j}.")
    
    Now i contains 20 and j contains 10.
    
  6. Three ways to give the same value to several variables. For the asterisk, see Common Sequence Operations.
    x, y, z = [10, 10, 10] #[10, 10, 10] is a list of three ints
    print(x, y, z)
    
    10 10 10
    
    x, y, z = 3 * [10]     #3 * [10] is a list of three ints
    print(x, y, z)
    
    10 10 10
    
    x = y = z = 10         #chained assignment
    print(x, y, z)
    
    10 10 10