Questions on for loop and pattern

Home / Python / Questions on for loop and pattern

Questions on for loop and pattern


1. Write a Python program to print each element of a list using a for loop.

numbers = [1, 2, 3, 4, 5]
for num in numbers:
    print(num)
Output:
1
2
3
4
5
 
2. Write a Python program to print all even numbers between 1 and 10 using a for loop.

for num in range(1, 11):
    if num % 2 == 0:
        print(num)
Output:
2
4
6
8
10
 
3. Write a Python program to calculate the sum of all numbers in a list using a for loop.

numbers = [5, 10, 15, 20]
total = 0
for num in numbers:
    total += num
print("Sum:", total)
Output:
Sum: 50
 
4. Write a Python program to print each character of a string using a for loop.

word = "Python"
for char in word:
    print(char)
Output:
P
y
t
h
o
n
 
5. Write a Python program to calculate the factorial of a given number using a for loop.

num = 5
factorial = 1
for i in range(1, num + 1):
    factorial *= i
print("Factorial:", factorial)
Output: Factorial: 120
 
6. Write a Python program to print the keys and values of a dictionary using a for loop.
 
person = {"name": "Alice", "age": 30, "city": "New York"}
for key, value in person.items():
    print(key, ":", value)
Output:
name : Alice
age : 30
city : New York
 
7. Write a Python program to find the maximum number in a list using a for loop.
 
numbers = [3, 8, 1, 6, 10, 2]
max_num = numbers[0]
for num in numbers:
    if num > max_num:
        max_num = num
print("Maximum number:", max_num)
Output:
Maximum number: 10
 
8. Write a Python program to print the square of each number in a list using a for loop.
 
numbers = [1, 2, 3, 4, 5]
for num in numbers:
    print(f"Square of {num} is {num**2}")
Output:
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
 
9. Write a Python program to count the number of vowels in a given string using a for loop.
 
vowels = "aeiouAEIOU"
count = 0
word = "Programming"
for char in word:
    if char in vowels:
        count += 1
print("Number of vowels:", count)
Output:
Number of vowels: 3
 
10. Write a Python program to print the index and corresponding value of each element in a list using a for loop.

numbers = [10, 20, 30, 40, 50]
for index, value in enumerate(numbers):
    print(f"Index {index} has value {value}")
Output:
Index 0 has value 10
Index 1 has value 20
Index 2 has value 30
Index 3 has value 40
Index 4 has value 50
 
PATTERN PROGRAMS

11. Right-angled Triangle Pattern
 

Python Code:
python
Copy code
n = 5
for i in range(1, n+1):
    print("*" * i)
 
12. Inverted Right-angled Triangle Pattern
 

Python Code:
n = 5
for i in range(n, 0, -1):
    print("*" * i)
 
13. Pyramid Pattern
 

Python Code:
n = 5
for i in range(1, n+1):
    print(" " * (n-i) + "*" * (2*i-1))
 
14. Inverted Pyramid Pattern
 

Python Code:
n = 5
for i in range(n, 0, -1):
    print(" " * (n-i) + "*" * (2*i-1))
 
15. Diamond Pattern
 

Python Code:
n = 5
for i in range(1, n+1):
    print(" " * (n-i) + "*" * (2*i-1))
for i in range(n-1, 0, -1):
    print(" " * (n-i) + "*" * (2*i-1))

16. Number Triangle Pattern
 

Python Code:
n = 5
for i in range(1, n+1):
    for j in range(1, i+1):
        print(j, end="")
    print()
 
17. Floyd's Triangle
 

Python Code:
n = 5
num = 1
for i in range(1, n+1):
    for j in range(1, i+1):
        print(num, end=" ")
        num += 1
    print()


18. Alphabet Triangle Pattern
 

Python Code:
n = 5
for i in range(1, n+1):
    ch = 65  # ASCII value of 'A'
    for j in range(1, i+1):
        print(chr(ch), end=" ")
        ch += 1
    print()
 
19. Reverse Number Triangle
 

Python Code:
n = 5
for i in range(n, 0, -1):
    for j in range(1, i+1):
        print(j, end=" ")
    print()
 
20. Half Pyramid using numbers
 

Python Code:
n = 5
for i in range(1, n+1):
    print(str(i) * i)