-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloops.py
100 lines (61 loc) · 1.5 KB
/
loops.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
# Challenge 1: Print each fruit in a fruit list:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# Challenge 2: Print each fruit in a fruit list, using a for loop:
for i in range(5):
print(i)
# Challenge 3: Print each fruit in a fruit list, using a for loop:
word = "hello"
for letter in word:
print(letter)
# Challenge 4: Print each fruit in a fruit list, using a for loop:
count = 0
while count < 5:
print(count)
count += 1
# Challenge 5: Print each fruit in a fruit list, using a for loop:
user_input = ""
while user_input != "exit":
user_input = input("Type 'exit' to end the loop: ")
print("You typed:", user_input)
print("Type 'exit' to end the loop.")
# Challenge 6: Print each fruit in a fruit list, using a for loop:
i = 1
while i <= 10:
if i == 5:
break
print(i)
i += 1
for i in range(1, 11):
if i == 5:
break
print(i)
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
break
print(f"i = {i}, j = {j}")
i = 1, j = 1
i = 2, j = 1
i = 3, j = 1
for i in range(1, 6):
if i == 3:
break
print(i)
else:
print("Loop completed without break")
i = 1
while i <= 5:
if i == 3:
break
print(i)
i += 1
else:
print("Loop completed without break")
lst = [1, 2, 3]
lst.append(4) # lst is now [1, 2, 3, 4]
lst = [1, 2, 3]
lst.insert(1, 1.5) # lst is now [1, 1.5, 2, 3]
lst = [1, 2, 3, 2]
lst.remove(2) # lst is now [1, 3, 2]