-
Notifications
You must be signed in to change notification settings - Fork 0
/
loops-for.py
51 lines (36 loc) · 899 Bytes
/
loops-for.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# iterator: lists, tuples, sets
l = [1, 2, 3, 4, 5, 6]
for item in l:
if item % 2 == 0:
print 'number {x} is odd'.format(x=item.__str__())
else:
print 'number {x} is even'.format(x=item.__str__())
list_sum = 0
for num in l:
list_sum += num
print list_sum
for char in 'hello':
print char.upper()
tup = (1, 2, 3, 4, 5, 6)
for item in tup:
print item
list1 = [(2, 4), (6, 8)]
for tup in list1:
print tup
# tuple unpacking
for (t1, t2) in list1:
print t1
print t2
d = {
'a': 1,
'b': 2,
'c': 3
}
for (key, value) in d.items():
print "Key is {key}, value is {value}".format(key=key, value=value)
list2 = [(1, 2, 3), (4, 5, 6)]
list3 = [[1, 2, 3], [4, 5, 6]]
for (v1, v2, v3) in list2:
print "{v1}, {v2}, {v3}".format(v1=v1, v2=v2, v3=v3)
for [v1, v2, v3] in list3:
print "{v1}, {v2}, {v3}".format(v1=v1, v2=v2, v3=v3)