• Save
  • Run All Cells
  • Clear All Output
  • Runtime
  • Download
  • Difficulty Rating

Loading Runtime

In Python, the assignment operator is represented by the = symbol, which is used to assign a value to a variable. The basic syntax of the assignment operator is:

variable = value

Here, the word "variable" is where you would put the name of the variable to which the "value" is being assigned. You can store (assign) lots of things inside of a variable. Acceptable "values" include "literal" values (e.g., a number or a string), the result of an expression, or the value that's stored in another variable.

For example, consider the following code snippet:

The value of 'x' is: 5The value of 'y' is: 11The value of 'z' is: 16

In this code, the first line assigns the value of 5 to the variable x.

The second line assigns the value of 2 * x + 1 to the variable y, which is computed using the value that was stored (assigned) to x on line 1.

The third line assigns the value of x + y to the variable z, which is computed using the previously assigned values of x and y.

The assignment operator can also be used in combination with "arithmetic operators", such as:

  • +=
  • -=
  • *=
  • /=
  • //=
  • %=
  • **=

These operators perform a mathematical operation on a variable and then assign the result back to the same variable. For example, the following code is equivalent to x = x + 1:

The value of 'x' is now: 6

Feel free to run these code cells multiple times to see the operators in action.

5
40
13.333333333333334
3.0
1.0
1.0