In the previous section we discussed the basics of the for loop. Now let us take a look at some more Problems.
1. Multiplication Table of a Number
Write a Python Program to print the multiplication table of a number
num = int(input("Enter number: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
# Enter number: 4
# 4 x 1 = 4
# 4 x 2 = 8
# 4 x 3 = 12
# 4 x 4 = 16
# 4 x 5 = 20
# 4 x 6 = 24
# 4 x 7 = 28
# 4 x 8 = 32
# 4 x 9 = 36
# 4 x 10 = 40
2. Count Digits in a Number
Write a Python Program to count the number of digits in the entered number.
num = input("Enter a number: ")
count = 0
for i in num:
count += 1
print("Total digits:", count)
# Enter a number: 53217
# Total digits: 5
3. Print Characters of a String
Write a Python Program to print the characters of a String by using for loop
text = input("Enter string: ")
for ch in text:
print(ch)
# Enter string: qubrica
# q
# u
# b
# r
# i
# c
# a
4. Palindrome Check
Write a Python Program to check if the entered string is palindrome
text = input("Enter a string: ")
reverse = ""
for ch in text:
reverse = ch + reverse
if text == reverse:
print("It is a Palindrome")
else:
print("It is Not a Palindrome")
# Enter a string: soos
# It is a Palindrome
5. Number Pattern
Print the pattern by using for loop
1
22
333
4444
55555
for i in range(1, 6):
print(str(i) * i)
1
22
333
4444
55555
| Iteration | i value | str(i) (String) | str(i) * i (Calculation) | Final Output |
|---|---|---|---|---|
| 1 | 1 | "1" | "1" * 1 | 1 |
| 2 | 2 | "2" | "2" * 2 | 22 |
| 3 | 3 | "3" | "3" * 3 | 333 |
| 4 | 4 | "4" | "4" * 4 | 4444 |
| 5 | 5 | "5" | "5" * 5 | 55555 |
6. Reverse Number Pattern
Print the following pattern
5
54
543
5432
54321
for i in range(5, 0, -1):
for j in range(5, i - 1, -1):
print(j, end="")
print()
5
54
543
5432
54321| Outer Loop (i) | Range of j (5 to i) | Inner Values of j | Output |
|---|---|---|---|
| 5 | range(5, 4, -1) | 5 | 5 |
| 4 | range(5, 3, -1) | 5,4 | 54 |
| 3 | range(5, 2, -1) | 5,4,3 | 543 |
| 2 | range(5, 1, -1) | 5,4,3,2 | 5432 |
| 1 | range(5, 0, -1) | 5,4,3,2,1 | 54321 |
7. Floyd’s Triangle
Write a Python Program to print the below Pattern
1
2 3
4 5 6
7 8 9 10
num = 1
rows = 4
for i in range(1, rows + 1):
for j in range(i):
print(num, end=" ")
num += 1
print()
1
2 3
4 5 6
7 8 9 10 | Row (i) | Range of j | j Iterations | num Value (Before Print) | Output | num += 1 (After Print) |
|---|---|---|---|---|---|
| 1 | range(1) | 0 | 1 | 1 | 2 |
| 2 | range(2) | 0, 1 | 2, 3 | 2 3 | 3, 4 |
| 3 | range(3) | 0,1, 2 | 4, 5, 6 | 4 5 6 | 5, 6, 7 |
| 4 | range(4) | 0, 1, 2, 3 | 7, 8, 9, 10 | 7 8 9 10 | 8, 9, 10, 11 |
8. Pyramid Star Pattern
Print the pyramid Pattern
*
***
*****
*******
*********
rows = 5
for i in range(rows):
print(" " * (rows - i - 1) + "*" * (2 * i + 1))
*
***
*****
*******
*********| i | Space Calculation (5 - i - 1) | Star Calculation (2 * i + 1) | Resulting Row |
|---|---|---|---|
| 0 | 5 - 0 - 1 = 4 | 2(0) + 1 = 1 | ----* |
| 1 | 5 - 1 - 1 = 3 | 2(1) + 1 = 3 | ---*** |
| 2 | 5 - 2 - 1 = 2 | 2(2) + 1 = 5 | --***** |
| 3 | 5 - 3 - 1 = 1 | 2(3) + 1 = 7 | -******* |
| 4 | 5 - 4 - 1 = 0 | 2(4) + 1 = 9 | ********* |