Login

Back

An introduction to lists

Q1. Type this code into Python and get it working:

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)

farmAnimals[3] = 'chicken'

print('\n')
print(len(farmAnimals))
for item in farmAnimals:
    print (item)

Q2. How many farm animals are there, according to the program.
Q3. What position is 'cow' at?
Q4. What position is 'sheep' at?
Q5. Modify your code to change 'goat' to 'duck'.
Q6. It is not possible to change a string using an index once the string has been created. Strings are immutable. It is possible to change a list once it has been created. What technical term do we give to this?
Q7. Add some code to print out the length and contents of the other lists (wildAnimals, data and newCrops).
Q8. Create a new list called allAnimals by concatenating the farmAnimals and wildAnimals lists. 
Q9. Print out the length of the allAnimals list and all the items in it.
Q10. In the list Data, what data type is True?

Back