-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9_for.py
More file actions
33 lines (24 loc) · 803 Bytes
/
9_for.py
File metadata and controls
33 lines (24 loc) · 803 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
#Printing numbers from 1 to 5 using a for loop
# Use a for loop to print numbers from 1 to 5
for i in range(1, 6):
print(i)
# Iterating over elements in a list using a for loop
# Define a list of fruits
fruits = ["apple", "banana", "cherry", "date", "elderberry"]
# Use a for loop to iterate over each fruit and print it
for fruit in fruits:
print(fruit)
# Calculating the sum of elements in a list using a for loop
# Define a list of numbers
numbers = [10, 20, 30, 40, 50]
# Initialize a variable to store the sum
total = 0
# Use a for loop to iterate over each number and calculate the sum
for num in numbers:
total += num
# Print the total sum
print("The total sum is:", total)
# Print number pair 1 to 100
for num in range(1, 101):
if num % 2 == 0:
print(num)