Cubing a number means multiplying a number three times, which returns some number. The new number is called the cube of the number multiplied three times.

Cubing a number in python is one of the most straightforward tasks to perform using mathematical operators or a module of the python language.

This post will cover the cubing of all kinds of numeric data types available in python.

But before that, we are going to see what are the numeric datatypes that python supports.

To learn more about the data types in python, follow this post.

**Numeric Data Types of Python**

The numeric or number data types of python are broadly classified into three types.

They are integer, floating point, and complex.

Let us see what these numbers are one by one.

**Integer Data Type**

The python language’s integer(int) data type represents numbers like whole numbers, zero, positive numbers, and negative numbers.

The integer data type does not support decimal numbers.

Examples of integer numbers are 3, -11, 0, and 4.

Let us see how we can accept an integer from the user.

```
#user inputs integer number
x=int(input("Enter a number:"))
print("The number you have entered is:",x)
print("The data type of the number is:",type(x))
```

Let us break down the code. In the first line, we are creating a variable to store the number that the user inputs. We are using the `input()`

to accept the user’s input from the keyboard. No matter what the input is, this function always reads the input data as a string.

Please visit this post to learn more about the input() function.

The `int`

keyword is used to convert the above read string to an integer.

In the following line, we are printing the number given by the user.

Lastly, we are checking the data type of the number using the `type()`

function.

Let us see how the user can input the number of their choice.

The output is given below.

**Floating Point Data Type**

The floating point numbers real numbers with a decimal point. These numbers can be positive, negative, and even fractional.

Examples of floating point numbers are 3.142, -2.90, -9.8, and so on.

Let us see an example of `float()`

data type.

```
#user inputs floating point number
y=float(input("Enter a number:"))
print("The number you have entered is:",y)
print("The data type of the number is:",type(y))
```

To explain the above code briefly, we are creating a variable called y to store the input given by the user. We are using the `input()`

function to accept the input from the keyboard. This input is converted into the float data type by the `float`

keyword.

In the last line, we check the type of number the user has given.

The output is given below.

**Complex Data Type**

A number is said to be a complex number if it is in the form of `a+bj`

where a and b are integers. a is the real part, and b is the imaginary part of the number and j*j=-1.

You must be thinking, “The complex numbers I have studied in the higher grade mathematics used to be of the form a+ib , but python uses a different format”?

Well, there is a reason behind this. Most developers or programmers say that this is because python has adopted the conventions of physics and engineering. In Physics and engineering, ‘i’ has different use cases. It is used to denote current, intensity, and an imaginary unit.

`j`

is also used to denote an imaginary unit. But it is used just for this purpose. So to avoid any possible confusion, python uses `a+bj`

convention.

Let us see an example of complex numbers.

```
#user inputs a complex number number
z=complex(input("Enter a number:"))
print("The number you have entered is:",z)
print("The data type of the number is:",type(z))
```

In the first line, we have created a variable called z to store the number given by the user. This number is taken as a complex data type.

We are printing the number entered by the user in the second line.

Lastly, we are printing the type of the number.

Let us see the output of the above code.

Now that we have understood the numeric data types in python, let us try to find out the cube of such numbers.

**How to Cube a Number?**

There are three ways to compute the cube of a number.

- Using the
`**`

operator - Using the
`pow()`

function - Creating a user-defined function

We will see these three methods for all the numeric data types.

**Cubing an Integer**

As we know, we have positive integers and negative integers. So we are going to cube both positive and negative integers.

Let us see an example of how to cube an integer by the three methods.

**Using the ** Operator**

The ** operator is used to multiply a number as many times as the user wishes.

For example, to square the number 2, we use 2**2.

Let us see the code.

```
#using the **
x=int(input("Enter a number:"))
cub=x**3
print("The cube of the number is :",cub)
```

We are storing the user’s input in a variable called x.

We are creating a new variable called cub to perform the cube operation using `**`

operator. Since we are cubing a number, the number after the operator should be 3.

In the following line, we are printing the result.

Let us see the output.

**Using the pow() Function**

The `math`

module of the python language has a built-in function called `pow()`

which can be used to raise a number to the power of another number.

Let us see an example.

```
#using the pow() function
import math
y=int(input("Enter a number:"))
cubed=pow(y,3)
print("The cube of the number is:",cubed)
```

Firstly, we import the math module to use the pow() function.

Next, we create a variable called y to store the number given by the user.

