Login

Back

An introduction to lists - Answers

Q1. Get the code working.
Q2. 4
Q3. Cow is at position 0 (lists always start from index 0).
Q4. Sheep is at position 2
Q5. To change 'goat' to 'duck' : 

farmAnimals[1]='duck'

Q6. It is possible to change a list once it has been created. We say that lists are 'mutable'.
Q7. Code to print out the length and contents of the other lists (wildAnimals, data and newCrops):

farmAnimals = ['cow','goat','sheep','pig']
wildAnimals = ['fox', 'rat', 'badger']
data = [2.0, 17, 'horse', True]
newCrops = []

print(len(farmAnimals))
for item in farmAnimals:
    print (item)

print('\n')

print(len(wildAnimals))
for item in wildAnimals:
    print (item)

print('\n')

print(len(data))
for item in data:
    print (item)

print('\n')

print(len(newCrops))
for item in newCrops:
    print (item)

print('\n')

Q8 and Q9. Create a new list called allAnimals by concatenating the farmAnimals and wildAnimals lists. Print the length and display the contents of the list.

allAnimals = farmAnimals + wildAnimals
print(len(allAnimals))
for item in allAnimals:
    print (item)

Q10. In the list Data, True is Boolean.

Back