Find Cube of a Number - Python
Last Updated :
05 May, 2025
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
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
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
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))
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))
Explanation: loop multiplies n with itself 3 times, storing the result in res
Related article:
Similar Reads
Python Program to Find Cube of a Number 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 n
2 min read
Factorial of a Number - Python The factorial of a number is the product of all positive integers less than or equal to that number. For example, the factorial of 5 (denoted as 5!) is 5 Ã 4 Ã 3 Ã 2 Ã 1 = 120. In Python, we can calculate the factorial of a number using various methods, such as loops, recursion, built-in functions,
4 min read
Python program to find power of a number The task of finding the power of a number in Python involves calculating the result of raising a base number to an exponent. For example, if we have a base 2 and an exponent 3, the result is 2^3=8 .Using ** operatorThis is the simplest and most Pythonic way to calculate the power of a number. The **
2 min read
Minimum of two numbers in Python In this article, we will explore various methods to find minimum of two numbers in Python. The simplest way to find minimum of two numbers in Python is by using built-in min() function. Pythona = 7 b = 3 print(min(a, b))Output3 Explanation:min() function compares the two numbers a and b.So, it retur
2 min read
Sum the Digits of a Given Number - Python The task of summing the digits of a given number in Python involves extracting each digit and computing their total . For example, given the number 12345, the sum of its digits is 1 + 2 + 3 + 4 + 5 = 15. Using modulo (%)This method efficiently extracts each digit using the modulus (%) and integer di
2 min read
Python - Convert Number to List of Integers We need to split a number into its individual digits and represent them as a list of integers. For instance, the number 12345 can be converted to the list [1, 2, 3, 4, 5]. Let's discuss several methods to achieve this conversion.Using map() and str() Combination of str() and map() is a straightforwa
3 min read