Skip to content

Commit 0c323b3

Browse files
Day 4 - Iteration & Loops
1 parent 2c5d102 commit 0c323b3

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Day 4 - Iteration & Loops
2+
3+
4+
5+
### The `for` loop:
6+
```
7+
🕹 % python3
8+
Python 3.8.2 (v3.8.2:7b3ab5921f, Feb 24 2020, 17:52:18)
9+
[Clang 6.0 (clang-600.0.57)] on darwin
10+
Type "help", "copyright", "credits" or "license" for more information.
11+
>>> my_list = [1,2,3,4,5]
12+
>>> my_list[0]
13+
1
14+
>>> my_list[1]
15+
2
16+
>>> my_list[2]
17+
3
18+
>>> my_list[3]
19+
4
20+
>>> my_list[4]
21+
5
22+
>>> for my_var in my_list:
23+
... print(my_var)
24+
...
25+
1
26+
2
27+
3
28+
4
29+
5
30+
>>> my_var
31+
5
32+
>>> for i in "abc":
33+
... print(i)
34+
File "<stdin>", line 2
35+
print(i)
36+
^
37+
IndentationError: expected an indented block
38+
>>>
39+
>>> for i in "abc":
40+
... print(i)
41+
... print(i)
42+
File "<stdin>", line 3
43+
print(i)
44+
^
45+
IndentationError: unindent does not match any outer indentation level
46+
>>> for i in "abc":
47+
... print(i)
48+
... print(i)
49+
...
50+
a
51+
a
52+
b
53+
b
54+
c
55+
c
56+
>>> "abc"[1]
57+
'b'
58+
>>>
59+
>>>
60+
>>>
61+
>>>
62+
>>>
63+
>>>
64+
>>>
65+
>>>
66+
>>>
67+
>>>
68+
>>>
69+
>>>
70+
>>> for x in 10:
71+
... print(x)
72+
...
73+
Traceback (most recent call last):
74+
File "<stdin>", line 1, in <module>
75+
TypeError: 'int' object is not iterable
76+
>>> for x in "10":
77+
... print(x)
78+
...
79+
1
80+
0
81+
>>> for x in range(0, 10):
82+
... print(x)
83+
...
84+
0
85+
1
86+
2
87+
3
88+
4
89+
5
90+
6
91+
7
92+
8
93+
9
94+
>>> user_1 = {'username': 'jmitchel3', 'id': 1}
95+
>>> user_2 = {'username': 'abc', 'id': 2}
96+
>>> my_users = [user_1, user_2]
97+
>>> for user in my_users:
98+
... print(user)
99+
...
100+
{'username': 'jmitchel3', 'id': 1}
101+
{'username': 'abc', 'id': 2}
102+
>>> for user in my_users:
103+
... print(user['username'])
104+
...
105+
jmitchel3
106+
abc
107+
>>> for user in my_users:
108+
... print(user['email'])
109+
...
110+
Traceback (most recent call last):
111+
File "<stdin>", line 2, in <module>
112+
KeyError: 'email'
113+
>>> user_2 = {'username': 'abc', 'id': 2, 'email': 'abc@abc.abc'}
114+
>>> print(my_users)
115+
[{'username': 'jmitchel3', 'id': 1}, {'username': 'abc', 'id': 2}]
116+
>>> my_users = [user_1, user_2]
117+
>>> print(my_users)
118+
[{'username': 'jmitchel3', 'id': 1}, {'username': 'abc', 'id': 2, 'email': 'abc@abc.abc'}]
119+
>>> for user in my_users:
120+
... print(user['email'])
121+
...
122+
Traceback (most recent call last):
123+
File "<stdin>", line 2, in <module>
124+
KeyError: 'email'
125+
>>> for user in my_users:
126+
... if 'email' in user:
127+
... print(user['email'])
128+
...
129+
abc@abc.abc
130+
>>> selected_user = {}
131+
>>> my_user_lookup = 2
132+
>>> for user in my_users:
133+
... if 'id' in user:
134+
... if user['id'] == my_user_lookup:
135+
... selected_user = user
136+
...
137+
>>> print(selected_user)
138+
{'username': 'abc', 'id': 2, 'email': 'abc@abc.abc'}
139+
>>> for x in range(0, 10):
140+
... print(x)
141+
...
142+
0
143+
1
144+
2
145+
3
146+
4
147+
5
148+
6
149+
7
150+
8
151+
9
152+
>>> for x in range(0, 10):
153+
... for user in my_users:
154+
... if user['id'] == x:
155+
... print(user)
156+
...
157+
{'username': 'jmitchel3', 'id': 1}
158+
{'username': 'abc', 'id': 2, 'email': 'abc@abc.abc'}
159+
```

0 commit comments

Comments
 (0)