Python Tuple Exercises Let’s check out some exercises that will help understand tuples better. Exercise 7-a Assign the first element of the tuple to answer_1 on line 2 tt=(11, 100, 99, 1000, 999) answer_1= print(answer_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(answer_1,tt[0],"answer_1 checks") myTests().main() Hint 1 tuple[index]Correct syntax to call a tuple’s element is brackets with the index number inside next to the tuple’s name: Hint 2 Index starts with zero. Solution answer_1 = tt[0] Exercise 7-b This time print the third element of the tuple. tt=(11, 100, 101, 999, 1001) #Type your answer here. answer_1= print(answer_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(answer_1,tt[2],"answer_1 checks") myTests().main() Hint 1 0,1,2 … Third element is index 2: [2] Solution answer_1 = tt[2] Exercise 7-c Print the second from the last element of the tuple. lst=(11, 100, 101, 999, 1001) #Type your answer here. answer_1= print(answer_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(answer_1,lst[-2],"answer_1 checks") myTests().main() Hint 1 Second from last element has index of -2. Solution answer_1 = lst[-2] Exercise 7-d What's the index of 2? tt=(55, 777, 54, 6, 76, 101, 1, 2, 8679, 123, 99) # Type your code here. answer_1= print(answer_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(answer_1,tt.index(2),"answer_1 checks") myTests().main() Hint 1 .index() takes 1 parameter which is the element you’re looking for. Just type it inside .index() and print it to see the value. Solution answer_1=tt.index(2) Exercise 7-e How many times does 777 occur? tt=(55, 6, 777, 54, 6, 76, 7777, 1, 777, 2, 6) # Type your code here. answer_1= print(answer_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(answer_1,tt.count(777),"answer_1 checks") myTests().main() Hint 1 .count() method tells how many times a value occur in a tuple. You need to type the value you’re checking inside .count() Solution answer_1=tt.count(777) Exercise 7-f What is the sum of all the numbers in the tuple? tt=(42, 1092, 11, 88, 65, 2, 6) # Type your code on line 4: ans_1= print(ans_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,sum(tt),"ans_1 checks") myTests().main() Hint 1 sum() function will calculate the total sum inside the numbers of your tuple. Solution ans_1 = sum(tt) Exercise 7-g What is the minimum value in the tuple? tt=(66, 555, 11, 101, 9, 1001) # Type your code on line 4: ans_1= print(ans_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,min(tt),"ans_1 checks") myTests().main() Hint 1 min() function will show the minimum value of the tuple. Solution ans_1 = min(tt) Exercise 7-h What is the maximum value in the tuple? tt=(66, 555, 11, 101, 9, 1001) # Type your code on line 4: ans_1= print(ans_1) ==== from unittest.gui import TestCaseGui class myTests(TestCaseGui): def testOne(self): self.assertEqual(ans_1,max(tt),"ans_1 checks") myTests().main() Hint 1 max() function will show the minimum value of the tuple. Solution ans_1 = max(tt)