Page 342 - Artificial Intellegence_v2.0_Class_9
P. 342
if i%2==0:
sumeven+=i
else:
sumodd+=i
i+=1
print("Sum of even :",sumeven)
print("Sum of odd :",sumodd)
5. Input a number and print the next five consecutive numbers.
Ans. n=int(input("Enter a number :"))
for i in range(n+1,n+6):
print(i)
6. Print each element of list along with its index number (both forward and backward).
Ans. L1= ['P', 'Y', 'T', 'H', 'O', 'N']
length=len(L1)
for e in range(length):
print("Forward indexing:",e, "Backward Indexing:"\,(e-length), "Element Value is
",L1[e])
7. Print the sum of odd numbers in a list.
Ans. 1=[1,12,6,3,8,7,13]
sum=0
for i in range(len(1)):
if 1[i]%2==0:
sum+=1[i]
print(sum)
8. Swap the adjacent elements in the list. Print the list after swapping is done.
Ans. #swapping the adjacent value
11=[1,2,3,4,5]
print("list before change",11)
1=len(11)
if 1%2==0:
for i in range(0,1,2):
11[i], 11[i+1]=11[i+1],11[i]
else:
for i in range(0,1–1,2):
11[i],11[i+1]=11[i+1],11[i]
print("list after change",11)
9. To store only integers in a new list by copying from the given list.
Ans. l=[12,"abc",45.6,True,34,45]
l2=[]
for i in l:
if type(i)==int:
l2.append(i)
print(l2)
340 Touchpad Artificial Intelligence (Ver. 2.0)-IX

