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

Cube (Solution)

00:00 I already pasted and formatted the exercise task. It says, write a function called cube() that takes one number parameter and returns the value of that number raised to the third power.

00:12 Test the function by calling your cube() and displaying the results.

00:17 Okay, so there are basically two tasks that I want to divide. So I will write my function in between those two parts. The first part is write the function and then test the function underneath.

00:31 The function name is cube(), so it’s def cube(): and the cube() function takes one parameter, which is a number. I call this variable num.

00:43 And then in the next line, indented, return num. Well, actually, let’s just return num for now. Run the module to test if the function call works and the number is returned without doing anything to it yet.

01:00 So if you call cube() with the argument 3, then the IDLE shell returns 3 because you don’t do anything with the number yet, but it’s cool to test this.

01:10 So what do you need to do? If you want the third power of a number, what you could do is write num * num * num.

01:22 So in our example, this would be three times three times three. That would actually work. So let’s try it out. If you call cube() with 3,

01:35 then the result is 27, which is 3 raised to the third power. But that doesn’t look very nice. So there is another way you can do that, and that’s using the double asterisk (**).

01:47 So if you adjust the return statement to num**3, you raise the num to the third power.

01:57 Try it out in the shell if it still works.

02:00 If you pass in 3, it’s 27. That’s correct. Cool. So that’s the first part of the exercise. You have created a function that takes a parameter named num and returns that num raised to the third power.

02:14 Okay, let’s tackle the next task of this exercise. Test the function by calling your cube() and displaying the results. Basically, it’s what you did in the IDLE shell, you do in the scripting window.

02:27 Now you call cube(1), and we expect 1. And then call it with 2, so cube(2), and you expect 8.

02:40 And after that, let’s call cube() with the argument 3, which should be 27.

02:47 All right, save the file and run the module.

02:50 Nothing happens because you need a print() function to actually show something in the output. So go to line 7 where you have the first cube() call and add print(cube(1)), and then in the end, closing parentheses, and the same in the next two lines, print(cube(2)), and in the last line print() and then inside the print() call, the cube() call with the 3 and the closing parentheses at the end.

03:22 Now, if you save and run the module, then you should see the expected numbers, which is, well, 1, 8, and 27. So apparently I didn’t do the right math there.

03:34 So it’s not 16, but eight because two raised by the third power is eight, not sixteen. Yeah, that’s a good sign that our cube() function works correctly.

03:47 And sometimes it’s better to have a function than just to rely on your own math skills. But yeah, with all of this done, you have tackled the second part of the exercise, but I want to show you something else before we wrap up this lesson.

04:03 Instead of returning num and then the two asterisks with the 3, you can use the built-in function pow(), which takes a base, which is your num variable, and the exponent, which is 3 in this case.

04:18 And then when you run the module, you see the result is the same. So this would be an alternative solution.

Become a Member to join the conversation.