This page contains the collection of python programs for practice to enhance your python programming knowledge and bit the interviews.
Copy the below python program and execute it with the help of python compiler.
1 2 3 4 5 6 7 8 | num1 = input('Enter first number: ') num2 = input('Enter second number: ') #Perform add operation sum = float(num1) + float(num2) #Print the output print('The sum of {0} and {1} is: {2}'.format(num1, num2, sum)) |
1 2 3 4 5 6 7 8 | num1 = input('Enter first number: ') num2 = input('Enter second number: ') #Perform subtraction operation sum = float(num1) - float(num2) #Print the output print('The subtraction of {0} and {1} is: {2}'.format(num1, num2, sum)) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | n=int(input("Enter a number: ")) temp=n rev=0 while(n>0): dig=n%10 rev=rev*10+dig n=n//10 if(temp==rev) { print("The number is a palindrome!") }else { print("The number isn't a palindrome!") } |
1 2 3 4 5 6 7 8 9 10 | n=int(input("Enter the number: ")) rev=0 while(n>0): dig=n%10 rev=rev*10+dig n=n//10 print("The reversed number is:", rev) |
1 2 3 4 5 6 7 8 9 10 | year=int(input("Enter year to be checked:")) if(year%4==0 and year%100!=0 or year%400==0) { print("The year is a leap year!) } else { print("The year isn't a leap year!) } |
1 2 3 4 5 6 7 8 9 10 11 12 13 | num = int(input("Enter a number: ")) if num > 1: for i in range(2,num): if (num % i) == 0: print(num,"is not a prime number") print(i,"times",num//i,"is",num) break else: print(num,"is a prime number") else: print(num,"is not a prime number") |
1 2 3 4 5 6 7 8 9 10 | n=int(input("Enter the number: ")) a=list(map(int,str(n))) b=list(map(lambda x:x**3,a)) if(sum(b)==n): print("The number is an Armstrong number. ") else: print("The number is not an Armstrong number! ") |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | nterms = int(input("How many terms you want? ")) #define variables n1 = 0 n2 = 1 count = 2 #check if the number is positive or negative if nterms <= 0: print("Plese enter a positive integer") elif nterms == 1: print("Fibonacci sequence:") print(n1) else: print("Fibonacci sequence:") print(n1,",",n2,end=', ') while count < nterms: nth = n1 + n2 print(nth,end=' , ') # update values n1 = n2 n2 = nth count += 1 |
1 2 3 4 5 6 7 8 9 10 | num = int(input("Enter a number: ")) factorial = 1 if num < 0: print("Sorry, factorial does not exist for negative numbers") elif num == 0: print("The factorial of 0 is 1") else: for i in range(1,num + 1): factorial = factorial*i print("The factorial of",num,"is",factorial) |
1 2 3 4 5 6 7 | import calendar # Enter the month and year yy = int(input("Enter year: ")) mm = int(input("Enter month: ")) #display the calendar of entered month and year print(calendar.month(yy,mm)) |
Here is more python programs from beginners to advanced level for practice.
Hope above python programs for practice tutorial helps you.
Here is some more python assignments for practice:
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.