December 2000
Intermediate to advanced
816 pages
16h 57m
English
The for loop in Python is more like a foreach iterative-type loop in a shell scripting language than a traditional for conditional loop that works like a counter. Python's for loop takes what we will later describe as a sequence type (list, tuple, or string) and iterates over each element of that sequence.
>>> print 'I like to use the Internet for:' I like to use the Internet for: >>> for item in ['e-mail', 'net-surfing', 'homework', 'chat']: … print item … e-mail net-surfing homework chat
Our output in the previous example may look more presentable if we display the items on the same line rather than on separate lines. print statements by default automatically add a NEWLINE character at the ...
Read now
Unlock full access