Python For Loop Problems and Solutions: Practice Logic for Beginners

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
Iterationi valuestr(i) (String)str(i) * i (Calculation)Final Output
11"1""1" * 11
22"2""2" * 222
33"3""3" * 3333
44"4""4" * 44444
55"5""5" * 555555

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 jOutput
5range(5, 4, -1)55
4range(5, 3, -1)5,454
3range(5, 2, -1)5,4,3543
2range(5, 1, -1)5,4,3,25432
1range(5, 0, -1)5,4,3,2,154321

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 jj Iterationsnum Value (Before Print)Outputnum += 1 (After Print)
1range(1)0112
2range(2)0, 12, 32 33, 4
3range(3)0,1, 24, 5, 64 5 65, 6, 7
4range(4)0, 1, 2, 37, 8, 9, 107 8 9 108, 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))


    *
   ***
  *****
 *******
*********
iSpace Calculation (5 - i - 1)Star Calculation (2 * i + 1)Resulting Row
05 - 0 - 1 = 42(0) + 1 = 1----*
15 - 1 - 1 = 32(1) + 1 = 3---***
25 - 2 - 1 = 22(2) + 1 = 5--*****
35 - 3 - 1 = 12(3) + 1 = 7-*******
45 - 4 - 1 = 02(4) + 1 = 9*********