Python Program Factory

Click on the program to view the source code
Program 1. Write Python program to find sum of two numbers.
a = int ( input ( " Enter first number : " ) ) b = int ( input ( " Enter second number : " ) ) s = a + b print ( " Sum of two numbers is " , s )
Program 2. Python program to find product of two numbers
a=int ( input (" Enter first number : " ) ) b=int (input ("Enter second number: ")) p=a*b print( " Product of two numbers is : " , p)
Program 3. Write Python program to find average of two numbers.
a=int(input(" Enter first number :")) b=int(input(" Enter second number : ")) s=(a+b)/2 print(" Average of two numbers is",s)
Program 4. Python program to find print x raise power to y
x = int ( input (" Enter value of x : " ) ) y = int (input ("Enter value of y : ")) ans=x ** y print( " x raise to power y is : " , ans)
Program 5. Write Python program to find simple interest.
p=float(input(" Enter principal :")) t=float(input(" Enter time :")) r=float(input(" Enter rate :")) si=(p*t*r)/100 print(" Simple Interst is",si)
Program 6. Python program to check whether the given number is even or odd
n=int(input("Enter a number:")) if n%2==0: print("Even") else: print("Odd")
Program 7. Python program to check whether the given number is divisible by 5 or not.
n=int ( input (" Enter a number : " ) ) r = n % 5 if r ==0: print(n , " is divisible by 5") else: print( n , " is not divisible by 5 ")
Program 8. Python program to display the greater number among two numbers.
a=int(input("Enter first number: ")) b=int(input("Enter second number: ")) if a>b: print(a,"is greater") else: print(b,"is greater")
Program 9. Python program to display the smaller number among two numbers.
a=int(input("Enter first number: ")) b=int(input("Enter second number: ")) if a<b: print(a,"is smaller") else: print(b,"is smaller")
Program 10 . Python program to display whether the given number is positive, negative or zero
n = int ( input (" Enter a number : " ) ) if n > 0 : print( n , " is positive ") elif n < 0 : print( n , " is negative ") else: print(n , " is zero")
Program 11. Python program to display the greatest number among three numbers.
a=int(input("Enter first number: ")) b=int(input("Enter second number: ")) c=int(input("Enter third number: ")) if a>b and a>c: print(a,"is greatest") elif b>a and b>c: print(b,"is greatest") else: print(c,"is greatest")
Program 12. Python program to display the smallest number among three numbers.
a=int(input("Enter first number: ")) b=int(input("Enter second number: ")) c=int(input("Enter third number: ")) if a<b and a<c: print(a,"is smallest") elif b<a and b<c: print(b,"is smallest") else: print(c,"is smallest")
Program 13. Python program to display middle number among three numbers
a = int ( input (" Enter first number : " ) ) b = int ( input (" Enter second number : " ) ) c = int ( input (" Enter third number : " ) ) if a>b and a<c or a<b and a>c : print( a , " is middle number ") elif b>a and b<c or b>a and b<c : print( b , " is middle number ") else: print(c , "is middle number")
Program 14. Python program to display greatest number among four numbers
a = int ( input (" Enter first number : " ) ) b = int ( input (" Enter second number : " ) ) c = int ( input (" Enter third number : " ) ) d = int ( input (" Enter fourth number : " ) ) if a>b and a>c and a>d: print( a , " is greatest number ") elif b>a and b>c and b>d: print( b , " is greatest number ") elif c>a and c>b and c>d: print(c , " is greatest number") else: print(d , "is greatest number")
Program 15. Python program to display smallest number among four numbers
a = int ( input (" Enter first number : " ) ) b = int ( input (" Enter second number : " ) ) c = int ( input (" Enter third number : " ) ) d = int ( input (" Enter fourth number : " ) ) if a<b and a<c and a<d: print( a , " is smallest number ") elif b < a and b<c and b<d: print( b , " is smallest number ") elif c<a and c<b and c<d: print(c , " is smallest number") else: print(d , "is smallest number")
Program 16. Python program to display first five natural numbers using for loop
for i in range(1,6,1): print(i)
Program 17. Python program to print 1,2,3,......10.
for i in range(1,11): print(i)
Program 18. Python program to display first five natural numbers using while loop
i=1 while i<6: print(i) i+=1
Program 19. Python program to display first ten even numbers
a=2 for i in range(1,11): print(a) a=a+2
Program 20. Python program to display first twenty odd numbers
a=1 for i in range(1,21): print(a , end=" ") a=a+2
Program 21. Python program to find factorial of given number.
[Note: Factorial of any number is product of all lesser positive number. Example: 5 !=5*4*3*2*1=120 ]
num = int ( input ( “ Enter a number : ” ) ) f = 1 for i in range ( num , 1 , -1 ) : f = f * i print ( “ Factorial of ” , num , “ is ”, f )
Program 22. Python program to display multiplication table of given number.
num = int ( input ( " Enter a number : " ) ) for i in range ( 1, 11 ) : ans = num * i print ( ans )
Program 23. Python program to find the sum of the first ten natural numbers.
sum=0 for i in range ( 1, 11 ) : sum = sum + i print ( " Sum of ten natural numbers is " , sum )
Program 24. Pyhton Program to find HCF of two numbers.
num1 = int ( input ( " Enter first number: ")) num2 = int ( input ( " Enter second number: ")) a = num1 b = num2 while b != 0: temp = b b = a % b a = temp hcf = a print( " The HCF of ", num1, " and " , num2 , "is :", hcf)
Program 25. Pyhton Program to find LCM of two numbers.
num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) a = num1 b = num2 while b != 0: temp = b b = a % b a = temp hcf = a lcm = abs(num1 * num2) // hcf print( " The LCM of " , num1, " and ", num2 , " is : " , lcm)
Program 26. Python program to print the factors of a given number.
num = int ( input ( “ Enter a number : ” ) ) for i in range(1,num+1): if num % i == 0: print (i)
Program 27. Python program to find the sum of the digits of an entered number.
num = int ( input ( " Enter a number : " ) ) sum = 0 while num>0: r = num % 10 sum = sum + r num = num // 10 print (" Sum of digits is " , sum)
Program 28. Python program to reverse a given number.
num = int ( input ( " Enter a number : " ) ) rev = 0 while num>0: r = num % 10 rev = rev * 10 + r num = num // 10 print (" Reverse of given number is " , rev)
Program 29. Python to test whether the supplied number is Palindrome number or not.
Hint: A word, phrase, or sequence that reads the same backwards as forwards is called Palindrome. Example : 121 , 101 etc
n = int ( input ( " Enter a number : " ) ) num=n rev = 0 while n>0: r = n % 10 rev = rev*10 + r n = n // 10 if num==rev: print( " Palindrome number ") else: print(" Not Palindrome number ")
Program 30. Python program to check whether the given number is Armstrong or not.
An Armstrong number is a number which equal to the sum of the cubes of its individual digits. For example: 153 is an Armstrong number as 153=(1)3+(5)3+(3)3=1+125+27=153
n = int ( input ( " Enter a number : " ) ) num=n arm = 0 while n>0: r = n % 10 arm = arm + r**3 n = n // 10 if num==arm: print( " Armstrong number ") else: print(" Not Armstrong number ")
Program 31. Python program to display the series: 0, 1, 1, 2, 3, ... up to the 10th term(Fibonacci Series)
An integer in the infinite sequence 0,1, 1, 2, 3, 5, 8, 13, ..... of which the first two terms are 0 and 1 and each succeeding term is the sum of the two immediately preceding is called Fibonacci Series.
a=0 b=1 for i in range(1,11,1): print (a , end=" ") c=a+b a=b b=c
Program 32. Python program to display 7,22,11,34,17,..... up to 10th terms(Hailstone Series)
Start with any integer value greater than 0, say x. If x is even, then the next value in the series is x/2; if x is odd, then the next value in the series is 3x + 1. This type of series is called Hailstone series
a=7 for i in range(1,11,1): print (a , end=" ") if a%2==0: a=a//2 else: a=3*a+1
Program 33. Python program to continuously add numbers entered by the user. Stop taking input when the sum exceeds 50.
total_sum = 0 while total_sum <= 50 : num = int(input("Enter a number to add: ")) total_sum = total_sum + num print("Current sum:", total_sum) print("Loop stopped. Sum exceeded 50.")
Program 34. Python program to print numbers from 1 to 20, but skip multiples of 3 using continue.
for i in range(1, 21): if i % 3 == 0: continue print ( i, end = " ")
Program 35. Python program to input a number and check whether it is prime or not .
num = int ( input ( " Enter a number : " ) ) c = 0 for i in range(1, num + 1): if num % i == 0: c += 1 if c == 2: print( num , " is prime" ) else: print( num , " is not prime" )
Program 36. Python program to print prime numbers between 1 to 100.
for num in range(1,101): c = 0 for i in range(1, num + 1): if num % i == 0: c += 1 if c == 2: print( num , end = " " )
Program 37. Python program that has given a list of numbers, find the first prime number using a loop. If a prime is found, break the loop. numbers = [10, 15, 16, 17, 20]
numbers = [10, 15, 16, 17, 20] for num in numbers: c = 0 for i in range(1, num + 1): if num % i == 0: c += 1 if c == 2: print("First prime number is:", num) break
Program 38. Python program to nested loop to print the multiplication table from 1 to 5.
for i in range(1,6): for j in range(1,11): print(i*j , end = " ") print()
Program 39. Python program to create a list of 10 integers and print the list.
numbers = [ ] for i in range( 1 , 11 ): print("Enter integer", i , end=": ") num = int ( input ( ) ) numbers.append ( num ) print ( numbers )
Program 40. Python program that takes 5 numbers as input from the user and stores them in a list, then prints the list and its sum.
numbers = [ ] for i in range(1, 6): print("Enter integer", i , end=": ") num=int ( input ( ) ) numbers.append(num) total_sum = sum(numbers) print("The list of numbers is :", numbers) print("The sum of the numbers is :", total_sum)
Program 41. Python program that takes ten numbers from the user and stores them in a list. Separate even and odd numbers into two different lists, then print both lists.
all_numbers = [ ] even_numbers = [ ] odd_numbers = [ ] for i in range(1,11): print("Enter integer", i , end=": ") num = int ( input ( ) ) all_numbers.append(num) for num in all_numbers: if num % 2 == 0: even_numbers.append(num) else: odd_numbers.append(num) print("Even numbers:", even_numbers) print("Odd numbers:", odd_numbers)
Program 42. Python program to input any 10 integers in a list and display the reverse of given list.
integers = [ ] for i in range(10): print("Enter integer", i +1 , end=": ") num = int ( input ( ) ) integers.append(num) integers.reverse( ) print(" Reversed List is :", integers)
Program 43. Python program to create a list with 10 elements and find the greatest and smallest numbers in the list.
integers = [ ] for i in range(10): print("Enter integer", i +1 , end=": ") num = int ( input ( ) ) integers.append(num) g=max(integers) s=min(integers) print(" Greatest number is : " , g ) print( " Smallest number is : " , s )
Program 44. Python program to append a new element to the end of a list and print the updated list.
my_list = [10, 20, 30, 40] new_element = int ( input ( " Enter a number : ")) my_list.append(new_element) print("Updated list:", my_list)
Program 45. Python program to print the length of a list.
my_list = ["apple", "banana", "mango", "litchi"] list_length = len(my_list) print("Length of the list:", list_length)
Program 46. Python program to check if a given element exists in the list.
my_list = [10, 25, 40, 55, 70] search_element = int ( input ( " Enter an element to be searched : " )) if search_element in my_list: print( " Element ", search_element , "exists in the list.") else: print( " Element " , search_element, " does not exist in the list.")
Program 47. Python program to count the number of times a specific element appears in the list.
my_list = [1, 3, 7, 3, 8, 3, 5, 9, 3] target = 3 occurrences = my_list.count(target) print( " The element " , target, " appears " , occurrences, " times in the list.")
Program 48. Python program to find the index of a specific element in a list.
colors = ["red", "green", "blue", "yellow"] target = input ( " Enter a element : ") if target in colors: position = colors.index(target) print( " The index of " , target , " is : " , position) else: print( target , " is not present in the list.")
Program 49. Python program to create a dictionary with 5 key-value pairs and print it
student = { "name": "Ram Prasad", "age": 14, "grade": "A", "major": "Computer Science", "gpa": 3.8 } print(student)
Program 50. Python program to access a value from a dictionary using a key.
student = {"name": "Ram Prasad ", "age": 14, "major": "Computer Science"} student_name = student["name"] print("Name:", student_name) student_age = student.get("age") print("Age:", student_age)
Program 51. Python program to add a new key-value pair to an existing dictionary and print the updated dictionary.
student = {"name": "Alex Lal", "age": 14, "major": "Computer Science"} student["email"] = "alex@example.com" print("Updated Dictionary:", student)
Program 52. Python program to remove a key-value pair from a dictionary.
student = {"name": "Ram Prasad", "age": 14, "major": "Computer Science", "gpa": 3.8} removed_value = student.pop("gpa") print("Removed GPA:", removed_value) print("Updated Dictionary:", student)
Program 53. Python program to check if a specific key exists in a dictionary.
student = {"name": "Alex Lal ", "age": 14, "major": "Computer Science"} key_to_check = "age" if key_to_check in student: print( " Key ", key_to_check , " exists in the dictionary.") else: print( " Key " , key_to_check, " does not exist.")
Program 54. Python program to get all the keys of a dictionary.
student = {"name": "Alex Lal", "age": 14, "major": "Computer Science"} keys_list = list(student.keys()) print("Keys:", keys_list)
Program 55. Python program to get all the values of a dictionary.
student = {"name": "Alex Lal ", "age": 14, "major": "Computer Science"} values_list = list(student.values()) print("Values:", values_list)
Program 56. Python program that create a dictionary for student with keys "name","age", and "marks". Take input from user and print it.
student={ } student["name"]=input( " Enter student name : ") student["age"]=int(input( " Enter student age: ")) student["marks"]=int(input( " Enter student marks:")) print( " student name is ",student["name"]) print( " student age is ",student["age"]) print( " student marks is ",student["marks"])
Program 57. Python program that create a dictionary for student with keys "name","age", and "marks". Take input from user and print it. Then remove age and display only name and marks
student={ } student["name"]=input( " Enter student name : ") student["age"]=int(input( " Enter student age: ")) student["marks"]=int(input( " Enter student marks:")) print( " student name is ",student["name"]) print( " student age is ",student["age"]) print( " student marks is ",student["marks"]) del student["age"] print(student)
Program 59. Python program to display: 1 12 123 1234 12345
for i in range ( 1 , 5+1 ) : for j in range ( 1 , i+1 ) : print(j, end = " ") print( )
Program 60. Python program to display: 1 22 333 4444 55555
for i in range ( 1 , 5+1 ) : for j in range ( 1 , i+1 ) : print( i, end = " ") print( )
Back