Class 10 - Assignment Solution - Python Programs - 2-Dec-24
Class 10 - Assignment Solution - Python Programs - 2-Dec-24
ARTIFICIAL INTELLIGENCE
PRACTICAL FILE
CLASS – X
Q10
Write a program that asks the user to input number of seconds
and then express it in terms of how many hours, minutes and
seconds it contains.
Q11 Write a program to input three numbers and swap them. 1st
becomes 2nd, 2nd becomes 3rd , 3rd becomes 1st.
Q12
Write a program to calculate the BMI (Body Mass Index) of a
person. BMI is a calculation using a person’s height and weight.
The formula is BMI=kg/m2.
Q14 Write a program that takes a number and check whether the
given number is odd or even.
Q15 Write a program to accept three integers and print the largest
of the three. Make use of only if statement.
Q16 Write a program to input a number (num) and print the sum of
numbers from 1 to num.
Programs on List
Q19 Write a program to create a list of 5 elements and print the
second item in the list.
Q20 Write a program to change the value of 2nd element in the list.
Q21 Write a program to add an element using the append method in
the list.
Q22 Write a program to add an element in the 3rd position Using the
insert method in the list.'
Q24 Write a program to use negative indexing to print the last item
in the list.
Q26 Write a program to use the correct syntax to print the number
of items in the list.(Use len function).
Q27 Return the items from the beginning to, excluding element from
the fourth element.
Q28 Write a program to return the items from 2nd to the end.
Q29 Write a program to create another list. Add these two lists.
Programs on Numpy and Matplotlib Library
Q30 Write a program to calculate the mean using numpy library.
Q31 Write a program to calculate the median using numpy library.
Q32 Write a program to calculate the Standard Deviation using
numpy library.
Q35 Write a program to plot a Bar graph for the given data using
MatPlotLib library.
Q36 Write a program to plot a Scatter Chart for the given data using
MatPlotLib library.
Q37 Write a program to plot a Bar Graph for the given data using
MatPlotLib library.
Q38 Write a program to plot a Pie Chart for the given data using
MatPlotLib library.
'''1. Write a program to input a welcome message and print it.
'''
msg=input("Enter a message:")
print("The message is:",msg)
'''OUTPUT
Enter a message:Daly College, Indore
The message is: Daly College, Indore
'''
'''2. Write a program to obtain length and breadth of a rectangle and calculate
its area.
'''
l=int(input("Enter the length:"))
b=int(input("Enter the breadth:"))
area=l*b
print("Area of the rectangle:",area)
'''
OUTOUT
Enter the length:5
Enter the breadth:7
Area of the rectangle: 35
'''
'''3. Write a program to read distance in miles and print in kilometres. (1 Mile =
1.609 Kms)
'''
miles=int(input("Enter the distance in miles:"))
kms=1.609*miles
print("The distance in kms is :",kms)
'''OUTPUT
Enter the distance in miles:2
The distance in kms is : 3.218
'''
'''OUTPUT
11.574074074074074 Days in 1 million seconds
'''
'''5. Write a program to input three numbers and print their sum.
'''
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
c=int(input("Enter third number:"))
sum=a+b+c
print("Sum of three numbers is:",sum)
'''OUTPUT
Enter first number:10
Enter second number:20
Enter third number:30
Sum of three numbers is: 60
'''
'''OUTPUT
Enter a number:5
Cube is : 125
'''
'''7. Write a program to input a value in kilometre and convert it into miles.
(1 KM = 0.621371 Miles)
'''
kms=int(input("Enter the distance in kms:"))
miles=0.621371*kms
print("The distance in miles is :",miles)
'''OUTPUT
Enter the distance in kms:5
The distance in miles is : 3.106855
'''
a,b=b,a
'''OUTPUT
Enter first number:10
Enter second number:20
The swapped value of a is : 20
The swapped value of b is : 10
'''
'''9. Write a program to input a value in tonnes and convert it into quintals and
kilograms. (1 Ton = 10 Quintals, 1 Ton = 1000 Kgs)
'''
tonnes=int(input("Enter a value in tonnes:"))
quintles=tonnes*10
kgs=tonnes*1000
print("Quintles are :",quintles)
print("Kilogrms are :",kgs)
'''OUTPUT
Enter a value in tonnes:5
Quintles are : 50
Kilogrms are : 5000
'''
'''10. Write a program that asks the user to input number of seconds and then
express it in terms of how many hours, minutes and seconds it contains.
'''
time=int(input("Enter time in seconds:"))
hrs=time//3600
time=time%3600
min=time//60
sec=time%60
'''OUTPUT
Enter time in seconds:4000
Number of hours are : 1
Number of minutes are : 6
Number of seconds are : 40
'''
'''11. Write a program to input three numbers and swap them. 1st becomes
2nd, 2nd becomes 3rd , 3rd becomes 1st.
'''
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
c=int(input("Enter second number:"))
a,b,c=b,c,a
print("Swapped values of a, b & c are:",a,b,c)
'''
OUTPUT
Enter first number:10
Enter second number:20
Enter second number:30
Swapped values of a, b & c are: 20 30 10
'''
'''12. Write a program to calculate the BMI (Body Mass Index) of a person. BMI
is a calculation using a person’s height and weight. The formula is BMI=kg/m2.
'''
weight=float(input("Enter your weight in KG:"))
height=float(input("Enter your height in M:"))
bmi=weight/(height*height)
print("The BMI is :", bmi)
'''
OUTPUT
Enter your weight in KG:80
Enter your height in m:1.8
The BMI is : 24.691358024691358 '''
'''13. Write a program to enter two integers and perform all arithmetic
operations on them.
'''
a=int(input("Enter first no.:"))
b=int(input("Enter second no.:"))
sum=a+b
sub=a-b
mult=a*b
div=a/b
floordiv=a//b
rem=a%b
if n%2==0:
print("Number is even")
else:
print("Number is odd")
'''
OUTPUT
Enter a number:21
Number is odd
'''
'''15. Write a program to accept three integers and print the largest of the
three. Make use of only if statement.
'''
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
c=int(input("Enter third number:"))
max=a
if b>max:
max=b
if c>max:
max=c
print("Largest nuber is :", max)
'''
OUTPUT
Enter first number:10
Enter second number:20
Enter third number:30
Largest nuber is : 30
'''
'''16. Write a program to input a number (num) and print the sum of numbers
from 1 to num.
'''
sum=0
n=int(input("Enter nth term:"))
for i in range(1,n+1):
sum=sum+i
print("Sum of n numbers is :",sum)
'''
OUTPUT
Enter nth term:10
Sum of n numbers is : 55
'''
'''17. Write a program to print squares of first 5 natural numbers.
'''
sum=0
for i in range(1,6):
sq=i*i
print("Square of",i,"is :", sq)
'''
OUTPUT
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
'''
'''18. Write a program to input a year and check if it is a leap year or not.
'''
year=int(input("Enter a 4 digit year(yyyy):"))
if((year%400 == 0) or (year%100!=0) and (year%4==0)):
print("Leap Year");
else:
print("Not a Leap Year")
'''
OUTPUT
Enter a 4 digit year(yyyy):2000
Leap Year
'''
'''19. Write a program to create a list of 5 elements and print the second item
in the list.
'''
'''
OUTPUT
Enter 5 elements in the list:[1,3,5,7,9]
List elements are: [1, 3, 5, 7, 9]
Second element is: 3
'''
'''20. Write a program to change the value of 2nd element in the list.
'''
'''
OUTPUT
Enter elements in the list:[2,5,1,7,9]
List elements are: [2, 5, 1, 7, 9]
Enter new element for 2nd position :75
Updated list elements are: [2, 75, 1, 7, 9]
'''
'''21. Write a program to add an element using the append method in the list.
'''
list1.append(newele)
'''
OUTPUT
Enter elements in the list:[4,7,3,9,1]
List elements are: [4, 7, 3, 9, 1]
Enter new element in the list:20
Updated list elements are: [4, 7, 3, 9, 1, 20]
'''
''22. Write a program to add an element in the 3rd position Using the insert
method in the list.
'''
list1.insert(2,newele)
'''
OUTPUT
Enter elements in the list:[2,7,12,34,45]
List elements are: [2, 7, 12, 34, 45]
Enter new element for 3rd position in the list:100
Updated list elements are: [2, 7, 100, 12, 34, 45]
'''
'''23. Write a program to remove an element using the remove method from
the list.
'''
list1.remove(remele)
'''
OUTPUT
Enter elements in the list:[1,12,23,34,45]
List elements are: [1, 12, 23, 34, 45]
Enter element to be removed from the list:23
Updated list elements are: [1, 12, 34, 45]
'''
'''24. Write a program to use negative indexing to print the last item in the list.
'''
'''
OUTPUT
Enter elements in the list:[25, 37, 48, 55, 67]
List elements are: [25, 37, 48, 55, 67]
Lst element of the list is : 67
'''
'''25. Write a program to use a range of indexes to print the third, fourth, and
fifth item in the list.
'''
print ("Third, Fourth and Fifth element of the list are :",list1[2:5])
'''
OUTPUT
Enter elements in the list:[7,5,8,4,9,2,1]
List elements are: [7, 5, 8, 4, 9, 2, 1]
Third, Fourth and Fifth element of the list are : [8, 4, 9]
'''
'''26. Write a program to use the correct syntax to print the number of items in
the list.(Use len function).
'''
'''
OUTPUT
Enter elements in the list:[23, 67, 98, 34, 56]
List elements are: [23, 67, 98, 34, 56]
Length of the list is : 5
'''
'''27. Return the items from the beginning to, excluding element from the
fourth element.
'''
'''
OUTPUT
Enter elements in the list:[20, 25, 18, 9, 45, 39, 52, 67]
List elements are: [20, 25, 18, 9, 45, 39, 52, 67]
List elements from 2nd to end are : [20, 25, 18, 9]
'''
'''28. Write a program to return the items from 2nd to the end.
'''
'''
OUTPUT
Enter elements in the list:[22, 37, 54, 68, 25]
List elements are: [22, 37, 54, 68, 25]
List elements from 2nd to end are : [54, 68, 25]
'''
''29. Write a program to create another list. Add these two lists.
'''
list3=list1+list2
'''
OUTPUT
Enter elements of the list 1:[32,44,67]
List elements are: [32, 44, 67]
Enter elements of the list 2:[98,76,56]
List elements are: [98, 76, 56]
Joint List is: [32, 44, 67, 98, 76, 56]
'''
import numpy as np
ar1=np.array([40,50,20,20,40,60,20,30,60,30,80,90])
print("Mean is:",np.mean(ar1))
'''
OUTPUT
Mean is: 45.0
'''
'''31. Write a program to calculate the median using numpy library.'''
import numpy as np
ar2=np.array([46,53,25,20,47,60,60,38,80,99])
print("Median:",np.median(ar2))
'''
OUTPUT
Mean is: 45.0
'''
import numpy as np
ar3=np.array([56,34,87,23,46,53,25,20,47,60,60,12,60,90,54,32,43,67])
print("Standard Deviation:",np.std(ar3))
'''
OUTPUT
Standard Deviation: 20.973013230115917
'''
'''33. Write a program to calculate the Variance using numpy library.'''
import numpy as np
ar4=np.array([56,34,87,23,46,53,25,20,47,60,60,12,60,90,54,32,43,67])
print("Variance:",np.var(ar4))
'''
OUTPUT
Variance: 439.86728395061726
'''
'''34. Write a program to plot a line chart for the given data using MatPlotLib
library.'''
'''
''35. Write a program to plot a Bar graph for the given data using MatPlotLib
library.
'''
import matplotlib.pyplot as plt
stuname=['Amit','Ajay','Abhay','Rohit','Mohan','Sagar']
marks=[30,70,60,80,40,20]
plt.barh(stuname,marks)
plt.xlabel('Marks:')
plt.ylabel('Student name:')
plt.show()
'''
OUTPUT
'''
'''36. Write a program to plot a Scatter Chart for the given data using
MatPlotLib library.
'''
import matplotlib.pyplot as plt
Stnumber=[1,2,3,4,5,6]
percentage=[30,70,60,80,40,20]
plt.scatter(Stnumber,percentage)
plt.xlabel('Student Roll Number:')
plt.ylabel('Percentage:')
plt.show()
'''
OUTPUT
'''
'''37. Write a program to plot a Bar Graph for the given data using MatPlotLib
library.
'''
import matplotlib.pyplot as plt
Info=['Gold','Silver','Bronze','Total']
Australia=[60,80,90,190]
plt.bar(Info,Australia)
plt.xlabel("Medal Type")
plt.ylabel("Australia Medal Count")
plt.show()
'''
OUTPUT
'''
''38. Write a program to plot a Pie Chart for the given data using MatPlotLib
library.
'''
import matplotlib.pyplot as plt
Col=[12000,8000,3000,9800,12300,7500]
Section=['A','B','C','D','E','F']
plt.title("Volunteering Week Collection")
plt.pie(Col, labels=Section)
plt.show()
'''
OUTPUT
'''