how to iterate in python code example

Example 1: loop through python object

for attr, value in k.__dict__.items():
        print(attr, value)

Example 2: what is iteration in python

# Iteration is the execution of a statement repeatedly and without
# making any errors.

Example 3: how to use iteration in python

>>> a = ['foo', 'bar', 'baz']
>>> for i in a:
...     print(i)
...
foo
bar
baz

Example 4: how to use iteration in python

for i = 1 to 10
    <loop body>

Example 5: how to create a break in iterating a sequence with a specific set of values in python

>>> d = {'foo': 1, 'bar': 2, 'baz': 3}
>>> for k, v in d.items():
...     print('k =', k, ', v =', v)
...
k = foo , v = 1
k = bar , v = 2
k = baz , v = 3

Example 6: how to use iteration in python

n = 5
while n > 0:
    print n
    n = n-1
print 'Blastoff!'

Tags:

Cpp Example