How do you square a number in a list in python?

The square of a number is that number multiplied by itself. But what are the ways to do that in the Python programming language? Let’s find out.

IN THIS ARTICLE:

  • Three ways to square numbers in Python
  • Square a Python number with the exponent operator: **
    • Example: square Python numbers with exponent operator
  • Square a Python number with the pow() function
    • Example: square values with Python’s pow() function
  • Square a number in Python with multiplication
    • Example: square Python values by multiplying
  • Square all values in a Python list or array
    • Square multiple values with a list comprehension
    • Square values with Python’s for loop
  • Summary

# Three ways to square numbers in Python

In mathematics, a square is the result of multiplying a number by itself (Wikipedia, 2019). That multiplication is done just once, like so: n x n. That makes squaring the same as raising a number to the power of 2.

For example, 9 x9 is 81 just as 9 squared (92) is 81 as well. One feature of squares is that they’re always positive. This is because a negative times another negative always gives a positive. For instance, -9 squared, or (-9)2, is also 81.

There are several ways to square a number in Python:

  • The ** (power) operator can raise a value to the power of 2. For example, we code 5 squared as 5 ** 2.
  • The built-in pow() function can also multiply a value with itself. 3 squared is written like: pow(3, 2).
  • And, of course, we also get the square when we multiply a value with itself. 4 squared then becomes 4 * 4.

Each approach gives the correct answer, and one isn’t better than the others. Just pick the one you like the most.

Let’s see how each option works in Python.

# Square a Python number with the exponent operator: **

The first way to square a number is with Python’s exponent (**) operator. Those two asterisks have Python perform exponentiation (Matthes, 2016). To square a value we can raise it to the power of 2.

So we type the number to square, then **, and end with 2. For example, to get the square of 3 we do:

# Example: square Python numbers with exponent operator

Let’s see how squaring with ** works in practice:

# Some random values
valueA = 3
valueB = 12
valueC = 25
valueD = 120.50
valueE = -75.39

# Square each value
squareA = valueA ** 2
squareB = valueB ** 2
squareC = valueC ** 2
squareD = valueD ** 2
squareE = valueE ** 2

# Output each value squared
print(valueA, "squared =", squareA)
print(valueB, "squared =", squareB)
print(valueC, "squared =", squareC)
print(valueD, "squared =", squareD)
print(valueE, "squared =", squareE)

This mini-program first makes five variables, named valueA through valueE.

We square the value of each variable next. For that we use the variable’s name, the ** exponent operator, and type 2. We store the squared results in the squareA through squareE variables.

Then we call Python’s print() function to display both the original and squared value. This is how that output looks:

3 squared = 9
12 squared = 144
25 squared = 625
120.5 squared = 14520.25
-75.39 squared = 5683.6521

# Square a Python number with the pow() function

Another way to square numbers is with the built-in pow() function. That function raises some value to a certain power (Lutz, 2013). Its first argument is the number we want to raise; the second argument is the exponent. When we square with pow(), that second argument is always 2.

For example, to square 3 we type:

We can also square a number with the math.pow() function. That function accepts the same arguments, but returns a floating-point value each time. So math.pow(3, 2) gives 9.0 rather than 9.

# Example: square values with Python’s pow() function

Here’s how we use the pow() function in practice:

# Some random values
valueA = 3
valueB = 12
valueC = 25
valueD = 120.50
valueE = -75.39

# Square each value with pow()
squareA = pow(valueA, 2)
squareB = pow(valueB, 2)
squareC = pow(valueC, 2)
squareD = pow(valueD, 2)
squareE = pow(valueE, 2)

# Output the results
print(valueA, "squared =", squareA)
print(valueB, "squared =", squareB)
print(valueC, "squared =", squareC)
print(valueD, "squared =", squareD)
print(valueE, "squared =", squareE)

Here we first make 5 different variables (valueA through valueE).

Then we square each of those variables. For that we call pow() with two arguments: the variable and 2. Doing so raises the variable’s value to the power of 2, which gives us the square. We put those squares in the squareA through squareE variables.

The last bit of code outputs the original and squared value. To do that we call the print() function several times. Here’s what that displays:

3 squared = 9.0
12 squared = 144.0
25 squared = 625.0
120.5 squared = 14520.25
-75.39 squared = 5683.6521

# Square a number in Python with multiplication

A square is just a number multiplied by itself. And so another option is to do that multiplication with the * operator directly.

So to square 3, we multiply that value with itself:

# Example: square Python values by multiplying

So to square values with the * operator we just multiply each value with itself. Here’s an example program that does just that:

# Some random values
valueA = 3
valueB = 12
valueC = 25
valueD = 120.50
valueE = -75.39

# Square each value by multiplying with itself
squareA = valueA * valueA
squareB = valueB * valueB
squareC = valueC * valueC
squareD = valueD * valueD
squareE = valueE * valueE

# Display the results
print(valueA, "squared =", squareA)
print(valueB, "squared =", squareB)
print(valueC, "squared =", squareC)
print(valueD, "squared =", squareD)
print(valueE, "squared =", squareE)

