Course
Loop Tuples
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Loop Tuples
You can traverse the items in a tuple with Python's
for
loop construct. The traversal can be done, using tuple as an iterator or with the help of index.Syntax
Python tuple gives an iterator object. To iterate a tuple, use the
for
statement as followsfor obj in tuple: . . . . . .
Example 1
The following example shows a simple Python
for
loop constructtup1 = (25, 12, 10, -21, 10, 100)for num in tup1: print (num, end = ' ')
It will produce the following output
25 12 10 -21 10 100
Example 2
To iterate through the items in a tuple, obtain the range object of integers "
0
" to "len-1
".tup1 = (25, 12, 10, -21, 10, 100)indices = range(len(tup1))for i in indices: print ("tup1[{}]: ".format(i), tup1[i])
It will produce the following output
tup1[0]: 25tup1 [1]: 12tup1 [2]: 10tup1 [3]: -21tup1 [4]: 10tup1 [5]: 100
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.