Next / Previous / Contents / TCC Help System / NM Tech homepage

13. Simple statements

Simple (non-branching) statement types are summarized below.

13.1. Assignment

Assignment is a statement, not an operator, in Python. The general form is:

L0=L1=...=E

This statement first evaluates some expression E, and then assigns that value to one or more destinations L0, L1, and so on. Each destination can be either of:

  • A variable name. The variable is bound to the value of the expression, that is, that variable now has that value, and any previous value it may have had is forgotten. For example, the statement

    beanCount=m=0

    sets variables beanCount and m to the integer value 0.

  • Part of a mutable object that contains multiple values. For example, if L is a list, the statement

    L[0]='xyz'

    would set the first element of L to the string 'xyz'.

    As another example, suppose D is a dictionary. The statement

    D["color"]="red"

    would associate key "color" with value "red" in that dictionary.

    You can also assign to slices of a list:

    L[1:3] = [10, 20, 30]

    This statement would delete elements 1 and 2 of list L and replace them with three new elements 10, 20, and 30.

    Finally, if an object X has an attribute X.klarn, you can assign a value of 73 to it using:

    X.klarn = 73
  • If the expression E is a sequence, the destination can be a list of variables, and the sequence is unpacked into the variables in order. For example, this statement

    x, y, z  =  [10, 11, 12]

    sets x to 10, y to 11, and z to 12.