Page 412 - Ai_417_V3.0_C9_Flipbook
P. 412
Commands Output
fruits = ["apple", "banana", "cherries"] ['apple', 'banana', 'cherries', 'melon']
fruits.append("melon")
print(fruits)
cars = ["Maruti", "Ford"] ['Maruti', 'Ford', 'Honda', 'Hyundai']
cars.append("Honda")
cars.append("Hyundai")
print(cars)
#input names of 5 friends and store in list Enter Friend Name: Amit
friends=[] Enter Friend Name: Sneha
for i in range(5): Enter Friend Name: Swati
nm=input("Enter Friend Name: ") Enter Friend Name: Sudhir
friends.append(nm) Enter Friend Name: Shashi
print(friends) ['Amit', 'Sneha', 'Swati', 'Sudhir', 'Shashi']
#adding a list at the end of another list ['cat', 'dog', 'rabbit', ['tiger', 'lion']]
pets = ['cat', 'dog', 'rabbit']
wild = ['tiger', 'lion']
pets.append(wild)
print(pets)
The extend( ) Function
The extend() function is used to append multiple values at a time in a list. The syntax of the extend()
function is:
list.extend(iterable value)
where iterable value can be a list. For example,
Commands Output
n=[1,2,3,4] [1, 2, 3, 4, 5]
m=[5]
n.extend(m)
print(n)
pets = ['cat', 'dog', ['cat', 'dog', 'rabbit', 'tiger', 'lion']
'rabbit']
wild=['tiger', 'lion']
pets.extend(wild)
print(pets)
410 Touchpad Artificial Intelligence (Ver. 3.0)-IX

