Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Tuple Sums (Solution)

00:00 Let’s start with creating the data tuple. I’m going to open and close parentheses, and then inside of here I can create two more tuples that are each going to contain two integers, so I’m making another set of parentheses, and then inside of that set of parentheses, I’m putting data just one and two.

00:19 Then I will put a comma after and then create the next element of the data tuple, which is another tuple. And then this one contains the integers three and four, separated by commas again, so we have

00:34 data tuple. And the data tuple also contains two tuples. So if I use the type() function and pass in data[0], for example. So accessing the first tuple in my nested tuple.

00:50 Then this is also a tuple, so we’ve got a nested tuple called data.

00:56 The next task was to loop over data and print the sum of each nested tuple in there. Okay, this is kind of how I remember the task.

01:11 We’re going to need a for loop, but I want to keep track of the index of the data. So I’m going to start off by creating an index variable that I’ll start with one, and then I’m writing the for loop for each row in the data tuple, I want to print an f-string, so it’s just a nice way of formatting it.

01:38 And then here, I think it was saying row, and then the row number, which is going to be the index,

01:47 and then the sum column. And then here I need to sum the values in the tuple, which you can do by using the built-in sum() function. So I can use sum() and then pass in the row.

02:01 So this row is the loop variable,

02:05 and that’s going to take on the values of first, the first tuple, and then the second tuple. And summing all elements in each of those rows using, using this sum() opening parentheses, and then passing in the loop variable row.

02:20 Okay, that should be the string that I want to print. Then I still need to increase the index. So I’m going to say index += 1 so that then, it’s going to say row number two when it goes the second time through the loop, let’s try it out.

02:37 Row one, sum three and row two sum seven. The output looks just like it should. Perfect.

02:45 Note that there’s also another way of solving this using the built-in enumerate() function. Maybe you’ve heard about it, maybe not. If not, and you’re happy with the solution that’s similar to the one before where you define an index variable, then that’s completely fine.

03:00 But I wanted to show you this enumerate() function too, because it makes the code look a little more Pythonic. I would say. You can say for row_number, , row in enumerate(data), and then I have to define that I want to start at one because otherwise it starts at zero by default.

03:24 And now I can directly print out,

03:29 I have access to both the row number as well as the row loop variable here. You could also keep calling that index, but I feel like row number is a bit more descriptive.

03:41 So print that out. And that’s it actually. So you don’t need to work with an index variable, and you don’t need to increment the index variable because enumerate() does that for you.

03:51 What enumerate() does is it returns a tuple, and then over here inside of your for loop, you are actually unpacking that tuple gaining two values.

04:00 First, a counter that starts at one. This is what you defined by saying start=1, and then the element enumerate() wraps these two things into a tuple.

04:10 And here you’re unpacking that tuple so that you have access to both of those, and you can use them in your loop. In this case, just for the f-string for your call to print().

04:20 And as you can see, the output is the same, but the code is shorter and a little easier to reason about, at least if you’ve heard about the enumerate() function before.

04:31 Now you have. So hope that’s a useful addition to your tool belt.

Become a Member to join the conversation.