Repository navigation
Expand file tree
/
Copy pathexamples.py
More file actions
104 lines (81 loc) · 1.81 KB
/
Copy pathexamples.py
File metadata and controls
104 lines (81 loc) · 1.81 KB
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
"""
Example code from Python For Loops: Quick Answers and Examples
"""
# Basic Usage
word = 'Mississippi'
count = 0
for letter in word:
if letter == 'i':
count = count + 1
print(count)
# Lists
student_list = ("jake", "john", "jim")
for student in student_list:
print(student)
# How do loops work?
for character in "Kite":
print(character)
# iterating with next()
example_tuple = ("smartwater", "fiji", "aquafina")
my_tuple = iter(example_tuple)
print(next(my_tuple))
print(next(my_tuple))
print(next(my_tuple))
# with a loop
example_tuple = ("smartwater", "fiji", "aquafina")
for x in example_tuple:
print(x)
# break statement
students = ["John", "Jerry", "Sarah"]
for x in students:
print(x)
if x == "Jerry":
break
students = ["John", "Jerry", "Sarah"]
for x in students:
if x == "Jerry":
break
print(x)
# continue statement
students = ["John", "Jerry", "Sarah"]
for x in students:
if x == "Jerry":
continue
print(x)
# range()
for x in range(5):
print(x)
for x in range(0, 10, 2):
print(x)
# else
for x in range(5):
print(x)
else:
print("Finally finished!")
# non-nested loop
list_of_lists = [['yorkshire', 'jack russell', 'golden retriever'], [0, 1, 2], [11, 22, 33]]
for list_item in list_of_lists:
print(list_item)
# nested loop
list_of_lists = [['yorkshire', 'jack russell', 'golden retriever'], [0, 1, 2], [11, 22, 33]]
for list_item in list_of_lists:
for item in list_item:
print(item)
# nesting for combination
adj = ["red", "sporty", "electric"]
cars = ["BMW", "Lexus", "Tesla"]
for x in adj:
for y in cars:
print(x, y)
# infinite loop
count = 0
i = 1
while i == 1:
count += 1
# Calling range()
for i in range(6):
print(i)
for i in range(1, 6):
print(i)
for i in range(5):
print(i + 1)