-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathj_loops.py
More file actions
37 lines (26 loc) · 816 Bytes
/
j_loops.py
File metadata and controls
37 lines (26 loc) · 816 Bytes
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
stuff = ['a', 'b', 'c', 'd']
# Every loop in python is either a for loop
# (referred to as a for-in loop in some other languages)
# or a while loop
# While loops operate virtually identically to other languages
i = 0
while i < 5:
print(f'i is now: {i}')
i += 1
# for loops however operate a little differently
# iterate over a collection
for item in stuff:
print(f'item: {item}')
# iterate over a range:
# there is no direct equivalent to the c: ' for(int i = 0; i < 5; ++i) {} '
# instead, range is used
# note that ranges are exclusive; they run up to, but not including,
# the final value.
for i in range(5):
print(f'range a: {i}')
for i in range(2, 5):
print(f'range b: {i}')
for i in range(2, 8, 2):
print(f'range c: {i}')
for i in range(2, 8, -1):
print(f'range d: {i}')