-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfor.py
executable file
·117 lines (87 loc) · 1.88 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 9 23:43:32 2021
@author: maherme
"""
#%%
# In Python, an iterable is an object capable of returning values one at a
# time.
# In other lenguages a for loop is similar to: for(int i=0; i<5; i++){...}
# This is similar to a while loop:
i = 0
while i < 5:
print(i)
i += 1
i = None
#%%
# In Python a for loop is used with iterable items.
# In this code range() is an iterable which return a number which is stored in
# i.
for i in range(5):
print(i)
#%%
# As iterable, you can do a for with a list:
for i in [1, 2, 3, 4]:
print(i)
#%%
# You can also use a string:
for c in 'hello':
print(c)
#%%
# You can also use a tuple:
for x in ('a', 'b', 'c', 4):
print(x)
#%%
# You can create more complex loops:
for i, j in [(1, 2), (3, 4), (5, 6)]:
print(i, j)
#%%
# You can use other statements inside a for loop:
for i in range(5):
if i == 3:
break
print(i)
#%%
# You can also use an "else" statement:
for i in range(1, 5):
print(i)
if i % 7 == 0:
print('multiple of 7 found')
break
else:
print('no multiples of 7 in the range')
#%%
# You can also use "try catch" statement. This working in the same way than
# in while loop.
for i in range(5):
print('--------------------')
try:
10/(i-3)
except ZeroDivisionError:
print('divided by 0')
continue
finally:
print('always run')
print(i)
#%%
# You don't need the index for an iterable with index like a string:
s = 'hello'
for c in s:
print(c)
#%%
s = 'hello'
i = 0
for c in s:
print(i, c)
i += 1
#%%
s = 'hello'
for i in range(len(s)):
print(i, s[i])
#%%
# This is a more readable way to implement the code above:
s = 'hello'
for i, c in enumerate(s):
print(i, c)
#%%