DELHI PUBLIC SCHOOL
NACHARAM
CLASS-9
Subject: Artificial Intelligence
UNIT-5 : PYTHON CODE(TERM1 & TERM2)
____________________________________________________________________________
1. To print personal information like Name, Father’s Name, Class/grade, School Name
Sol:-
Code:
name= input(“Enter your name=”)
fathername=input(“Enter your Father’s Name=”)
grade=int(input(“Enter the class which you are studying=”))
schoolname=input(“Enter your school name=”)
print(“*********”)
print(“Name:”, name)
print(“Father’s Name:”, fathername)
print(“Class:”,grade)
print(“School Name:”, schoolname)
2.
Sol:-
print('*')
print('**')
print('***')
print('****')
print('*****')
3. To find square of number 7
Sol:
number=7
sq= number ** 2
print(“The square of number 7 is =”, sq)
4. To find the sum of two numbers 15 and 20.
Sol:
num1=15
num2=20
result= num1+num2
print(“The sum of two numbers is=”, result)
5. To convert length given in kilometers into meters.
Sol:-
km=float(input(“Enter kilometers to convert into meters=”))
mtrs = km * 1000
print(“Meters =”, mtrs)
6. To calculate Simple Interest if the principle_amount = 2000 rate_of_interest = 4.5 time = 10
Sol:-
p=2000
r= 4.5
t=10
si=(p*t*r)/100
print(“The simple interest is =”, si)
7. To calculate Area and Perimeter of a rectangle
Sol:-
l=int(input(“Enter length of rectangle=”))
b=int(input(“Enter breadth of rectangle=”))
area=l*b
perimeter= 2*(l+b)
print(“The area of rectangle is=”, area)
print(“The perimeter of rectangle is=”, perimeter)
8. To calculate Area of a triangle with Base and Height
Sol:-
base=int(input(“Enter base of triangle=”))
height=int(input(“Enter height of triangle=”))
area=½ *base *height
print(“The area of triangle is=”, area)
9. To calculating average marks of 3 subjects
Sol:-
eng=float(input(“Enter English marks-”))
sci=float(input(“Enter Science marks-”))
math=float(input(“Enter Math marks-”))
total= eng+math+sci
average= total/3
print(“THE AVERAGE MARKS OF 3 SUBJECTS =”, average)
10. To calculate discounted amount with discount %
Sol:-
actualprice = float(input("Enter the actual price="))
discount=float(input("Enter discount in percentage="))
discount_amt=actualprice * discount/100
sell_price= actualprice-discount_amt
print("Amount given as discount =", discount_amt)
print("Selling price after discount =", sell_price)
11. To calculate Surface Area and Volume of a Cuboid
Hint: surface_area= 2*(lw*lh*wh); volume=l*w*h
Sol:-
l=int(input('Enter length of cuboid='))
w=int(input('Enter width of cuboid='))
h=int(input('Enter height of cuboid='))
surface_area=2*((l*w)+(l*h)+(w*h))
volume= l*w*h
print('The surface area of cuboid is=', surface_area)
print('The volume of cuboid is=', volume)
12. Program to check if a person can vote
Sol:
name=input(“Enter your name=”)
age=int(input(“Enter your age =”)
If (age >=18) :
print(name, “Can Vote”)
else:
print(name, “Cannot Vote”)
13. To check the grade of a student
Sol:-
name=input('Enter your name=')
percentage=float(input('Enter your overall percentage ='))
if (percentage >= 85 and percentage <=100) :
print('Grade A')
elif (percentage >=60 and percentage < 85):
print('Grade B')
elif (percentage >=40 and percentage < 60):
print('Grade C')
else:
print('Grade D')
14.. Input a number and check if the number is positive, negative or zero and display an
appropriate message
Sol:
num=float(input('Enter a number'))
if(num > 0):
print("Number is positive")
elif(num==0):
print("number is zero")
else:
print("Number is negative")
15. Create a list in Python of children selected for science quiz with following names- Arjun,
Sonakshi, Vikram, Sandhya, Sonal, Isha, Kartik Perform the following tasks on the list in
sequence-
○ Print the whole list
○ Delete the name “Vikram” from the list
○ Add the name “Jay” at the end
○ Remove the item which is at the second position.
Sol:-
name_list=['Arjun','Sonakshi', 'Vikram', 'Sandhya', 'Sonal', 'Isha','Kartik']
print( name_list)
name_list.remove('Vikram')
print(name_list)
name_list.append('Jay')
print(name_list)
name_list.pop(2)
print(name_list)
[Link] a list num=[23,12,5,9,65,44]
○ print the length of the list
○ print the elements from second to fourth position using positive indexing
○ print the elements from position third to fifth using negative indexing
Sol:
num=[23,12,5,9,65,44]
print( 'length of list is=', len(num))
print('second to fourth position using positive indexing=', num[2:4])
print('third to fifth using negative indexing=', num[-3:-1])
[Link] a list of first 10 even numbers, add 1 to each list item and print the final list.
Sol:
lst=[]
temp=0
for i in range(1,21):
if i%2==0:
temp=i
[Link](temp)
temp=temp+1
print(lst)
[Link] a list List_1=[10,20,30,40].
Add the elements [14,15,12] using extend function.
Now sort the final list in ascending order and print it.
Sol:
list_1=[10,20,30,40]
list_1.extend([14,15,12])
list_1.sort()
print(list_1)
19. To print first 10 natural numbers
Sol:
for x in range(1,10+1):
print(x)
[Link] print first 10 even numbers
Sol:
for x in range(1, 21):
if(x % 2 == 0):
print(x)
[Link] print odd numbers from 1 to n
Sol:
last = int(input('enter the number to which you want to generate odd numbers='))
i=1
while(i<=last):
if(i%2 != 0):
print(i)
i=i+1
[Link] print sum of first 10 natural numbers
Sol:
i=1
sum=0
while(i<=10):
sum=sum+i
i=i+1
print(“Sum of 10 natural numbers is=”, sum)
23. Program to find the sum of all numbers stored in a list
Sol:
List1=[12,15,2,23,45,65,67,88,79,9]
sum=0
for x in List1:
sum=sum+x
print('Sum of elements in list is=', sum)