You are currently viewing Python Loop Program for Practice

Python Loop Program for Practice

In previous articles, we have learned about Loops in Python. In this article, we will write some Python programs using loops. We will use the concept we have learned in the previous article about loops. These are simple programs for beginners.

Python program to print the hello world

#for i in range(1,11):
for i in range(10,0,-1):
	print("Hello World")
input()

Python Program to sum the number in given Range

sum=0
for i in range(1,101):
	sum=sum+i
print("Sum = ",sum)
input()

Program to check number is prime or not

import math
num=int(input("Enter the Number :   "))
f=0
for i in range(2,int(math.sqrt(num))+1):
	if(num%i==0):
		f=1
		break
if(f==0 and num>=2):
	print(num," Is prime")
else: 		
	print(num," Is not Prime")
input()

Program to find the sum of even number and odd number in the given range

even=0
odd=0
for i in range(1,50001):
	if(i%2==0):
		even=even+i
	else:
		odd=odd+i
print("Sum of Even Numbers = ",even)
print("Sum of Odd Numbers = ",odd)
input()

Program to print the table

num=int(input("Enter the Number :  "))
for i in range(1,11):
	print(num," * ",i," = ",num*i)
input()

Program to check number is perfect or not

num=int(input("Enter the Number :  "))
sum=0
#x=num//2+1
for i in range(1,num//2+1):
	if(num%i==0):
		sum=sum+i
if(sum==num):
	print("Perfect Number")
else:
	print("Not Perfect Number")

Program to get the total multiples of 19

c=0
for i in range(19,1000):
	if(i%19==0):
		print(i,end="\t")
		c=c+1
print("\nTotal Multiples of 19 = ",c)
input()

Program to calculate the total number of palindrome in a given range

c=0
for num in range(1000,50000):
	rev=0
	temp=num
	while(temp!=0):
		r=temp%10
		rev=rev*10+r
		temp=temp//10
	if(num==rev):
		print(num,end="\t")
		c=c+1
print("\nTotal Palindrome Numbers = ",c)
input()

Program to print the table from 1 to 12

for num in range(1,21):
	print("Table of ",num," is")
	for i in range(1,11):
		print(num," *",i," = ",num*i)
	print("\n")
input()

Program to find the prime number from 2 to 1000

import math
c=0
for num in range(2,1000):
	f=0
	for i in range(2,int(math.sqrt(num))+1):
		if(num%i==0):
			f=1
			break
	if(f==0):
		print(num,end="\t")
		c=c+1
print("\nTotal Prime Numbers = ",c)
input()

Program to calculate the total prime number between 2 to 100

import math
c=0
num=2
while(c<100):
	f=0
	for i in range(2,int(math.sqrt(num))+1):
		if(num%i==0):
			f=1
			break
	if(f==0):
		print(num,end="\t")
		c=c+1
	num=num+1
print("\nTotal Prime Numbers = ",c)
input()

Program to print a simple pattern in Python using the loop

for i in range(1,6):
	for j in range(1,6):
		print("*",end="")
	print()
input()

Program to print a pattern in Python using the loop.

for i in range(1,6):
	for j in range(1,6):
		print(j,end="")
	print()
input()

Program to print the pattern

for i in range(1,6):
	for j in range(1,6):
		print(i,end="")
	print()
input()

Program to print the pattern

for i in range(1,6):
	for j in range(5,0,-1):
		print(j,end="")
	print()
input()

Program to print pattern

for i in range(1,6):
	for j in range(1,6):
		print(6-i,end="")
	print()
input()


Program to print the pattern

x=1
for i in range(1,6):
	for j in range(1,6):
		print(x,end="\t")
		x=x+1
	print()
input()

Program to print the pattern

for i in range(1,6):
	for j in range(1,6):
		if(i%2==0):
			print(j,end="")
		else:
			print("*",end="")
	print()
input()

Program to print the pattern

for i in range(1,6):
	for j in range(1,6):
		if(j%2==0):
			print("*",end="")
		else:
			print(j,end="")
	print()
input()

Program to print the pattern

for i in range(1,6):
	for j in range(1,i+1):
			print("*",end="")
	print()
input()

Program to print the pattern

for i in range(1,6):
	for j in range(1,i+1):
			print(j,end="")
	print()
input()

Program to print the pattern

for i in range(1,6):
	for j in range(i,0,-1):
			print(j,end="")
	print()
input()

Program to print the pattern

x=0
for i in range(1,6):
	x=i
	for j in range(1,i+1):
			print(x,end= ' ')
			x=x+1
	print()


Program to print the pattern

x=0
for i in range(1,6):
	x=2*i-1
	for j in range(1,i+1):
			print(x,end="")
			x=x+1

Program to print the pattern

x=0
y=9
for i in range(1,6):
	x=y
	for j in range(1,i+1):
			print(x,end="")
			x=x-1
	print()
	y=y-1
input()

Program to print the pattern

for i in range(1,6):
	for j in range(1,i+1):
		print(i%2,end="")
	print()
input()

Program to print the pattern

for i in range(1,6):
	for j in range(1,2*i):
		print(j,end="")
	print()
input()

Program to print the pattern

for i in range(1,6):
	for j in range(i,6):
		print("*",end="")
	print()
input()
for i in range(5,0,-1):
	for j in range(1,i+1):
		print(j,end="")
	print()
input()

Program to print the pattern

for i in range(1,6):
	for j in range(5,i-1,-1):
		print(j,end="")
	print()
input()

Program to print the pattern

for i in range(1,6):
	for j in range(1,6-i):
		print(" ",end="")
	for j in range(1,i+1):
		print("*",end="")
	print()

for i in range(1,6):
	for j in range(1,6-i):
		print(" ",end="")
	for j in range(1,i+1):
		print("* ",end="")
	print()
input()

Program to print the pattern

for i in range(1,6):
	for j in range(1,6-i):
		print(" ",end="")
	for j in range(1,2*i):
		print("*",end="")
	print()
num=int(input("Enter the Number of Rows :  "))
for i in range(1,num+1):
	for j in range(1,num+1):
		if(i==1 or i==num or j==1 or j==num or i==j or i+j==num+1):
			print("* ",end="")
		else:
			print("  ",end="")
	print()
num=int(input("Enter the Number of Rows :  "))
for i in range(1,num+1):
	for j in range(1,num+1):
		if(i==1):
			print(j,end=" ")
		elif(i==num):
			print(num+1-j,end=" ")
		elif(j==1):
			print(i,end=" ")
		elif(j==num):
			print(num+1-i,end=" ")
		elif(i==j or i+j==num+1):
			if(i<=num/2):
				print(j,end=" ")
			else:
				print(num+1-j,end=" ")
		else:
			print(" ",end=" ")
	print()

for i in range(1,6):
	for j in range(1,i):
		print(" ",end="")
	for j in range(1,i+1):
		print("*",end="")
	for j in range(1,6-i):
		print("    ",end="")
	for j in range(1,i+1):
		print("*",end="")
	print()
for i in range(4,0,-1):
	for j in range(1,i):
		print(" ",end="")
	for j in range(1,i+1):
		print("*",end="")
	for j in range(1,6-i):
		print("    ",end="")
	for j in range(1,i+1):
		print("*",end="")
	print()

#febonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144..........
a=0
b=1
sum=a+b#initialization
num=int(input("Enter the Last Term Range :  "))#initialization
print(a,"\t",b,end="\t")
while(sum<=num):#condition
	print(sum,end="\t")
	a=b		#updation
	b=sum	#updation
	sum=a+b	#updation
input()

Program to find the sum of digit

num=int(input("Enter the Number :  "))
sum=0
while(num!=0):
	r=num%10
	sum=sum+r
	#num=int(num/10)
	num=num//10
print("Sum of Digits = ",sum)
input()

Leave a Reply