Page 328 - AI Ver 1.0 Class 9
P. 328
condition is False, the control exits from the loop.
Statements are always indented can be single or a block and are repeated till the condition is True.
8. What is negative indexing? Explain with an example.
Ans. In negative indexing, the index number begins with -1 till -length and we refer the elements from right to left.
For example, book=['C', 'O', 'M', 'P', 'U', 'T', 'E', 'R']
So, each letter gets an element number starting from -1
print(book[-2]) will give an output ['E']
print(book[-7]) will give an output ['O']
9. How can we modify elements in a list?
Ans. List is mutable so data can be easily modified by overwriting a new value to an existing value in a given list by using an
assignment operator (=).
Syntax
list[index] = newvalue
For example,
l1 = [10, 20, 40, 50]
l1[3] = 100
print(l1)
10. Find the output of the following codes:
a. l1=[10,20,40,50] [10, 'abc', 'xyz', 50]
l1[1:3]=["abc","xyz"]
print(l1)
b. names=["yash","amit","rhea","mayank"] ['amit', 'mayank', 'rhea', 'yash']
names.sort()
print(names)
c. A=[10,20,34,56,"abc","xyz",89.5,67] [10, 20, 34, 56, 'xyz', 89.5]
A.remove("abc")
A.pop()
print(A)
d. l1=[1,2,3,4,5,6] [2, 3, 4, 5, 6, 1]
print(l1[1:]+l1[:1])
e. x=2 [1, 1][2, 2][3, 3][4, 4]
for i in range(1,5):
l=[i]*x
print(l,end='')
11. Write an algorithm to check if a number is an Armstrong number.
Ans. Step 1: Start
Step 2: Declare Variable sum, temp, n
Step 3: Read n from user
Step 4: Initialize Variable sum=0 and temp=n
Step 5: Repeat Until n>=0
326 Touchpad Artificial Intelligence-IX

