Are you in search of simple python programs? Your search ends here.
Simple Python Programs 1 : Display Multiplication Table of n
# Input the number for which you want to generate the multiplication table
n = int(input("Enter the number for the multiplication table: "))
# Input the range limit for the multiplication table
range_limit = int(input("Enter the range limit: "))
# Print a header for the table
print(f"Multiplication Table for {n} up to {range_limit}:\n")
# Use a for loop to calculate and display the table
for i in range(1, range_limit + 1):
product = n * i # Calculate the product of n and i
print(f"{n} x {i} = {product}") # Display the multiplication step
Output:
Enter the number for the multiplication table:
5
Enter the range limit:
10
Multiplication Table for 5 up to 10:
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
Simple Python Programs 2 : Print Factors of Given Number
# Input a number from the user
num = int(input("Enter a number: "))
# Initialize an empty list to store the factors
factors = []
# Find the factors of the given number
for i in range(1, num + 1):
if num % i == 0:
factors.append(i)
# Print the factors
print(f"Factors of {num} are:")
for factor in factors:
print(factor)
Output:
Enter a number:
6
Factors of 6 are:
1
2
3
6
Simple Python Programs 3 : Print Prime Factors of a Number
# Input a number from the user
num = int(input("Enter a number: "))
# Initialize an empty list to store the prime factors
prime_factors = []
# Function to check if a number is prime
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# Find and print the prime factors of the given number
for i in range(2, num + 1):
while num % i == 0 and is_prime(i):
prime_factors.append(i)
num //= i
# Print the prime factors
print(f"Prime factors of the given number are: {prime_factors}")
Output:
Enter a number:
6
Prime factors of the given number are: [2, 3]
Simple Python Programs 4 : Input Year and Check Whether Leap Year
# Input a year from the user
year = int(input("Enter a year: "))
# Check if it's a leap year
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Output:
Enter a year:
2000
2000 is a leap year.
Enter a year:
2023
2023 is not a leap year.
Simple Python Programs 5 : Print Prime Numbers 1 to 100
# Function to check if a number is prime
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
# Print prime numbers from 1 to 100
print("Prime numbers from 1 to 100:")
for num in range(1, 101):
if is_prime(num):
print(num, end=" ")
# Print a newline for better formatting
print()
Output:
Prime numbers from 1 to 100:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Simple Python Programs 6 : Reverse of a Number
# Input a number from the user
num = int(input("Enter a number: "))
# Initialize a variable to store the reversed number
reversed_num = 0
# Reverse the number
while num > 0:
# Get the last digit of the number
last_digit = num % 10
# Add the last digit to the reversed number (shift left)
reversed_num = reversed_num * 10 + last_digit
# Remove the last digit from the original number
num //= 10
# Print the reversed number
print(f"The reversed number is: {reversed_num}")
Output:
Enter a number:
456789
The reversed number is: 987654
Simple Python Programs 7 : Calculate Sum of Series
# Input the first term (a), common difference (d), and the number of terms (n)
a = float(input("Enter the first term (a): "))
d = float(input("Enter the common difference (d): "))
n = int(input("Enter the number of terms (n): "))
# Calculate the sum of the series
# Sn = (n / 2) * [2a + (n-1) * d]
series_sum = (n / 2) * (2 * a + (n - 1) * d)
# Print the sum of the series
print(f"The sum of the series is: {series_sum}")
Output:
Enter the first term (a):
2
Enter the common difference (d):
3
Enter the number of terms (n):
4
The sum of the series is: 26.0
Simple Python Programs 8 : Calculate the Area of a Circle
# Input the radius of the circle from the user
radius = float(input("Enter the radius of the circle: "))
# Calculate the area of the circle using the formula: Area = π * r^2
pi = 3.14159 # A close approximation of π
area = pi * radius**2
# Display the calculated area
print(f"The area of the circle with radius {radius} is {area:.2f}")
Output:
Enter the radius of the circle:
5
The area of the circle with radius 5.0 is 78.54
Simple Python Programs 9 : Check if a Number is Even or Odd
# Input a number from the user
number = int(input("Enter a number: "))
# Check if the number is even or odd
if number % 2 == 0:
print(f"{number} is an even number.")
else:
print(f"{number} is an odd number.")
Output:
Enter a number:
2
2 is an even number.
Enter a number:
5
5 is an odd number.
Simple Python Programs 10 : Calculate the Square Root
import math
# Input a number from the user
number = float(input("Enter a number: "))
# Calculate the square root
sqrt = math.sqrt(number)
# Display the square root
print(f"The square root of {number} is: {sqrt}")
Output:
Enter a number:
4
The square root of 4.0 is: 2.0
Simple Python Programs 11 : Calculate the Area of a Rectangle
# Input length and width from the user
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
# Calculate the area
area = length * width
# Display the area
print(f"The area of the rectangle is: {area}")
Output:
Enter the length of the rectangle:
5
Enter the width of the rectangle:
8
The area of the rectangle is: 40.0
Simple Python Programs 12 : Find the Maximum of Three Numbers
# Input three numbers from the user
a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
c = float(input("Enter the third number: "))
# Find the maximum using the max function
maximum = max(a, b, c)
# Display the maximum number
print(f"The maximum number is: {maximum}")
Output:
Enter the first number:
4
Enter the second number:
5
Enter the third number:
6
The maximum number is: 6.0
Simple Python Programs 13 : Convert Temperature from Celsius to Fahrenheit
# Input temperature in Celsius from the user
celsius = float(input("Enter temperature in Celsius: "))
# Convert to Fahrenheit using the formula
fahrenheit = (celsius * 9/5) + 32
# Display the temperature in Fahrenheit
print(f"{celsius}°C is equal to {fahrenheit}°F")
Output:
Enter temperature in Celsius:
35
35.0°C is equal to 95.0°F
Simple Python Programs 14 : Calculate Compound Interest
# Input principal amount, rate, time, and number of times interest is compounded annually
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the annual interest rate (as a decimal): "))
time = float(input("Enter the time (in years): "))
n = int(input("Enter the number of times interest is compounded annually: "))
# Calculate the compound interest
compound_interest = principal * (1 + rate/n)**(n*time) - principal
# Display the compound interest
print(f"The compound interest is: {compound_interest:.2f}")
Output:
Enter the principal amount:
1000
Enter the annual interest rate (as a decimal):
0.05
Enter the time (in years):
3
Enter the number of times interest is compounded annually:
4
The compound interest is: 160.75
Simple Python Programs 15 : Calculate the Fibonacci Sequence
# Input the number of terms in the Fibonacci sequence
n = int(input("Enter the number of terms: "))
# Initialize the first two terms
a, b = 0, 1
# Generate and print the Fibonacci sequence
print("Fibonacci Sequence:")
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
Output:
Enter the number of terms:
15
Fibonacci Sequence:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
2 thoughts on “15 Simple Python Programs with Output Please Don’t Miss”