Python-Ref > Basic usage > Variables > Types of assignment
 
 

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

Types of assignment

Different way a variable may get it value.
The first example shows several possible ways of an variable being created and assigned value.
Expand/Shrink
Zdroj: (var3-1.py)
  1   # simple assigment
  2   a = "Hi there"
  3   print "a =", a
  4   # simple assigment of function result
  5   b = len( a)
  6   print "b =", b
  7   # simple assigment of method result
  8   c = a.upper()
  9   print "c =", c
 10   # for loop
 11   for x in [1,2]:
 12     print "x =", x
stdout:
a = Hi there
b = 8
c = HI THERE
x = 1
x = 2
Doba běhu: 20.2 ms
The second one shows what does not work.
Expand/Shrink
This is an example text in an example file.
It even has
several
lines.
Zdroj: (var3-2.py)
  1   # value lost because not assigned to anything
  2   "Hi there"
  3   # function result lost
  4   a = "xx"
  5   len( a)
  6   # method result lost the same way
  7   #  we open a file
  8   f = file( "example.txt", "r")
  9   #  and send the first line to silicon heaven
 10   f.readline()
 11   # another method result lost the same way
 12   a.upper()
Doba běhu: 20.3 ms