Back

Slicing tuples - Answers

monsters=('goblin', 'orc', 'evil pixie', 'ogre', 'firedog', 'mummy', 'witch')

Q1. print(monsters[2:6]) >>> ['evil pixie', 'ogre', 'firedog', 'mummy']
Q2. print(monsters[0:4]) >>> ['goblin', 'orc', 'evil pixie', 'ogre']
Q3. print(monsters[:3]) >>> ['goblin', 'orc', 'evil pixie']
Q4. print(monsters[5:2]) >>> []
Q5. print(monsters[3:11]) >>> ['ogre', 'firedog', 'mummy', 'witch']
Q6. print(monsters[4:]) >>> ['firedog', 'mummy', 'witch']
Q7. print(monsters[:]) >>> ['goblin', 'orc', 'evil pixie', 'ogre', 'firedog', 'mummy', 'witch']
Q8. print(monsters[-4:-1]) >>> ['ogre', 'firedog', 'mummy']
Q9. print(monsters[-8:]) >>> ['goblin', 'orc', 'evil pixie', 'ogre', 'firedog', 'mummy', 'witch']
Q10. print(monsters[:-4]) >>> ['goblin', 'orc', 'evil pixie']
Q11. print(monsters[0]) >>> goblin
Q12. print(monsters[3]) >>> ogre
Q13. print(monsters[5]) >>> mummy
Q14. print(monsters[-1]) >>> witch
Q15. print(monsters[-5]) >>> evil pixie
Q16. print(monsters[-105]) >>> Error - index out of range.
Q17. print(monsters[2:2*3]) >>> ['evil pixie', 'ogre', 'firedog', 'mummy']
Q18. print(monsters[2.5]) >>> Error - must use an Integer. Floats are not allowed.
Q19. print(monsters['a']) >>> Error - must use an Integer. Strings are not allowed.
Q20. print(monsters[2+2:]) >>> ['firedog', 'mummy', 'witch']

Back