We are creating another variable called cubed to use the pow function on the number. Since we are cubing the number, we must use the number 3.

Lastly, we are printing the result.

**Creating a User-Defined Function **

In the last approach, we will create a function that cubes a number given as input. We are going to see cubing of a negative integer.

The code is given below.

```
#creating a user defined function
z=int(input("Enter a number:"))
def cube(z):
return z*z*z
cu=cube(z)
print("The cube of the number is:",cu)
```

Firstly, we create a new variable called z to store the number given by the user.

In the third line, we create a function using the `def`

keyword. The number stored in the variable z is passed as an argument to the cube function.

We are cubing the number by multiplying the same number three times inside the function.

Next, we create another variable called cu to call the function.

Lastly, we are printing the result.

**Cubing a Floating Point Number**

Let us see how we can cube a floating point number by the three methods.

### **Using the ** Operator**

The code is given below.

```
#using **
a=float(input("Enter a number"))
print("The number you have entered is:",a)
z1=a**3
print("The cube is:",z1)
```

Firstly, we create a variable called a to store the number given by the user.

Next, we print the number given by the user.

We created a new variable called z1 to perform the cube operation by the ** operator.

Next, we print the result of the operation.

The output is given below.

**Using the pow() Function**

Let us see how to use the pow function to cube a floating point number.

```
#cube of a float number using pow()
import math
b=float(input("Enter a number"))
print("The number you have entered is:",a)
z2=pow(b,3)
print("The cube is:",z2)
```

In the first line, we import the math module to use the pow function.

Next, we created a variable called b for storing the input from the keyboard.

Next, we print the number given as input.

We use the pow function to cube the number given as the input.

Lastly, we are printing the result.

**Creating a User-Defined Function**

We will create a user-defined function that cubes the number and prints the result when called.

Let us see the code.

```
#cube of a float number using a function
c=float(input("Enter a number"))
print("The number you have entered is:",c)
#creating a user defined function
def cubef(c):
return c*c*c
cu=cubef(c)
print("The cube of the number is:",cu)
```

In the first line, we created a variable called c to store the input from the user.

In the next line, we print the number.

Next, we create a function called cubef using the `def`

keyword.

Inside this function, we multiplied the same number three times to obtain the cube of the number.

Next, we created a variable called cu to store the result of calling the cubef function.

In the last line, we are printing the result.

**Cubing a Complex Number**

Let us see how we can cube a complex number.

**Using the ** Operator**

```
x=complex(input("Enter a number:"))
x1=x**3
print("The cube of the number you have given is:",x1)
```

The variable x is used to store the input given by the user.

In the following line, we are creating a variable to cube the number given by the user. This variable is x1.

Next, we print the result.

The output is given below.

Wondering how we got the above output?

**Using the pow() Function**

Let us see how we can cube a complex number using `pow()`

.

```
#cube of a complex number
y=complex(input())
print("The number you have entered is:")
y1=pow(y,3)
print("The cube of the complex number is:",y1)
```

The variable y is created to store the input from the user.

In the following line, we print the number given by the user.

Next, we use the pow function to compute the cube. The result is stored in y1.

Lastly, we are printing the cube.

The output is given below.

**Creating a User-Defined Function**

**Creating a User-Defined Function**

Let us see the code for creating a function.

```
#cube of a complex number using a function
z=complex(input("Enter a number"))
print("The number you have entered is:",z)
#creating a user defined function
def cubec(z):
return z*z*z
z1=cubec(z)
print("The cube of the number is:",z1)
```

To explain the above code, in short, we have created a variable called z to store the input from the user. We are printing this number using print. Next, we create a function called cubec with the help of def keyword. Inside this function, we calculate the cube by multiplying z three times by itself.

We are calling this function in the following line and printing the result.

The cube of the given complex number is shown below.

**Conclusion**

To sum up everything, we have seen the different numeric data types supported by python: integer, float, and complex, and their respective examples. We have seen how we can take the user’s choice of numbers from the keyboard in these three data types using the input() function.

We have seen the reason behind the notation of the complex data type in python.

Next, we have seen the three methods or approaches to cube a number. We tried to cube integers, floating point numbers, and also complex numbers with the help of `**`

operator, `pow()`

function, and also a user-defined function.

We have also seen the usage of `type()`

to determine the data type of the input provided.

In cubing a complex number, we observed the mathematical solution behind the output obtained.

**References**

To know more about the pow() built-in function, refer to the official documentation.