• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / How to Cube Numbers in Python

How to Cube Numbers in Python

May 5, 2022 Leave a Comment

To cube a number in Python, the easiest way is to multiply the number by itself three times.

cube_of_10 = 10*10*10

We can also use the pow() function from the math module to cube a number.

import math

cube_of_10 = math.pow(10,3)

Finally, we can find the cube of a number in Python with the built in exponentiation operator **.

cube_of_10 = 10**3

Cubing numbers is a common task to do when working with numbers in a program. In Python, we can easily cube a number.

By definition, the cube of a number is the number multiplied by itself three times. We can calculate the cube of a number in Python by multiplying it by itself three times.

Below are some examples of how to find the cubes of various numbers in Python

print(4*4*4)
print(9*9*9)
print(13*13*13)
print(80*80*80)

#Output:
64.0
729.0
2197.0
512000.0

Squaring a Number in Python with the math module pow() Function

The Python math module has many powerful functions which make performing certain calculations in Python very easy.

One such calculation which is very easy to perform in Python is finding the cube of a number.

The pow() function from the Python math module also lets us compute the cube of a number.

The pow() function takes two numbers as input, the first number is the base and the second number is the exponent.

For cubing in Python, we pass “3” to the second parameter in the pow() function.

Below are some examples of how to use the pow() function to find the cubes of various numbers.

import math

print(math.pow(4, 3))
print(math.pow(9, 3))
print(math.pow(13, 3))
print(math.pow(80, 3))

#Output:
64.0
729.0
2197.0
512000.0

Finding the Cube of a Number with the ** Operator in Python

We can also use the built in ** to find exponents in Python. To find a cube with the built in exponentiation operator ** , we just put “3” after **.

Below are some examples of how to use the Python built in ** operator to find the cubes of different numbers.

import math

print(4**(3))
print(9**(3))
print(13**(3))
print(80**(3))

#Output:
64.0
729.0
2197.0
512000.0

Hopefully this article has been useful for you to learn how to cube numbers in Python.

Other Articles You'll Also Like:

  • 1.  Get Public IP Address Using Python
  • 2.  Comparing Datetimes in Python
  • 3.  Python Prime Factorization – Find Prime Factors of Number
  • 4.  How to Clear Turtle Screen in Python with clear() Function
  • 5.  Using Selenium to Check if Element Exists in Python
  • 6.  pandas ceil – Find the Ceiling of a Column Using Numpy ceil
  • 7.  Create Symbolic Link with Python os.symlink() Function
  • 8.  Get Last Character in String in Python
  • 9.  Python Round to Nearest 10 With round() Function
  • 10.  Python Add Days to Date Using datetime timedelta() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy