Python-Ref > Basic usage > The print command > Printing complex types
 
 

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

Printing complex types

How to print lists or functions.
Printing of simple types, such as strings or numbers is straightforward and simple.
Expand/Shrink
Zdroj: (print2.py)
  1   print "Here we go."
  2   print 1
  3   print 10*12
  4   print 1.23*0.65
stdout:
Here we go.
1
120
0.7995
Doba běhu: 21.0 ms
What happens when we would like to print some more complex types, such as lists, dictionaries or functions?
Expand/Shrink
Zdroj: (print3.py)
  1   print [1,2,3,4,"hi"]
  2   print range(10)
  3   print range
  4   print {1:'hi',2:'there'}
stdout:
[1, 2, 3, 4, 'hi']
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
<built-in function range>
{1: 'hi', 2: 'there'}
Doba běhu: 22.3 ms
As is obvious from the previous example, print does not have any problems with sequence types, such as tuples, lists etc. It simple prints the whole sequence.
On the other hand, objects of more esoteric nature, such as functions, types or classes are not directly printable and only their description is printed out, such as in case of the built-in function range.
Please pay close attention to the difference between the two different lines using range in the example above.
print range(10)
prints the result of application of the function range onto the number 10, which is the list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
while
print range
prints the function itself, i.e. the function is not applied to any value.