Cube in Python

The cube root of a number is a value that, when multiplied thrice or three times, produces the original value. 

The cube root symbol is denoted by ‘3√’. In the case of the square root, we have used just the root symbol such as ‘√’, also called a radical.

For example, the cube root of 64, denoted as 3√64, is 4 because when we multiply 4 by itself three times, we get 4 x 4 x 4 = 64 = 43.

How to calculate cube root in Python

To find the cube root in Python, use the simple math equation: x ** (1. / 3). It computes the (floating-point) cube root of x. It is a simple math equation that takes the cube root of x, rounds it to the nearest integer, raises it to the third power, and checks whether the result equals x.

x = 27

cr = x ** (1./3.)

print(cr)

Output

3.0

To calculate the exponent number in Python, we use the **. The double star(**) is also called the power operator. To calculate the cube root, we can set the power equal to 1/3.

Find a cube root of a negative number in Python.

To find the cube root of a negative number in Python, first, use the abs() function, and then you can use the simple math equation to calculate the cube root.

We can not find the cube root of the negative numbers the way we calculated for the above method. For example, the cube root of integer -27 should be -3, but Python returns 1.5000000000000004+2.598076211353316j.

Let’s write a complete function that will check if the input number is negative, then it will calculate accordingly.

def cuberoot(x):
    if x < 0:
        x = abs(x)
        cube_root = x**(1/3)*(-1)
    else:
        cube_root = x**(1/3)
    return cube_root


print(cuberoot(27))
print(round(cuberoot(-27)))

Output

3.0
-3

As you can see, we need to round the result to get the cube root’s exact value.

Using Numpy cbrt() function

To calculate a cube root in numpy, use the numpy.cbrt() method. The np.cbrt() function returns the cube root of every array element.

Numpy cbrt() is a mathematical method to find the cube root of every element in a given array.

import numpy as np 

arr1 = [1, 8, 27, 64] 
arr2 = np.cbrt(arr1) 
print(arr2)

Output

[1. 2. 3. 4.]

The np.cbrt() function is the easiest method to calculate a number’s cube root. Unlike the above approaches, it does not get in trouble with negative inputs and returns the exact number like 4 for input 64.

l= [1,2,3,l0l1

l

l3

l4= l6

l7 l8l9 =0

=1=2=3l8=3=6

l

=8

=9[0

Output:

[1, 8, 27, 64]

Method 2 : Using pow() function

This is also the brute force way. In this, we use in-built pow() function

Example:

Python3




# Initializing list

l= [1,2,3,l0l1

l

l3

l4= l6

l7 l8l9 =0

=1,3,4,53,7

l

=8

=9[0

Output:

[1, 8, 27, 64]

Method 3 : Using list comprehension

This task can also be performed using list comprehension. This is similar to above function. Just the difference is that its compact and one liner.

Example:

Python3




# Initializing list

l= [1,2,3,l0l1

l

,5

l4= [,4,5332l7 l8l9 36

l

=8

=9[0

Output:

[1, 8, 27, 64]

Method 4: Using lambda

This can also be achieved using lambda function

Example:

Python3




# Initializing list

l= [1,2,3,l0l1

l

l4= l06l07l08l07l10 l11=3=3 3l15

How do you make a cube in Python?

Step 1: Import libraries. Step 2: In this step, we are selecting the 3D axis of the dimension X =5, Y=5, Z=5, and in np. ones() we are passing the dimensions of the cube. Step 3: In this step, we are selecting color opacity as alpha = 0.9 ( vary from 0.0 – 1.0 ).

How do you show the cube of 3 in Python?

2: Python program to find Cube of given number Using Cube() function.
Take input number from the user..
Calculate the cube of the given number using function..
Print cube of the given number..

How to do cubic root in Python?

You could use x ** (1. / 3) to compute the (floating-point) cube root of x . This takes the cube root of x , rounds it to the nearest integer, raises to the third power, and finally checks whether the result equals x .