Whether you’re a math enthusiast, a developer, or just somebody looking to learn new tricks on your Mac, you might find it interesting to learn how to cube numbers. In this blog post, we will walk you through the process of creating a simple program on your Mac to cube numbers.
Running Python on Your Mac
All Macs come with Python installed. Python is a high-level programming language that’s great for coding beginners. It’s also perfect for our purpose today. To run Python on your Mac, you’ll need to use the Terminal app. To launch Terminal, you can use the Finder or Spotlight Search. Once Terminal is open, type “python” and press enter. You should see something like this:
Python 2.7.16 (default, Jun 17 2020, 12:21:02) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.10.44.4)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
Creating a Cubing Function in Python
Next, let’s create a function in Python that will take a number as input and return this number cubed. Here is how to do it:
def cube(n): return n ** 3
By typing in the above code, you’ve just created a function named “cube” that takes one parameter, “n”, and returns the cube of “n”.
Using the Cubing Function
To use this function, simply call it with a number as the argument. For instance, to get the cube of 3, you would type:
After pressing enter, you should see “27” printed in the console, which is indeed the cube of 3.
There you have it! You’ve just learned how to cube numbers using Python on your Mac. While this is a simple example, it showcases the power of Python and the ease of use of Mac’s Terminal app. Happy cubing!