Open In App

Find Cube of a Number - Python

Last Updated : 05 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a number and our task is to find the cube of this number in Python. The cube of a number is calculated by multiplying the number by itself twice. For example, if the input is 3, then the output will be 3 * 3 * 3 = 27. In this article, we will learn different ways to find the cube of a number in Python, let's discuss them one by one:

Using Arithmetic Multiplication Operator

A simple and easy way is to manually multiply the number by itself twice by using the arithmetic multiplication operator. This method is clear and intuitive.

Python
n = 5
print(n * n * n)

Output
125

Explanation: Calculates the cube of 5 as 5 * 5 * 5, which equals 125.

Using Exponentiation Operator

The exponentiation operator ** is a straightforward way to raise a number to any power. To find the cube, we can simply raise the number to the power of 3.

Python
n = 5
print(n ** 3)

Output
125

Explanation: Calculates the cube of 5 as 5 ** 3, which equals 125.

Using pow() Function

The built-in pow() function take two argument pow(a,b), where it returns 'a' raised to the power of 'b', so it can also be used to find the cube of a number by setting the parameter b=3.

Python
n = 5

print(pow(n, 3))

Output
125

Using Lambda Function

The Lambda functions are small anonymous functions defined with the lambda keyword. They can be useful for simple calculations.

Python
n = 5

cube = lambda x: x * x * x

print(cube(n))

Output
125

Explanation: Here, the lambda function takes x and returns x * x * x.

Using For Loop

To find the cube of a number using a for loop, you need to multiply the number by itself three times. This can be done by initializing a result variable to 1 and then repeatedly multiplying it by the number.

Python
def cube(n):
    res = 1
    
    for _ in range(3):
        res *= n
    return res
  
n = 5
print(cube(n))

Output
125

Explanation: loop multiplies n with itself 3 times, storing the result in res

Related article:


Practice Tags :

Similar Reads