Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

AI Practical File

Subject Code 417


Pranay Chopra
X-D
Board Roll Number:
Q1 Program to obtain three numbers and print their sum

In [1]: num1=int(input("Enter first number: "))


num2=int(input("Enter second number: "))
num3=int(input("Enter third number: "))
summ=num1+num2+num3
print("Sum of three numbers is: ",summ)

Enter first number: 5


Enter second number: 10
Enter third number: 15
Sum of three numbers is: 30

Q2 Write a program to input a number and print its cube

In [2]: num=int(input("Enter a number: "))


cube=num*num*num
print("Cube of number is: ",cube)

Enter a number: 5
Cube of number is: 125

Q3 Write a program to input two integers and perform all arithmetc operations on them

In [3]: num1=int(input("Enter first number: "))


num2=int(input("Enter second number: "))
summ=num1+num2
diff=num1-num2
mul=num1*num2
div=num1/num2
mod=num1%num2
print("Sum is: ",summ)
print("Difference is: ",diff)
print("Multiplication is: ",mul)
print("Division is: ",div)
print("Modulus is: ",mod)

Enter first number: 5


Enter second number: 10
Sum is: 15
Difference is: -5
Multiplication is: 50
Division is: 0.5
Modulus is: 5

Q4 Write a program to find average of three numbers

In [4]: num1=int(input("Enter first number: "))


num2=int(input("Enter second number: "))
num3=int(input("Enter third number: "))
summ=num1+num2+num3
avg=summ/3
print("Average is: ",avg)

Enter first number: 5


Enter second number: 10
Enter third number: 15
Average is: 10.0
Q5 Write a program to input age from a user and determine if the user is eligible to vote
or not

In [8]: age=int(input("Enter your age: "))


if age>=18:
print("You are eligible to vote")
else:
print("You are not eligible to vote")

Enter your age: 15


You are not eligible to vote

============

Enter your age: 35


You are eligible to vote

Q6 Write a program to input two numbers and see if they are equal

In [9]: num1=int(input("Enter first number: "))


num2=int(input("Enter second number: "))
if num1==num2:
print("Numbers are equal")
else:
print("Numbers are not equal")

Enter first number: 5


Enter second number: 10
Numbers are not equal

============

Enter first number: 15


Enter second number: 15
Numbers are equal

Q7 Program to input a number and check if it is even or odd

In [10]: num=int(input("Enter a number: "))


if num%2==0:
print("Number is even")
else:
print("Number is odd")

Enter a number: 5
Number is odd

============

Enter a number: 10
Number is even

Q8 Program to accept three numbers and find the largest


In [11]: num1=int(input("Enter first number: "))
num2=int(input("Enter second number: "))
num3=int(input("Enter third number: "))
if num1>num2 and num1>num3:
print("First number is greatest")
elif num2>num1 and num2>num3:
print("Second number is greatest")
else:
print("Third number is greatest")

Enter first number: 5


Enter second number: 10
Enter third number: 15
Third number is greatest

============

Enter first number: 5


Enter second number: 15
Enter third number: 10
Second number is greatest

============

Enter first number: 15


Enter second number: 10
Enter third number: 5
First number is greatest

Q9 Write a program to print all items from a list containing some color names in it

In [12]: colorlist=["Red","Green","White","Black"]
for i in colorlist:
print(i)

Red
Green
White
Black

Q10 Write a program to print total marks from a list of marks

In [13]: markslist=[77,78,75,76,74]
print("List of marks:",markslist)
tmarks=0
for i in markslist:
tmarks=tmarks+i
print("Total marks is: ",tmarks)

List of marks: [77, 78, 75, 76, 74]


Total marks is: 380

Q11 Program to accept a number and print its multiplication table

In [14]: num=int(input("Enter a number: "))


for i in range(1,11):
print(num,"X",i,"=",num*i)
Enter a number: 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

Q12 Program to print sum of natural numbers from 1 to 7

In [15]: sum=0
for i in range(1,8):
sum=sum+i
print("Sum of first 7 natural numbers is: ",sum)

Sum of first 7 natural numbers is: 28

Q13 Write a program to print squares of first 5 natural numbers

In [17]: i=1
while i<=5:
print("Square of",i,"is",i*i)
i+=1

Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25

Q14 Write a program to input a number and print the sum of numbers from 1 to num

In [18]: num=int(input("Enter a number: "))


sum=0
for i in range(1,num+1):
sum=sum+i
print("Sum of numbers from 1 to",num," is: ",sum)

Enter a number: 5
Sum of numbers from 1 to 5 is: 15

Q15 Program to calculate and print the sum of even and odd integers of the first n natural
numbers

In [19]: n=int(input("Enter a number: "))


