-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path004-forloop.py
More file actions
30 lines (24 loc) · 793 Bytes
/
004-forloop.py
File metadata and controls
30 lines (24 loc) · 793 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
"""
Iterate over a sequence. Sequence can be range, list, tuple, dictionary, set
or string
----------------------------------------------------------------------------
"""
print("Iterate over a range:")
for i in range(3): # i is a veriable, you can name it anything
print(i)
print("\nIterate over a range; part 2:")
for i in range(3,11): # i is a veriable, you can name it anything
print(i)
print("\nIterate over a range; part 3:")
for i in range(3,11,2): # i is a veriable, you can name it anything
print(i)
print("\nIterate over a list:")
cars = ["Audi", "BMW", "Chrysler", "Dodge"]
for car in cars:
print(car)
print("\nThe break statement:")
for car in cars:
print(car)
if car == "BMW":
print("'BMW' found, breaking the loop")
break