In this tutorial, I will explain how to square a number in Python. As a data scientist working for a US-based company, I often perform mathematical operations like squaring numbers for data analysis tasks. Let’s explore various methods to square a number in Python with examples and screenshots of executed example code.
Methods to Square a Number in Python
We will learn how to square a number in Python using various methods. Let’s get into the different techniques to achieve this task.
Read How to Check if a Number is NaN in Python?
Method 1. Use Multiplication
The simplest and most intuitive way to square a number is by multiplying the number by itself. This method is simple and easy to understand.
# Example: Squaring the population of New York City
population_nyc = 8419600
squared_population = population_nyc * population_nyc
print(f"The squared population of New York City is {squared_population}")Output:
The squared population of New York City is 70889664160000I have executed the above example code and added the screenshot below.

In this example, we squared the population of New York City by multiplying it by itself. This method is efficient and works well in most cases.
Check out How to Generate Random Numbers Between 0 and 1 in Python?
Method 2. Use the Exponentiation Operator (**) in Python
Python provides an exponentiation operator ** that allows you to raise a number to any power. This operator is also used for square numbers.
# Example: Squaring the area of Los Angeles
area_la = 503
squared_area = area_la ** 2
print(f"The squared area of Los Angeles is {squared_area}")Output:
The squared area of Los Angeles is 253009I have executed the above example code and added the screenshot below.

The exponentiation operator is concise and readable, making it a popular choice among developers.
Read How to Create an Empty Set in Python?
Method 3. Use the pow() Function in Python
Python’s built-in pow() function can also be used to square numbers. The pow() function takes two arguments: the base and the exponent.
# Example: Squaring the GDP of California
gdp_california = 3.2 # in trillion USD
squared_gdp = pow(gdp_california, 2)
print(f"The squared GDP of California is {squared_gdp}")Output:
The squared GDP of California is 10.240000000000002I have executed the above example code and added the screenshot below.

The pow() function is versatile and can handle both integer and floating-point numbers.
Check out How to Convert a Dictionary to an Array in Python?
Method 4. Squaring Elements in a List Using List Comprehension in Python
If you have a list of numbers and you want to square each element, list comprehension is an efficient way to achieve this.
# Example: Squaring the populations of major US cities
populations = [8419600, 3980400, 2716000, 2328000, 1690000] # NYC, LA, Chicago, Houston, Phoenix
squared_populations = [pop ** 2 for pop in populations]
print(f"The squared populations of major US cities are {squared_populations}")List comprehension is a concise way to apply an operation to each element in a list.
Check out How to Concat Dict in Python?
Method 5. Use NumPy for Large-Scale Operations in Python
For large-scale numerical operations, the NumPy library is highly efficient. NumPy provides an array of objects and various mathematical functions, including squaring elements.
import numpy as np
# Example: Squaring the populations of major US cities using NumPy
populations_np = np.array([8419600, 3980400, 2716000, 2328000, 1690000])
squared_populations_np = np.square(populations_np)
print(f"The squared populations of major US cities using NumPy are {squared_populations_np}")NumPy is optimized for performance and is widely used in scientific computing.
Check out How to Get the Length of a Dictionary in Python?
Method 6. Use a Lambda Function with map() in Python
Lambda functions are anonymous functions in Python. You can use them with the map() function to apply an operation to each element in a list.
# Example: Squaring the areas of major US states using a lambda function
areas = [695662, 423967, 380831, 268596, 163696] # Alaska, Texas, California, Montana, New Mexico
squared_areas = list(map(lambda x: x ** 2, areas))
print(f"The squared areas of major US states are {squared_areas}")Using lambda functions map() provides a functional programming approach to squaring numbers.
Read How to Save Python Dictionary to a CSV File?
Conclusion
In this tutorial, I explained how to square a number in Python. We discussed some methods like multiplying the number by itself, exponential operator(**) to raise a number to any power, pow() function to square a number, list comprehension is an efficient way, NumPy library is highly efficient, and lambda functions with the map() function.
You may also like to read:
- How to Convert a Dictionary to a List in Python?
- Python Dictionary Count
- How to Unpack a Tuple in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.