Python-Ref > Basic usage > Variables > Assignment
 
 

^^->
Klíčová slova
Moduly
Knihovní funkce

Assignment

How to store a value in a variable.
To assign a value to a variable, one uses the operator '='.
Expand/Shrink
Zdroj: (var1-1.py)
  1   a = 1   # assign 1 to a
  2   b = 2
  3   c = a + b
  4   
  5   print a
  6   print b
  7   print c
  8   
  9   b = "hello"   # reassign b to value "hello"
 10   
 11   print b
stdout:
1
2
3
hello
Doba běhu: 21.1 ms
The right value of the assignment is evaluated before the assignment takes place. Therefor it is possible to use the same variable on both sides of the assignment.
Expand/Shrink
Zdroj: (var1-2.py)
  1   a = 1
  2   print a
  3   
  4   a = a + 5
  5   print a
  6   
  7   a = a*a - 2*a + 3
  8   print a
stdout:
1
6
27
Doba běhu: 20.9 ms