#python #programming #question #intermediate #looping
Write a Python program that demonstrates the use of for loops in various forms with the following requirements:
1. Use a standard for loop to iterate through a list of numbers and print each number.
2. Use a for loop with enumerate() to print both the index and value of each element in a list.
3. Use a for loop with zip() to iterate through two lists simultaneously and print corresponding elements.
4. Use a for loop with range() to print numbers from 1 to 10, but only print even numbers.
5. Use a nested for loop to create a multiplication table (1-5) and print it in a formatted way.
```python
# 1. Standard for loop
numbers = [1, 2, 3, 4, 5]
print("1. Standard for loop:")
for num in numbers:
print(num)
# 2. For loop with enumerate()
fruits = ['apple', 'banana', 'cherry']
print("\n2. For loop with enumerate():")
for index, fruit in enumerate(fruits):
print(f"Index {index}: {fruit}")
# 3. For loop with zip()
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
print("\n3. For loop with zip():")
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
# 4. For loop with range() - even numbers only
print("\n4. For loop with range() - even numbers:")
for i in range(1, 11):
if i % 2 == 0:
print(i)
# 5. Nested for loop - multiplication table
print("\n5. Nested for loop - multiplication table (1-5):")
for i in range(1, 6):
for j in range(1, 6):
print(f"{i} × {j} = {i*j}", end="\t")
print() # New line after each row
```
By: @DataScienceQ 🚀
Write a Python program that demonstrates the use of for loops in various forms with the following requirements:
1. Use a standard for loop to iterate through a list of numbers and print each number.
2. Use a for loop with enumerate() to print both the index and value of each element in a list.
3. Use a for loop with zip() to iterate through two lists simultaneously and print corresponding elements.
4. Use a for loop with range() to print numbers from 1 to 10, but only print even numbers.
5. Use a nested for loop to create a multiplication table (1-5) and print it in a formatted way.
```python
# 1. Standard for loop
numbers = [1, 2, 3, 4, 5]
print("1. Standard for loop:")
for num in numbers:
print(num)
# 2. For loop with enumerate()
fruits = ['apple', 'banana', 'cherry']
print("\n2. For loop with enumerate():")
for index, fruit in enumerate(fruits):
print(f"Index {index}: {fruit}")
# 3. For loop with zip()
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
print("\n3. For loop with zip():")
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
# 4. For loop with range() - even numbers only
print("\n4. For loop with range() - even numbers:")
for i in range(1, 11):
if i % 2 == 0:
print(i)
# 5. Nested for loop - multiplication table
print("\n5. Nested for loop - multiplication table (1-5):")
for i in range(1, 6):
for j in range(1, 6):
print(f"{i} × {j} = {i*j}", end="\t")
print() # New line after each row
```
By: @DataScienceQ 🚀