Simple Python Programs for Beginners
Print Hello World – Program 1 #
Program:
hello_world.py
# This is a simple Python program to print text on the screen
# The print() function is used to display output
print("Hello, World!")
Output:
Hello, World!
Add Two Numbers – Program 2 #
Program:
add_two_number.py
# This program adds two numbers provided by the user
# Input from user (input() returns a string, so we convert it to float or int)
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Add the two numbers
sum = num1 + num2
# Display the result
print("The sum of", num1, "and", num2, "is:", sum)
Output:
Enter first number: 10
Enter second number: 20
The sum of 10.0 and 20.0 is: 30.0
Maximum of Two Numbers – Program 3 #
Program:
max_two_numbers.py
# This program finds the maximum of two numbers
# Take two numbers as input
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
# Use if-else condition to compare the numbers
if a > b:
print(a, "is greater than", b)
elif a < b:
print(b, "is greater than", a)
else:
print("Both numbers are equal")
Output:
Enter first number: 10
Enter second number: 23
23.0 is greater than 10.0
Check if a Number is Positive, Negative, or 0 – Program 4 #
Program:
check_number.py
# Program to check if a number is positive, negative, or zero
# Take input from user
num = float(input("Enter a number: "))
# Check conditions using if-elif-else
if num > 0:
print("The number is positive.")
elif num < 0:
print("The number is negative.")
else:
print("The number is zero.")
Output:
Enter a number: 30
The number is positive.
Check if a Number is Odd or Even – Program 5 #
Program:
odd_even.py
# Program to check if a number is odd or even
# Take integer input from user
num = int(input("Enter an integer: "))
# Use modulus operator to find remainder when divided by 2
if num % 2 == 0:
print(num, "is an even number.")
else:
print(num, "is an odd number.")
Output:
Enter an integer: 20
20 is an even number.
Check Leap Year – Program 6 #
Program:
leap_year.py
# Program to check whether a given year is a leap year or not
# Take year input from user
year = int(input("Enter a year: "))
# Leap year rules:
# 1. If divisible by 4 → possible leap year
# 2. If divisible by 100 → must also be divisible by 400
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
Output:
Enter a year: 2024
2024 is a leap year.
Swap Two Numbers using three variables – Program 7 #
Program:
swap_two_numbers_3variables.py
# Program to swap two variables
# Take input from user
x = input("Enter value of x: ")
y = input("Enter value of y: ")
# Display original values
print("Before swapping: x =", x, "y =", y)
# Swap values using a temporary variable
temp = x
x = y
y = temp
# Display swapped values
print("After swapping: x =", x, "y =", y)
Output:
Enter value of x: 10
Enter value of y: 20
Before swapping: x = 10 y = 20
After swapping: x = 20 y = 10
Swap Two Numbers using two variables – Program 8 #
Program:
swap_two_numbers_2variables.py
# Program to swap two variables
# Take input from user
x = input("Enter value of x: ")
y = input("Enter value of y: ")
# Display original values
print("Before swapping: x =", x, "y =", y)
# Swap values using a temporary variable
temp = x
x = y
y = temp
# Display swapped values
print("After swapping: x =", x, "y =", y)
Output:
Enter value of x: 10
Enter value of y: 20
Before swapping: x = 10 y = 20
After swapping: x = 20 y = 10
Generate a Random Number – Program 9 #
Program:
generate_random_number.py
# Program to generate a random number between two values
# Import the random module
import random
# Take range input from user
start = int(input("Enter start value: "))
end = int(input("Enter end value: "))
# Generate a random number between start and end
rand_num = random.randint(start, end)
# Display the random number
print("Random number between", start, "and", end, "is:", rand_num)
Output:
Enter start value: 20
Enter end value: 80
Random number between 20 and 80 is: 31
Convert Kilometers to Miles – Program 10 #
Program:
km_to_miles.py
# Program to convert distance from kilometers to miles
# 1 kilometer = 0.621371 miles
km = float(input("Enter distance in kilometers: "))
# Convert to miles
miles = km * 0.621371
# Display the result
print(f"{km} kilometers is equal to {miles} miles.")
Output:
Enter distance in kilometers: 100
100.0 kilometers is equal to 62.137100000000004 miles.
Convert Celsius to Fahrenheit – Program 11 #
Program:
celsius_to_fahrenheit.py
# Program to convert temperature from Celsius to Fahrenheit
# Formula: F = (C × 9/5) + 32
celsius = float(input("Enter temperature in Celsius: "))
# Convert to Fahrenheit
fahrenheit = (celsius * 9/5) + 32
# Display the result
print(f"{celsius}° Celsius is equal to {fahrenheit}° Fahrenheit.")
Output:
Enter temperature in Celsius: 40
40.0° Celsius is equal to 104.0° Fahrenheit.
Find the Square Root – Program 12 #
Program:
square_root.py
# Program to find the square root of a number
# Import the math module to use the sqrt() function
import math
# Take input from user
num = float(input("Enter a number: "))
# Calculate the square root using math.sqrt()
sqrt = math.sqrt(num)
# Display the result
print("The square root of", num, "is:", sqrt)
Output:
Enter a number: 25
The square root of 25.0 is: 5.0
Calculate the Area of a Triangle – Program 13 #
Program:
area_of_triangle.py
# Program to find the area of a triangle using base and height
# Take input from user
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
# Calculate area using (1/2) * base * height
area = 0.5 * base * height
# Display the result
print("The area of the triangle is:", area)
Output:
Enter the base of the triangle: 10
Enter the height of the triangle: 30
The area of the triangle is: 150.0
Find Area of a Circle – Program 14 #
Program:
area_of_circle.py
# Program to calculate the area of a circle
# Import math module to use pi constant
import math
# Take input for radius
radius = float(input("Enter the radius of the circle: "))
# Calculate area using the formula: πr²
area = math.pi * radius ** 2
# Display the result
print("The area of the circle is:", area)
Output:
Enter the radius of the circle: 20
The area of the circle is: 1256.6370614359173
Find the Largest Among Three Numbers – Program 15 #
Program:
largest_three_numbers.py
# Program to find the largest among three numbers
# Take three numbers as input
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
# Compare using if-elif-else statements
if (a >= b) and (a >= c):
largest = a
elif (b >= a) and (b >= c):
largest = b
else:
largest = c
# Display the result
print("The largest number is:", largest)
Output:
Enter first number: 30
Enter second number: 50
Enter third number: 40
The largest number is: 50.0
Display the Multiplication Table – Program 16 #
Program:
multiplication_table.py
# Program to display the multiplication table of a number
# Take integer input from user
num = int(input("Enter a number to display its multiplication table: "))
# Use a for loop to print table from 1 to 10
print(f"Multiplication Table of {num}")
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
Output:
Enter a number to display its multiplication table: 5
Multiplication Table of 5
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
Print ASCII Value of a Character – Program 17 #
Program:
ascii_value_character.py
# Program to find ASCII value of a given character
# Take a single character input
char = input("Enter a character: ")
# Use ord() function to get ASCII value
ascii_value = ord(char)
# Display the result
print(f"The ASCII value of '{char}' is {ascii_value}.")
Output:
Enter a character: A
The ASCII value of 'A' is 65.
Compute the Power of a Number (using built-ins) – Program 18 #
Program:
power_of_number.py
# Program to compute the power of a number using built-in function
# Take base and exponent as input
base = float(input("Enter the base number: "))
exponent = float(input("Enter the exponent: "))
# Calculate power using pow() function
result = pow(base, exponent)
# Display the result
print(f"{base} raised to the power of {exponent} is {result}.")
Output:
Enter the base number: 2
Enter the exponent: 3
2.0 raised to the power of 3.0 is 8.0.
Find Simple Interest – Program 19 #
Program:
simple_interest.py
# Program to calculate Simple Interest
# Take input for Principal, Rate, and Time
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time in years: "))
# Formula for Simple Interest: (P × R × T) / 100
simple_interest = (principal * rate * time) / 100
# Display the result
print(f"Simple Interest = {simple_interest}")
Output:
Enter the principal amount: 1000
Enter the rate of interest: 8
Enter the time in years: 5
Simple Interest = 400.0
Find Compound Interest – Program 20 #
Program:
compound_interest.py
# Program to calculate Compound Interest
# Common inputs
principal = float(input("Enter principal (P): "))
rate = float(input("Enter annual interest rate in % (R): "))
time = float(input("Enter time in years (T): "))
# Compounded annually
amount_annual = principal * (1 + rate / 100) ** time
compound_interest_annual = amount_annual - principal
print("\n--- Compounded Annually ---")
print(f"Amount (A) = {amount_annual:.2f}")
print(f"Compound Interest (CI) = {compound_interest_annual:.2f}")
Output:
Enter principal (P): 10000
Enter annual interest rate in % (R): 8
Enter time in years (T): 5
--- Compounded Annually ---
Amount (A) = 14693.28
Compound Interest (CI) = 4693.28