Program of loop in Python
Python program to print a multiplication table of a given number.
# if the given range is 10
given_number = 5
for i in range(11):
print (given_number,” x”,i,” =”,5*i)
Output
5 x 0 = 0
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Python program to check if a given number is an Armstrong number.
# the given number
given_number = 153
# convert given number to string
# so that we can iterate through it
given_number = str(given_number)
# store the length of the string for future use
string_length = len(given_number)
# initialize a sum variable with
# 0 value to store the sum of the product of
# each digit
sum = 0
# iterate through the given string
for i in given_number:
sum += int(i)**string_length
# if the sum matches the given string
# its an Armstrong number
if sum == int(given_number):
print(“The given number”,given_number,”is an Armstrong number.”)
# if the sum do not match with the given string
# its an Armstrong number
else:
print(“The given number”,given_number,”is Not an Armstrong number.”)
Output
The given number 153 is an Armstrong number.
# Prints all letters except ‘e’ and ‘s’
i = 0
a = ‘geeksforgeeks’
while i < len(a):
if a[i] == ‘e’ or a[i] == ‘s’:
i += 1
continue
print(‘Current Letter :’, a[i])
i += 1
Output
Current Letter : g
Current Letter : k
Current Letter : f
Current Letter : o
Current Letter : r
Current Letter : g
Current Letter : k
# break the loop as soon it sees ‘e’
# or ‘s’
i = 0
a = ‘geeksforgeeks’
while i < len(a):
if a[i] == ‘e’ or a[i] == ‘s’:
i += 1
break
print(‘Current Letter :’, a[i])
i += 1
Output
Current Letter : g
# Python program to demonstrate
# while-else loop
i = 0
while i < 4:
i += 1
print(i)
else:
print(“No Break\n”)
i = 0
while i < 4:
i += 1
print(i)
break
else: # Not executed as there is a break
print(“No Break”)
Output
1
2
3
4
No Break
1
Printing multiplication table using Python nested for loops.
# Running outer loop from 2 to 3
for i in range(2, 4):
# Printing inside the outer loop
# Running inner loop from 1 to 10
for j in range(1, 11):
# Printing inside the inner loop
print(i, “*”, j, “=”, i*j)
# Printing inside the outer loop
print()
Output
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
Thank you for taking the time to read this post.I hope you found it helpful!”
Social Media Links of Tech DCode
👉🏻 YouTube
https://www.youtube.com/channel/UCjJnEdeugftBwQ3yMuD4B_A
https://www.instagram.com/thetechdcode/
👉🏻Facebook Page
https://www.facebook.com/thetechdcode
👉🏻Telegram Channel
https://t.me/thetechdcode
👉🏻Tech DCode Linktree
https://linktr.ee/thetechdcode
👉🏻My Personal Handles
https://linktr.ee/virtualshivamin
Social Media Links of SHIVAM SINGH (OWNER) :
👉🏻Instagram :
https://www.instagram.com/virtualshivamin/
👉🏻Facebook Page :
https://www.facebook.com/virtualshivamin/
👉🏻Twitter :
https://twitter.com/virtualshivamin/
👉🏻Personal Linktree :
https://linktr.ee/virtualshivamin