In this article Simple Python Programs we give the some simple programs like addition of two numbers, to find area of rectangle, etc. also some conditional and looping programs with output.
Simple Python Programs:
#Simple welcome program
print(“Wel-Come to Python Programming”)
# O/P: Wel-Come to Python Programming
#addition of two numbers using compile time initialization
a=10
b=20
print(“A + B = “,a+b)
# O/P: A + B = 30
#addition of two numbers using run time initialization
a=int(input(“Enter the first number: “))
b=int(input(“Enter the second number: “))
print(“A + B = “,a+b)
# O/P:
Enter the first number: 20
Enter the second number: 30
A + B = 50
# To find area of Rectangle.
l=int(input(“Enter the length of rectangle: “))
b=int(input(“Enter the bradth of rectangle: “))
print(“Area of rectangle: “,l*b)
# O/P:
Enter the length of rectangle: 10
Enter the bradth of rectangle: 7
Area of rectangle: 70
# To calculate area of Circle
r=float(input(“Enter the radius of circle: “))
area=3.142*r*r
print(“Area of Circle: “,area)
# O/P:
Enter the radius of circle: 1.0
Area of Circle: 3.142
#swapping of two numbers using third variable
a=int(input(“Enter the first number: “))
b=int(input(“Enter the second number: “))
print(“Befor Swap”)
print(a,b)
t=a;
a=b;
b=t;
print(“After swap”)
print(a,b)
# O/P:
Enter the first number: 10
Enter the second number: 20
Befor Swap
10 20
After swap
20 10
#swapping of two numbers without third variable
a=int(input(“Enter the first number: “))
b=int(input(“Enter the second number: “))
print(“Befor Swap”)
print(a,b)
a=a+b;
b=a-b;
a=a-b;
print(“After swap”)
print(a,b)
# O/P:
Enter the first number: 3
Enter the second number: 4
Befor Swap
3 4
After swap
4 3
# To find simple interest
p = int(input(“Enter the principal amount: “))
r = int(input(“Enter the rate of interest: “))
t = int(input(“Enter the time (in years): “))
si = (p * r * t) / 100
print(“Simple Interest:”, si)
# O/P:
Enter the principal amount: 50000
Enter the rate of interest: 10
Enter the time (in years): 5
Simple Interest: 25000.0
# Simple if statement program
age=int(input(“Enter the age: “))
if(age>=18):
print(“Person is eligibal for voting.”)
print(“Have a nice Day!”)
#O/P:
Enter the age: 20
Person is eligible for voting.
Have a nice Day!
#To find odd/even number
a=int(input(“Enter the first number: “))
if a%2==0:
print(a,” is Even number.”)
else:
print(a,” is Odd number.”)
# O/P:
Enter the first number: 10
10 is Even number.
# To find max number among 2 numbers.
a=int(input(“Enter the first number: “))
b=int(input(“Enter the second number: “))
if a>b:
print(a, ” : First number is largest number.”)
elif b>a:
print(b, ” : Second number is largest number.”)
else:
print(a, “Both are equal.”)
# O/P:
Enter the first number: 10
Enter the second number: 10
10 Both are equal.
# Display first 10 natural numbers using while loop.
i=1
while(i<=10):
print(i)
i+=1
# O/P:
1 2 3 4 5 6 7 8 9 10
# Display first 10 natural numbers using for loop.
for i in range(1,10):
i+=1
print(i)
# O/P:
1 2 3 4 5 6 7 8 9 10
# To find sum of first 10 natural numbers
i=1
sum=0
while(i<=10):
sum+=i
i+=1
print(“Sum of first 10 natural number is “,sum)
#O/P:
Sum of first 10 natural number is 55
# Program to find sum of digits of a number
n = int(input(“Enter the number: “))
sum = 0
while n > 0:
dig = n % 10 # get last digit
sum += dig # add digit to sum
n = n // 10 # remove last digit
print(“Sum of digits:”, sum)
# O/P:
Enter the number: 123
Sum of digits: 6
# Program to reverse a number
n = int(input(“Enter the number: “))
rev = 0
while n > 0:
dig = n % 10 # get last digit
rev = rev * 10 + dig # build reversed number
n = n // 10 # remove last digit
print(“Reversed number:”, rev)
# O/P:
Enter the number: 123
Reversed number: 321
# Program to check palindrome number
n = int(input(“Enter the number: “))
temp = n # store original number
rev = 0
while n > 0:
dig = n % 10
rev = rev * 10 + dig
n = n // 10
if temp == rev:
print(rev, ” is a Palindrome Number”)
else:
print(temp, ” is Not a Palindrome Number”)
# O/P:
Enter the number: 121
121 ia a Palindrome number.
# Addition of two numbers using function
def add(x, y):
return(x + y)
a = 5
b = 3
result = add(a, b)
print(“Sum:”, result)
#O/P:
Sum: 8
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System