Python Len Exercises Let’s check out some exercises that will help understand len() function better. Exercise 10-a Using len() function find out how many items are in the list. lst=[11, 10, 12, 101, 99, 1000, 999] answer_1= print(answer_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(answer_1,len(lst),"length checks") myTests().main() Hint 1 You can pass your object’s name inside len() function: len(object) Solution answer_1 = len(lst) Exercise 10-b len() function can also tell the length of a string. Find out the length of the string given below. msg="Be yourself, everyone else is taken." #Type your code here. msg_length= print(msg_length) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(msg_length,len(msg),"length checks") myTests().main() Hint 1 len(object) Solution msg_length = len(msg) Exercise 10-c How many keys are there in the dictionary? dict={"Real Madrid": 13,"AC Milan": 7,"Bayern Munich":5 ,"Barcelona": 5, "Liverpool": 5} #Type your answer here. ans_1= print(ans_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,len(dict),"length checks") myTests().main() Hint 1 len(object) Solution ans_1 = len(dict) Exercise 10-d Call the last element of the list by using the len() function. gift_list=['socks', '4K drone', 'wine', 'jam'] # Type your code here. index_no= ans_1= print(ans_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,gift_list[-1],"ans_1 checks") myTests().main() Hint 1 len() function will give you the length of a list. Since index starts from 0, you can subtract 1 from the length to find the index of the last item. Solution index_no = len(gift_list)-1ans_1 = gift_list[index_no] Exercise 10-e Below is a nested data. Find out the length of the item: 'raincoat'. You can use reverse indexing. gift_list=['4K drone', 'wine', 'jam', ['socks', 'pajamas', 'raincoat']] # Type your code here. ans_1= print(ans_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,len(gift_list[-1][-1]),"ans_1 checks") myTests().main() Hint 1 [-1] will let you access the last item of a list. Hint 2 When you have nested data, to reach the last element of the last element:You can chain your accessing notation as: [-1][-1] Solution ans_1 = len(gift_list[-1][-1])