sumeven=0
sumodd=0
for i in range(1,n+1):
if i%2==0:
sumeven=sumeven+i
else:
sumodd=sumodd+i
print("Sum of even numbers from 1 to",n," is: ",sumeven)
print("Sum of odd numbers from 1 to",n," is: ",sumodd)
Enter a number: 15
Sum of even numbers from 1 to 15 is: 56
Sum of odd numbers from 1 to 15 is: 64

Q16 Write a program to calculate the factorial of the number

In [20]: num=int(input("Enter a number: "))


fact=1
for i in range(1,num+1):
fact=fact*i
print("Factorial of",num,"is: ",fact)

Enter a number: 5
Factorial of 5 is: 120

Q17 Write a program to implement a simple calculator for two numbers. Offer a choice
through a menu
In [24]: def add(num1,num2):
return num1+num2
def sub(num1,num2):
return num1-num2
def mul(num1,num2):
return num1*num2
def div(num1,num2):
return num1/num2

while True:

print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Exit")

choice=int(input("Enter your choice: "))

if choice==5:
break

if choice not in range(1,6):


print("Invalid choice")
print("\n============\n")
continue

num1=int(input("Enter first number: "))


num2=int(input("Enter second number: "))

if choice==1:
print("Sum is: ",add(num1,num2))
print("\n============\n")
elif choice==2:
print("Difference is: ",sub(num1,num2))
print("\n============\n")
elif choice==3:
print("Product is: ",mul(num1,num2))
print("\n============\n")
elif choice==4:
print("Quotient is: ",div(num1,num2))
print("\n============\n")
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 0
Invalid choice

============

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 1
Enter first number: 5
Enter second number: 10
Sum is: 15

============

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 2
Enter first number: 10
Enter second number: 5
Difference is: 5

============

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 3
Enter first number: 5
Enter second number: 10
Product is: 50

============

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 4
Enter first number: 5
Enter second number: 10
Quotient is: 0.5

============

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 5

Q18 Write a program to allocate the grades of a student when grades are allocated in the
following manner:

Percentage Grade

Above 90% A

80% to 90% B

70% to 80% C

60% to 70% D

Below 60% E

The percentage of the student is to be taken via input

In [26]: percentage=float(input("Enter your percentage (without the % sign): "))


if percentage>=90:
print("Grade is A")
elif percentage>=80 and percentage<90:
print("Grade is B")
elif percentage>=70 and percentage<80:
print("Grade is C")
elif percentage>=60 and percentage<70:
print("Grade is D")
elif percentage<60:
print("Grade is E")

Enter your percentage (without the % sign): 99


Grade is A

============

Enter your percentage (without the % sign): 88


Grade is B

============

Enter your percentage (without the % sign): 77


Grade is C

============

Enter your percentage (without the % sign): 66


Grade is D

============

Enter your percentage (without the % sign): 55


Grade is E

Q19 Program to find the minimum element from a list along with its index in the list
In [27]: list=eval(input("Enter a list: "))
min=list[0]
index=0
for i in range(1,len(list)):
if list[i]<min:
min=list[i]
index=i
print("Minimum element is: ",min)
print("Index of minimum element is: ",index)

Enter a list: 5,10,25,101,63


Minimum element is: 5
Index of minimum element is: 0

Q20 Program to search for an element in a given list of numbers

In [29]: list=eval(input("Enter a list: "))


num=int(input("Enter a number: "))
flag=0
for i in range(0,len(list)):
if list[i]==num:
flag=1
print("Number is found at index",i)
break
if flag==0:
print("Number is not found")

Enter a list: 5,101,63,45,99


Enter a number: 63
Number is found at index 2

============

Enter a list: 5,101,63,45,99


Enter a number: 27
Number is not found

Q21 Program to count the frequency of a given element in a list of numbers

In [30]: list=eval(input("Enter a list: "))


num=int(input("Enter a number: "))
print("Number is: ",num)
count=0
for i in range(0,len(list)):
if list[i]==num:
count=count+1
print("Frequency of",num,"is: ",count)

Enter a list: 5,101,63,5,0,1,1,2,5,3,1,5,4,3


Enter a number: 5
Number is: 5
Frequency of 5 is: 4

============

Enter a list: 5,101,63,5,0,1,1,2,5,3,1,5,4,3


Enter a number: 10
Number is: 10
Frequency of 10 is: 0
Q22 Program to calculate the mean of a given list of numbers

In [31]: inp=eval(input("Enter a list: "))


list=list(inp)
sum=0
for i in range(0,len(list)):
sum=sum+list[i]
mean=sum/len(list)
print("Mean is: ",mean)

Enter a list: 5,101,63,5,0,1,1,2,5,3,1,5,4,3


Mean is: 14.214285714285714