We first make five variables, valueA through valueE. Each has a value that we want to square.

That squaring is what we do next. We multiply each variable with itself (for instance, valueA * valueA) and store the result in a new variable (squareA).

Then we output both the original and its squared value with several print() statements. This is how that output looks:

3 squared = 9
12 squared = 144
25 squared = 625
120.5 squared = 14520.25
-75.39 squared = 5683.6521

# Square all values in a Python list or array

The previous examples all squared a single value at a time. But sometimes we have a list or array whose values we need to square. Let’s look at two possible approaches for that.

# Square multiple values with a list comprehension

One option that squares a sequence of values is a list comprehension. Those runs efficient and requires just a bit of code.

Here’s how a list comprehension can square each value in a list:

# Some random values
numbers = [
    23, 3, 7, 81, 12,
    490, 312, 1234, 99
]

# Generate list with each value squared
squared = [number ** 2 for number in numbers]

# Output data
print("Original values:\n", numbers)
print("Values squared:\n", squared)

This example first makes a list named numbers. Its contents are various integer values.

Then we generate a new list with a list comprehension. The code between square brackets ([ and ]) squares each number value with the exponent (**) operator.

Those number values are generated by an in-line for loop expression: for number in numbers. This goes through our original numbers list and makes each element available as the number variable, one at a time.

After that list comprehension the squared list has each value squared. We then output the original and squared values with Python’s print() function. Here’s what that displays:

Original values:
 [23, 3, 7, 81, 12, 490, 312, 1234, 99]
Values squared:
 [529, 9, 49, 6561, 144, 240100, 97344, 1522756, 9801]

Of course, we could also squared the values in a different way. For instance, with simple multiplication:

# Generate list with each value squared
squared = [number * number for number in numbers]

Don’t need to keep the original values? Then a list comprehension can also overwrite the existing list with squared values. For that assign the list the outcome of the list comprehension. For instance:

# Square each value in the 'numbers' list,
# and overwrite the original list
numbers = [value ** 2 for value in numbers]

# Square values with Python’s for loop

Another option that squares values is Python’s for loop. This requires a bit more code than a list comprehension, but offers more flexibility. For instance inside a loop we can easily do more tasks than just squaring values. Plus a for loop is easier to read when code gets complex.

Here’s how a for loop can square values:

# Some random values
numbers = [
    23, 3, 7, 81, 12,
    490, 312, 1234, 99
]

# Generate list with each value squared
squared = []

for number in numbers:
    squared.append(number ** 2)

# Output data
print("Original values:\n", numbers)
print("Values squared:\n", squared)

Here we first make the numbers list. That list has several integer values. Then we make a second list, squared. This one starts empty ([]) but we’ll fill it inside the loop.

Next a for loop goes through all values in the numbers list. During each loop cycle, the number variable refers to a single element from that list.

Inside the loop we call the append() method on our squared list. That adds a new element to that list. The value we add is the number variable raised to the power 2 (number ** 2). Because the loop repeats this process for every list value, when the loop ends we got all values squared.

Finally we output both the original and square list with the print() function. Here’s what that displays:

Original values:
 [23, 3, 7, 81, 12, 490, 312, 1234, 99]
Values squared:
 [529, 9, 49, 6561, 144, 240100, 97344, 1522756, 9801]

Above we stored the squared values in a new list. If you don’t have to keep the original list, you can also overwrite it with squared values. When we do that with the for loop, Python’s enumerate() function is a big help to get both the value and its index. For example:

# Go through the original 'numbers' list, and
# square each number (replacing the original list)
for index, number in enumerate(numbers):
    numbers[index] = number ** 2

LEARN MORE

  • Raise numbers to a certain power in Python
  • How to get the square root of a Python value?
  • How to see if a Python number is a perfect square?

# Summary

A square is a number multiplied by itself. Python has three ways to square numbers.

The first is the exponent or power (**) operator, which can raise a value to the power of 2. We can calculate a square in the same way with the built-in pow() function. The third way is, of course, to multiply (*) a value with itself.

To square a sequence of numbers we can use a list comprehension or regular for loop. The first is compact and requires little code; the second is easier to read and handles complex situations well.

References

Lutz, M. (2013). Learning Python (5th Edition). Sebastopol, CA: O’Reilly Media.

Matthes, E. (2016). Python Crash Course: A Hands-On, Project-Based Introduction to Programming. San Francisco, CA: No Starch Press.

Wikipedia (2019, September 6). Square (algebra). Retrieved on September 18, 2019, from https://en.wikipedia.org/wiki/Square_(algebra)

Published December 20, 2019.

« All Python math articles

How do you square each element in Python?

square() in Python. numpy. square(arr, out = None, ufunc 'square') : This mathematical function helps user to calculate square value of each element in the array.

How do you put a squared on Python?

Squaring numbers in Python: Using the Exponent Operator. Multiplying the number by itself (n*n) Using pow()