AI HHW
AI HHW
Python Programs
S No Program Remarks
1 Program to add two numbers
2 Program to print the area of a rectangle
3 Program to calculate the simple interest.
Take input from the user for principal, rate of
interest and time
4 Program to swap two numbers by multiple
assignments.
5 Program to display calendar import calendar
6 Program to check if a number is odd or even
Total=a+b+c
Avg=Total/3
print("Total= ", Total)
print("Average= ", Avg)
OUTPUT:
Enter first number= 30
Enter second number= 20
Enter third number= 40
Total = 90
Average=30
Program 2
#Program to find largest of the three numbers
Output:
Enter first number= 12
Enter second number= 34
Enter third number= 1
34 is the largest number
Program 3
# Program to swap two numbers
# Method 1: Using multiple assignment
a=int(input("Enter a number= "))
b=int(input("Enter second number= "))
print("Numbers before swapping")
print("a= ",a)
print("b= ",b)
print("Numbers after swapping")
a,b=b,a # Using multiple assignment
print("a= ",a)
print("b= ",b)
Output:
# Method 1: Using multiple assignment
Enter a number= 9
Enter second number= 7
Numbers before swapping
a= 9
b= 7
Numbers after swapping
a= 7
b= 9
# Method 2: by taking a third variable
a=int(input("Enter a number= "))
b=int(input("Enter second number= "))
print("Numbers before swapping")
print("a= ",a)
print("b= ",b)
print("Numbers after swapping")
c=a
a=b # Using a third variable
b=c
print("a= ",a)
print("b= ",b)
OUTPUT:
Enter a number= 5
Enter second number= 6
Numbers before swapping
a= 5
b= 6
Numbers after swapping
a= 6
b= 5
Program 4
# Program to solve the Quadratic Equation
−𝑏±√𝑏 2 −4𝑎𝑐
#𝑥= 2𝑎
import math
a=int(input("Enter a = "))
b=int(input("Enter b= "))
c=int(input("Enter c= "))
x=(-b+math.sqrt(b*b-4*a*c))/2*a
print("Ans= " , x)
OUTPUT:
Enter a = 2
Enter b= 10
Enter c= 12
Ans = -8.0
Program 5
# Program to find the area and perimeter of the circle
pi=3.14
r=eval(input("Enter the radius= "))
area= pi * r *r
perimeter= 2*pi*r
print("Area of the circle= ", area)
print("Perimeter of the circle= ", perimeter)
OUTPUT:
Enter the radius= 5
Area of the circle= 78.5
Perimeter of the circle= 31.400000000000002
Program 6
# Program to check if the number inputted by the user is even
or odd
num=int(input("Enter a number= "))
if num%2==0:
print("It is an even number")
else:
print("It is an odd number")
OUTPUT:
Enter a number= 10
It is an even number
Enter a number= 5
It is an odd number
Program 7
Enter a number= 0
You have entered zero
Enter a number=
-9
You have entered a negative number
Enter a number=
9
You have entered a positive number
Program 8
OUTPUT:
if E_week>=30000:
print("Bravo!Your income = " ,E_week)
elif E_week>20000 and E_week<30000:
print("Kudos!You income = " ,E_week)
elif E_week>10000 and E_week<20000:
print("Good Going! You income = " ,E_week)
else:
print("Your income is less than 10000")
OUTPUT:
Enter the no of working hours= 8
Enter the earning per hour= 5000
Bravo!Your income = 40000
# Program to print odd & even numbers using for loop from 1-
20
print("Even numbers are ")
for i in range(1,11):
if i%2==0:
print(i)
2
4
6
8
10
1
3
5
7
9
Program 11
Program to show the square of N natural numbers
OUTPUT:
Enter the value for n= 10
Square of 1 = 1
Square of 2 = 4
Square of 3 = 9
Square of 4 = 16
Square of 5 = 25
Square of 6 = 36
Square of 7 = 49
Square of 8 = 64
Square of 9 = 81
Square of 10 = 100
Program 12
# Sum of n natural numbers (using while loop)
# s=1+2+3+........+n
n=int(input("Enter the value for n= "))
s=0
i=1
while i<=n:
s=s+i
i=i+1
print(s)
OUTPUT:
Enter the value for n= 10
1
3
6
10
15
21
28
36
45
55
Program 13
# Program to create a list of numbers and show the sum of all the
items in the list
list=[10,20,30,50,60]
list1=[11, 23, 56,90, 40]
print("Sum of all the items in the list= ", sum(list))
print("Sum of all the items in the list1= ", sum(list1))
OUTPUT:
Sum of all the items in the list= 170
Sum of all the items in the list1= 220
Program 14
# Program to create a list and add the elements using append
method
list1=[34,99]
print(list1)
list1.append(["Python","Hello World"])
print (list1)
OUTPUT:
[34, 99]
OUTPUT:
OUTPUT:
{1: 'One', 2: 'Two', 3: 'Three', 5: 'Five', 7: 'Seven', 9: 'Nine'}
dict_keys([1, 2, 3, 5, 7, 9])
dict_values(['One', 'Two', 'Three', 'Five', 'Seven', 'Nine'])
Program 17
# Program to create an array from the given list (Using numpy
package)
import numpy
arr=numpy.array([1,2,3,4,5])
print(arr)
OUTPUT:
[1 2 3 4 5]
Second Element= 2
Program 18
# Applying different arithmetic operations
#Adding 5 to all the elements
import numpy
arr=numpy.array([1,2,3,4,5])
print(arr)
arr1=arr+5
print(arr1)
#Dividing by 5
arr2=arr1/5
print(arr2)
OUTPUT:
[ 6 7 8 9 10]
[1.2 1.4 1.6 1.8 2. ]
Program 19
# Program to create an array from the given list (Using numpy
package)
import numpy
arr=numpy.array([1,2,3,4,5])
print(arr)
OUTPUT:
[ 1 4 9 16 25]
Program 20
# Creating a 2-dimensional zero array (4x3)
#4 rows, 3 columns
import numpy
arr=numpy.zeros((4,3))
print(arr)
OUTPUT:
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
Program 21
OUTPUT:
import numpy
arr=numpy.full((3,4),7)
print(arr)
OUTPUT:
[[7 7 7 7]
[7 7 7 7]
[7 7 7 7]]
Program 23
import numpy
arr=numpy.arange((0,30),5)
print(arr)
OUTPUT:
[ 0 5 10 15 20 25]
Program 24
import numpy
arr=numpy.array([10,20,30,40,50])
print("Type of the array= ", type(arr))
print("Dimesions of the array= ", arr.ndim)
print("Shape of the array= ", arr.shape)
print("Size of the array= ", arr.size)
print("Datatype of the array= ", arr.dtype)
OUTPUT:
import numpy
arr=numpy.array([10,34,67,8,9])
print(arr)
print("Maximum element of the array= ", arr.max())
print("Minimum element of the array= ", arr.min())
print("Sum of the elements of the array= ", arr.sum())
OUTPUT:
[10 34 67 8 9]
Maximum element of the array= 67
Minimum element of the array= 8
Sum of the elements of the array= 128