Q23 Menu Driven program to input a list and print sorted list, reverse of list , delete an
element using value and index.

In [1]: inp=eval(input("Enter a list: "))


list=list(inp)
while True:
print("1. Sort list")
print("2. Reverse list")
print("3. Delete an element using value")
print("4. Delete an element using index")
print("5. Exit")
choice=int(input("Enter your choice: "))
if choice==5:
break
elif choice==1:
lisst=list.copy()
lisst.sort()
print("Sorted list is: ",lisst)
print("\n============\n")
elif choice==2:
lisst=list.copy()
lisst.reverse()
print("Reversed list is: ",lisst)
print("\n============\n")
elif choice==3:
num=int(input("Enter a number: "))
lisst=list.copy()
lisst.remove(num)
print("List after deleting",num,"is: ",lisst)
print("\n============\n")
elif choice==4:
index=int(input("Enter an index: "))
lisst=list.copy()
lisst.pop(index)
print("List after deleting element at index",index,"is: ",lisst)
print("\n============\n")
else:
print("Invalid choice")
print("\n============\n")
Enter a list: 5,101,63,5,0,1,1,2,5,3,1,5,4,3
1. Sort list
2. Reverse list
3. Delete an element using value
4. Delete an element using index
5. Exit
Enter your choice: 0
Invalid choice

============

1. Sort list
2. Reverse list
3. Delete an element using value
4. Delete an element using index
5. Exit
Enter your choice: 1
Sorted list is: [0, 1, 1, 1, 2, 3, 3, 4, 5, 5, 5, 5, 63, 101]

============

1. Sort list
2. Reverse list
3. Delete an element using value
4. Delete an element using index
5. Exit
Enter your choice: 2
Reversed list is: [3, 4, 5, 1, 3, 5, 2, 1, 1, 0, 5, 63, 101, 5]

============

1. Sort list
2. Reverse list
3. Delete an element using value
4. Delete an element using index
5. Exit
Enter your choice: 3
Enter a number: 0
List after deleting 0 is: [5, 101, 63, 5, 1, 1, 2, 5, 3, 1, 5, 4, 3]

============

1. Sort list
2. Reverse list
3. Delete an element using value
4. Delete an element using index
5. Exit
Enter your choice: 4
Enter an index: 5
List after deleting element at index 5 is: [5, 101, 63, 5, 0, 1, 2, 5,
3, 1, 5, 4, 3]

============

1. Sort list
2. Reverse list
3. Delete an element using value
4. Delete an element using index
5. Exit
Enter your choice: 5
Q24 Program to find mean, median, standard deviation and variance of array with values
[10,11,12,13,14,15]

In [2]: import numpy as np


arr=np.array([10,11,12,13,14,15])
print("Array is: ",arr)
mean=np.mean(arr)
print("Mean is: ",mean)
median=np.median(arr)
print("Median is: ",median)
std=np.std(arr)
print("Standard deviation is: ",std)
var=np.var(arr)
print("Variance is: ",var)

Array is: [10 11 12 13 14 15]


Mean is: 12.5
Median is: 12.5
Standard deviation is: 1.707825127659933
Variance is: 2.9166666666666665

Refer File – emp.csv ( For program number 25 to 28 )

EmpNo Name Designation Salary

1001 Geeta Manager 65000

1002 Mohit Officer 30000

1003 Rita Clerk 16000

1004 Rohit CEO 75000

1005 Tarun Manager 50000

Q25 Program to read and display emp.csv file

In [3]: import pandas as pd


df=pd.read_csv("emp.csv")
print(df)

EmpNo Name Designation Salary


0 1001 Geeta Manager 65000
1 1002 Mohit Officer 30000
2 1003 Rita Clerk 16000
3 1004 Rohit CEO 75000
4 1005 Tarun Manager 50000

Q26 Program to make a Line chart of Name and Salary

In [4]: import pandas as pd


import matplotlib.pyplot as plt
df=pd.read_csv("emp.csv")
plt.plot(df["Name"],df["Salary"])
plt.xlabel("Name")
plt.ylabel("Salary")
plt.title("Line chart of Name and Salary of employees")
plt.show()
Q27 Program to make a Bar chart of Name and Salary

In [5]: import pandas as pd


import matplotlib.pyplot as plt
df=pd.read_csv("emp.csv")
plt.bar(df["Name"],df["Salary"])
plt.xlabel("Name")
plt.ylabel("Salary")
plt.title("Bar chart of Name and Salary of employees")
plt.show()
Q28 Program to make a Pie Chart

In [6]: import pandas as pd


import matplotlib.pyplot as plt
df=pd.read_csv("emp.csv")
plt.pie(df["Salary"],labels=df["Name"])
plt.title("Pie chart of Name and Salary of employees")
plt.show()

You might also like