1.
Python program to do arithmetical operations
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
sum = float(num1) + float(num2)
min = float(num1) - float(num2)
mul = float(num1) * float(num2)
div = float(num1) / float(num2)
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
print('The subtraction of {0} and {1} is {2}'.format(num1, num2, min))
print('The multiplication of {0} and {1} is {2}'.format(num1, num2, mul))
print('The division of {0} and {1} is {2}'.format(num1, num2, div))
Output:
Enter first number: 10
Enter second number: 20
The sum of 10 and 20 is 30.0
The subtraction of 10 and 20 is -10.0
The multiplication of 10 and 20 is 200.0
The division of 10 and 20 is 0.5
2. Python program to find the area of a triangle
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
Output:
Enter first side: 5
Enter second side: 6
Enter third side: 7
The area of the triangle is 14.70
3. Python program to convert Celsius to Fahrenheit
celsius = float(input('Enter temperature in Celsius: '))
fahrenheit = (celsius * 1.8) + 32
print('%0.1f Celsius is equal to %0.1f Fahrenheit'%(celsius,fahrenheit))
Output:
37.0 Celsius is equal to 98.6 Fahrenheit
4. Python program to find simple interest
p = int(input("Enter the principal amount :"))
t = int(input("Enter the time period :"))
r = int(input("Enter the rate of interest :"))
si = (p * t * r)/100
print('The Simple Interest is', si)
Output:
Enter the principal amount :100
Enter the time period :5
Enter the rate of interest: 12
The Simple Interest is 60.0
5. Python program to find the largest of three numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
Output
Enter first number: 11
Enter second number: 44
Enter third number: 22
The largest number is 44.0
6. Python program to find the smallest of three numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 <= num2) and (num1 <= num3):
smallest = num1
elif (num2 <= num1) and (num2 <= num3):
smallest = num2
else:
smallest = num3
print("The smallest number is", smallest)
Output
Enter first number: 11
Enter second number: 44
Enter third number: 22
The smallest number is 11.0
7. Python Program to Display the Multiplication Table
number = int(input ("Enter the number to print the multiplication table: "))
print ("The Multiplication Table of: ", number)
for count in range(1, 11):
print (number, 'x', count, '=', number * count)
Output
Enter the number to print the multiplication table: 13
The Multiplication Table of: 13
13 x 1 = 13
13 x 2 = 26
13 x 3 = 39
13 x 4 = 52
13 x 5 = 65
13 x 6 = 78
13 x 7 = 91
13 x 8 = 104
13 x 9 = 117
13 x 10 = 130
8. Python program to print Even Numbers in a List
list1 = [10, 21, 34, 45, 59, 66, 88, 93]
print(list1)
# iterating each number in list
print("Even numbers are :")
for num in list1:
if num % 2 == 0:
print(num, end=" ")
Output
[10, 21, 34, 45, 59, 66, 88, 93]
Even numbers are:
10 34 66 88
9. Python program to print odd Numbers in a Tuple
t1 = {1,2,3,4,5,6,7,8,9,10,11,12}
print(t1)
# iterating each number in tuple
print("odd numbers are :")
for num in t1:
if num % 2 != 0:
print(num, end=" ")
Output
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
odd numbers are :
1 3 5 7 9 11
10. Python program to count Even and Odd numbers in a List
# list of numbers
list1 = [10, 21, 4, 45, 66, 93, 1]
even_count, odd_count = 0, 0
# iterating each number in list
for num in list1:
# checking condition
if num % 2 == 0:
even_count += 1
else:
odd_count += 1
print(list1)
print("Even numbers in the list: ", even_count)
print("Odd numbers in the list: ", odd_count)
Output
[10, 21, 4, 45, 66, 93, 1]
Even numbers in the list: 3
Odd numbers in the list: 4
11. Python program to find simple interest:
# using function concept: input data taken from user keyboard
def simple_interest(p,t,r):
print('\n')
print('The principal is', p)
print('The time period is', t)
print('The rate of interest is',r)
si = (p * t * r)/100
print('The Simple Interest is', si)
P = int(input("Enter the principal amount :"))
T = int(input("Enter the time period :"))
R = int(input("Enter the rate of interest :"))
simple_interest(P,T,R)
Output
Enter the principal amount :100
Enter the time period :12
Enter the rate of interest :5
The principal is 100
The time period is 12
The rate of interest is 5
The Simple Interest is 60.0
12. Python program to find simple and compound interest
# Using function concept : input data taking from user.
def simple_interest(p,t,r):
si = (p * t * r)/100
print("The Simple Interest is",si)
def compound_interest(p, t, r):
amount = p * (pow((1 + r / 100), t))
CI = amount - p
print("Compound interest is %5.2f"%CI)
# Driver Code
p = int(input("Enter the principal amount: "))
t = int(input("Enter rate of interest: "))
r = int(input("Enter time in years: " ))
#Function Call
simple_interest(p,t,r)
compound_interest(p,t,r)
Output
Enter the principal amount: 200
Enter rate of interest: 12
Enter time in years: 5
The Simple Interest is 120.0
Compound interest is 159.17
13. Python Program to check if a Number is Positive, Negative or Zero.
def NumberCheck(a):
if a > 0:
print("Number given by you is Positive")
elif a < 0:
print("Number given by you is Negative")
else:
print("Number given by you is zero")
a = float(input("Enter a number as input value: "))
NumberCheck(a)
Output:
Enter a number as input value: -6
Number given by you is Negative
14.Python program to find factorial of given number using function
def factorial(n):
return 1 if (n==1 or n==0) else n * factorial(n - 1)
# Driver Code
num = int(input("Enter the integer number "))
print("Factorial of",num,"is",factorial(num))
Output
Enter the integer number 5
Factorial of 5 is 120
15. Python program to display the calendar
# import module
import calendar
yy = 2023
mm = 10
# display the calendar
print(calendar.month(yy, mm))
Output
October 2023
Mo Tu We Th Fr Sa Su
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
16. Python program to convert decimal into other number systems
dec=int(input("enter any decimal number "))
print("\nThe decimal converted value is:")
print("in binary : ", bin(dec))
print("in octal : ", oct(dec))
print("in hexadecimal : ",hex(dec))
Output
enter any decimal number 65
The decimal converted value is:
in binary : 0b1000001
in octal : 0o101
in hexadecimal : 0x41
17. Python program to display all the prime numbers within an interval
lower = 1
upper = 40
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
Output
Prime numbers between 1 and 40 are:
1
2
3
5
7
11
13
17
19
23
29
31
37
18. Python Program to Make a Simple Calculator
def add(P, Q):
return P + Q
def subtract(P, Q):
return P - Q
def multiply(P, Q):
return P * Q
def divide(P, Q):
return P / Q
print ("Please select the operation.")
print ("a. Add")
print ("b. Subtract")
print ("c. Multiply")
print ("d. Divide")
choice = input("Please enter choice (a/ b/ c/ d): ")
num_1 = int (input ("Please enter the first number: "))
num_2 = int (input ("Please enter the second number: "))
if choice == 'a':
print (num_1, " + ", num_2, " = ", add(num_1, num_2))
elif choice == 'b':
print (num_1, " - ", num_2, " = ", subtract(num_1, num_2))
elif choice == 'c':
print (num1, " * ", num2, " = ", multiply(num1, num2))
elif choice == 'd':
print (num_1, " / ", num_2, " = ", divide(num_1, num_2))
else:
print ("This is an invalid input")
output
Please select the operation.
a. Add
b. Subtract
c. Multiply
d. Divide
Please enter choice (a/ b/ c/ d): a
Please enter the first number: 10
Please enter the second number: 20
10 + 20 = 30
19. Python Program to Add Two Matrices
X = [[10,20,30],
[40,50,60],
[65,70,80]]
Y = [[10,11,12],
[13,14,15],
[16,17,18]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X)):
result[i][j] = X[i][j] + Y[i][j]
print("Addition of two matrix")
for r in result:
print(r)
Output
Addition of two matrix
[20, 31, 42]
[53, 64, 75]
[81, 87, 98]
20. Python Program to subtract Two Matrices
X = [[10,20,30],[10,20,30], [10,20,30]]
Y = [[11,11,12],[13,14,15], [16,17,18]]
result = [[0,0,0],[0,0,0],[0,0,0]]
for i in range(len(X)):
for j in range(len(X)):
result[i][j] = X[i][j] - Y[i][j]
print("Substraction of two matrix")
for r in result:
print(r)
output
Substraction of two matrix
[-1, 9, 18]
[-3, 6, 15]
[-6, 3, 12]
21. Python Program to compute sum of digits in number.
# Function to get sum of digits
def getSum(n):
sum = 0
while (n != 0):
sum = sum + (n % 10)
n = n//10
return sum
n = int(input("Enter any integer number "))
print(getSum(n))
Output
Enter any integer number 1234
10
22. Python Program to reverse a number
# Function to reverse a number
def getRev(n):
rev = 0
while(n > 0):
d = n % 10
rev = rev * 10 + d
n = n // 10
return( rev)
n = int(input("Enter any integer number "))
print(getRev(n))
Output
Enter any integer number 2345
5432