-
Notifications
You must be signed in to change notification settings - Fork 0
/
for-loop.py
50 lines (39 loc) · 1.05 KB
/
for-loop.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
# range
a = [1, 2, 3, 4]
for i in range(4):
print(a[i])
for step in range(5):
print(step)
a= list(range(3, 6))
print(a)
# direct list
a = [1, 2, 3, 4]
for i in a:
print(i, 'd')
arr = [2, 3]
summation = 0
for x in arr:
summation += x
print(summation) # 5
# enumerate process items in a sequence with an index.
for i, v in enumerate(['tic', 'tac', 'toe']):
print(i, v)
# reversed usage
for i in reversed(range(1, 10, 2)):
print(i)
collection = [('apple', 'red'), ('banana', 'yello'), ('kiwi',
'green')]
for name, color in collection:
print ('name: %s, color: %s' % (name, color, ))
def f(x):
return x * 3
list1 = [11, 22, 33]
list2 = [f(x) for x in list1]
print (list2)
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q, a in zip(questions, answers):
print('What is your {0}? It is {1}.'.format(q, a))
a = ['Hacker', 'Earth','1','2', 'Python', 'Language', '10']
a_to_dictionary = dict(zip(a[0::2], a[1::2]))
print(a_to_dictionary)