1. For loop with range
In the previous lessons we dealt with sequential programs and conditions. Often the program needs to repeat some block several times. That's where the loops come in handy. There are for
and while
loop operators in Python, in this lesson we cover for
.
for
loop iterates over any sequence. For
instance, any string in Python is a sequence of its characters, so we can iterate over them using for
:
for character in 'hello': print[character]
Another use case for a for-loop is to iterate some integer variable in increasing or decreasing order. Such a sequence of integer can be created using the function range[min_value, max_value]
:
for i in range[5, 8]: print[i, i ** 2] print['end of loop'] # 5 25 # 6 36 # 7 49 # end of loop
Function range[min_value, max_value]
generates a
sequence with numbers min_value
, min_value + 1
, ..., max_value - 1
. The last number is not included.
There's a reduced form of range[] - range[max_value]
, in which case min_value is implicitly set to zero:
for i in range[3]: print[i] # 0 # 1 # 2
This way we can repeat some action several times:
for i in range[2 ** 2]: print['Hello, world!']
Same as with if-else, indentation is what specifies which instructions are
controlled by for
and which aren't.
Range[] can define an empty sequence, like range[-5]
or range[7, 3]
. In this case the for-block won't be executed:
for i in range[-5]: print['Hello, world!']
Let's have more complex example and sum the integers from 1 to n inclusively.
result = 0 n = 5 for i in range[1, n + 1]: result += i # this ^^ is the shorthand for # result = result + i print[result]
Pay attention that maximum value in range[] is n + 1
to make i
equal to
n on the last step.
To iterate over a decreasing sequence, we can use an extended form of range[] with three arguments - range[start_value, end_value, step]
. When omitted, the step is implicitly equal to 1. However, can be any non-zero value. The loop always includes start_value and excludes end_value during iteration:
for i in range[10, 0, -2]: print[i] # 10 # 8 # 6 # 4 # 2
Advertising by Google, may be based on your interests
2. setting the function print[]
By default, the function print[]
prints all its arguments separating them by a space and the puts a newline symbol after it. This behavior can be changed using keyword arguments sep
[separator] and end
.
print[1, 2, 3] print[4, 5, 6] print[1, 2, 3, sep=', ', end='. '] print[4, 5, 6, sep=', ', end='. '] print[] print[1, 2, 3, sep='', end=' -- '] print[4, 5, 6, sep=' * ', end='.']
Advertising by Google, may be based on your interests
The range[] Function
To loop through a set of code a specified number of times, we can use the range[] function,
The range[] function returns a sequence of numbers, starting from 0 by default, and increments by 1 [by default], and ends at a specified number.
Note that range[6] is not the values of 0 to 6, but the values 0 to 5.
The range[] function defaults to 0 as a starting value, however it is possible to specify the starting value by adding a parameter: range[2, 6], which means values from 2 to 6 [but not including 6]:
The range[] function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter: range[2, 30, 3]:
Example
Increment the sequence with 3 [default is 1]:
for x in range[2, 30, 3]:
print[x]
Try it Yourself »
I think you are looking for nested loops.
Example [based on your edit]:
t1=[1,2,'Hello',[1,2],999,1.23]
t2=[1,'Hello',[1,2],999]
t3=[]
for it1, e1 in enumerate[t1]:
for it2, e2 in enumerate[t2]:
if e1==e2:
t3.append[[it1,it2,e1]]
# t3=[[0, 0, 1], [2, 1, 'Hello'], [3, 2, [1, 2]], [4, 3, 999]]
Which can be reduced to a single comprehension:
[[it1,it2,e1] for it1, e1 in enumerate[t1] for it2, e2 in enumerate[t2] if e1==e2]
But to find the common elements, you can just do:
print set[t1] & set[t2]
# set[[[1, 2], 1, 'Hello', 999]]
If your list contains non-hashable objects [like other lists, dicts] use a frozen set:
from collections import Iterable
s1=set[frozenset[e1] if isinstance[e1,Iterable] else e1 for e1 in t1]
s2=set[frozenset[e2] if isinstance[e2,Iterable] else e2 for e2 in t2]
print s1 & s2