Skip to content

Commit 12e1302

Browse files
add questions using loop😊🚀
1 parent efdc986 commit 12e1302

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#Find the average of numbers in a list using a for loop:
2+
my_list = [4, 7, 9, 2, 5]
3+
total = 0
4+
for num in my_list:
5+
total += num
6+
average = total / len(my_list)
7+
print(average, "average numbers")
8+
9+
#Find the largest number in a list using a for loop:
10+
11+
my_list = [3, 19, 19, 60, 28, 80]
12+
largest = my_list[0]
13+
for i in my_list:
14+
if i > largest:
15+
largest = i
16+
print(largest, "is a largest Number")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#Find the first occurrence of a number in a list using a while loop:
2+
my_list = [3, 8, 2, 7, 4]
3+
target = 7
4+
index = 0
5+
while index < len(my_list):
6+
if my_list[index] == target:
7+
break
8+
index += 1
9+
else:
10+
index = -1
11+
print(index)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Print the Fibonacci sequence up to the 10th term using a while loop:
2+
a, b = 0, 1
3+
count = 0
4+
while count < 10:
5+
print(a, end=" ")
6+
a, b = b, a + b
7+
count += 1
8+
9+
#Find the common elements in two lists using a for loop:
10+
list1 = [1, 2, 3, 4, 5, 6, 8]
11+
list2 = [3, 4, 5, 6, 7]
12+
common_elements = []
13+
for i in list1:
14+
if i in list2 and i not in common_elements:
15+
common_elements.append(i)
16+
print("Common elements:", common_elements)

0 commit comments

Comments
 (0)