Page 314 - AI Ver 1.0 Class 9
P. 314
l1 = [1, 2, 3] [1, 2, 3, 4, 5, 6, 7, 8, 9]
l3 = [7, 8, 9]
newlist = l1 + [4, 5, 6] + l3
print(newlist)
Using * Operator with List
The * operator is used to replicate a list by a specific number of times. With * operator, one operand has to be a
list and the other should only be an integer, otherwise it will give an error. For example,
Commands Output
A = [1, 2, 3] [1, 2, 3, 1, 2, 3, 1, 2, 3]
B = A * 3
print(B)
X = 5 [5, 5, 5]
A = [X] * 3
print(A)
Commands Output
name = "Amit" ['Amit', 'Amit', 'Amit', 'Amit']
L1= [name] * 4
print(L1)
name = "Amit" TypeError: can't multiply sequence by non-int of type 'str'
L1 = [name] *
"a"
Comparing the Lists
The comparison of the lists is done using comparison operators >, <, >=, <=, != and ==. These operators
compare the elements in lexicographical order. For example, if corresponding elements are same, it goes to the
next element, and so on until it finds elements that differ. For example,
l1 = [1, 2, 3]
l2 = [1, 2, 3]
l3 = [1, 2, [3]]
l4 = [1.0, 2.0, 3,0]
print(l1 == l2) #True
print(l1 > l2) #False
print(l1 == l3) #False
print(l1 == l4) #False
print([1, 2, 6] < [1, 2, 5]) #False
312 Touchpad Artificial Intelligence-IX

