Top Ad unit 728 × 90

Libreoffice

Libreoffice Writer

Python Basic Programs


    Python Basic Programs



    Add two numbers in Python

a = input("First number: ")

b = input("Second number: ")

res = float(a) + float(b)

print(res)


Find Maximum of two numbers in Python



a = 7
b = 3
print(max(a, b))

or

a = 7
b = 3

if a > b:
    print(a)
else:
    print(b)


Output


7



Factorial of a Number - Python



n = 6

 fact = 1

for i in range(1, n + 1):
    fact *= i
print(fact)


Output

720



Python Program for Simple Interest



principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the interest rate (%): "))
time = float(input("Enter the time (years): "))

simple_interest = (principal * rate * time) / 100

print("Simple Interest:",simple_interest)



Python Program to Check Armstrong Number




num = int(input("Enter a number: "))
n = num
power = len(str(num))
total = 0

while n > 0:
    digit = n % 10
    total += digit ** power
    n //= 10

if total == num:
    print("Armstrong Number")
else:
    print("Not an Armstrong Number")


Output

153 is an Armstrong number


Python Program for Compound Interest




P = 1200   
R = 5    
T = 2      

A = P * (1 + R/100) ** T
CI = A - P

print("Compound interest:", CI)


Output


123


Python Program for Compound Interest (Taking Input from User)



p = int(input("Principal amount: "))
r = int(input("Rate of interest: "))
t = int(input("Time in years: "))


Amt = p * (pow((1 + r / 100), t))
CI = Amt - p

print("Compound interest:", CI)



Python Program to Find Area of a Circle



PI = 3.142
r = 5 # radius
area = PI * (r * r)
print(area)


Output

78.55


Python Program to Find Area of a Rectangle




length= float(input("Enter the length of the rectangle: "))
width= float(input("Enter the width of the rectangle: "))

area = (length*width)
print("The area of the rectangle is:", area)



Python Program to Find Area of a Square



side= float(input("Enter the Side of the Square: "))
area=(side*side)
print("The area of the Square is:", area)



Python Program to Find Area of a Triangle



base= float(input("Enter the base of the triangle: "))
height= float(input("Enter the height of the triangle: "))

area = (base*height)/2
print("The area of the triangle is:" area)


Python Program to Find Area of a  trapezium



height = float(input("Height of trapezoid: "))
base1 = float(input('Base one value: '))
base2 = float(input('Base two value: '))
area = ((base1 + base2) / 2) * height
print("The Area of Trapezium:", area)


Python Program to Find Area of a Parallelogram


base = float(input('Length of base: '))
height = float(input('Measurement of height: '))
area = base * height
print("Area is: ", area)



Python Program to Find Area of a Sphere



pi=22/7
radian= float(input('Radius of sphere: '))
surface area = 4 * pi * radian **2
volume = (4/3) * (pi * radian ** 3)
print("Surface Area is: ", surface area)
print("Volume is: ", volume)



Python Program to kilometers, the converted values in meters, centimeters, inches, and feet



km=float(input("Enter The Kilometer : "));
m=km*1000;
cm=m*100;
i=cm/2.54;
ft=i/12;
print("Kilometers : ",km);
print("Meters  : ",m);
print("Centimeters : ",cm);
print("Inches : ",i);
print("Feet : ",ft);



Python Program to convert minutes into hours



min=int(input("Enter time in minutes"))
h=min//60
m=min%60
print("Hours=",h)
print("Minutes=",m)


Student Mark Calculation in Python



m1=int(input("Enter the marks 1:"))
m2=int(input("Enter the mark 2:"))
m3=int(input("Enter the mark 3:"))
m4=int(input("Enter the mark 4:"))
m5=int(input("Enter the mark 5:"))
total=m1+m2+m3+m4+m5
per=total/5
print("Total Marks:",total)
print("Percentage Marks:",per)


Convert Celsius to Fahrenheit in Python by User Input



c = float(input("Enter temperature in celsius: "))

f = (c * 1.8) + 32

print(str(f))


Convert Celsius to Fahrenheit in Python by User Input



f = float(input("Enter temperature in Fahrenheit: "))

c = (f * 1.8) + 32

print(str(c))


Python program to calculate GST Goods and Service Tax  


item=input("Enter item name :")
sp_cost=float(input("How much is selling price of item"))
gst_rate=float(input("What is GST rate %"))
cgst=sp_cost*((gst_rate/2)/100)
sgst=cgst
amt=sp_cost+cgst*sgst
print("CGST (@",(gst_rate/2),"%) :",(cgst))
print("SGST(@",(gst_rate/2),"%) :",(sgst))
print("Amount payable: ",(amt))


Python Program Swapping two Number without using third variable (arithmetic operations)



x = int(input("Enter any Number x: "))
y = int(input("Enter any Number y: "))

print("Before swapping:")

print("x =" ,x)
print("y=" ,y)


x = x + y
y = x - y
x = x - y

print("After swapping:")

print("x =" ,x)
print("y=" ,y)


Python Program Swapping Three Number Variable


x = int(input("Enter any Number x: "))
y = int(input("Enter any Number y: "))

print("Before swapping:")

print("x =" ,x)
print("y=" ,y)

z=x
x=y
y=z

print("After swapping:")

print("x =" ,x)
print("y=" ,y)



Python Program Swapping Three Number Variable



a = int(input("Enter any Number a: "))
b = int(input("Enter any Number b: "))

# Before swap
print(f"Before swap: a = {a}, b = {b}")

# Swapping
a, b = b, a

# After swap
print(f"After swap: a = {a}, b = {b}")




Python Basic Programs Reviewed by ADcomputercampus on November 23, 2025 Rating: 5

No comments:

Contact Form

Name

Email *

Message *

Powered by Blogger.