Bootcamps

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Academy

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Full-Stack Software Developer - 16w

Data Science and Machine Learning - 16 wks

Search from all Lessons


LoginGet Started

Weekly Coding Challenge

Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!

Start the Challenge

Podcast: Code Sets You Free

A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.

Listen the podcast
← Back to How to's
Edit on Github

How to square a number in python

Written by:

The most straight forward way to square a number in Python would be to use the built-in pow (power) method. It will receive two parameters, the number to square and the amount of times to power that number.

1square = pow(6, 2) 2print(square) 3#Output-> 36

There are many ways or reasons to square a number in python: Using function power(), math.pow() and others, here is a more details explanation:

Creating the square function ourselves:

The first method we are presenting is exactly creating a function that will return the squared number of whatever number it receives: if it receives x we will return x * x.

As you should already know, a squared number is the number multiplied by itself.

Let say the square of 2 would be 4 because 2*2=4. If we were to choose 5, we would receive 25 and so on.

Keeping that in mind, we can create our function:

1def square(num): 2 return num*num 3 4print(square(3)) 5#Output-> 9

Using the power(square) operator:

Python comes with a built in with a power (square) operator **. The syntax is quite straight forward as all the other logic or math operators.

1def squareOperator(base): 2 return base ** 2 3 4print(squareOperator(4)) 5#Output-> 16

Using the math.pow():

The only difference between the math.pow() and pow resides in the first needs the math module imported and it always return a float number, even if the result of the operation has not a floating point or decimals (if you want to learn more about Float numbers, check What is a float in Python).

1import math 2 3def squareMathPow(base, power): 4 return math.pow(base, power) 5 6print(squareMathPow(5,2)) 7#Output-> 25

Using the Numpy np.power() function:

We can also use a built-in function in the numpy library. The syntax is as simple as:

1import numpy as np 2 3print(np.power(5, 2)) 4#Output-> 25

You can find more of these articles at 4Geeks. Hope you enjoyed the reading and keep on the Geek side!