Squaring a number means raising it to the power of 2, i.e., multiplying it by itself (e.g., a^2 = a × a).
The most readable and efficient way to square a number in Python is to use the exponent operator (**).
number = 7 squared = number**2 print(f"Square of {number} is {squared}") # Output: Square of 7 is 49
We raised the number to the power 2 and 7^2 = 49.
It works with all numeric types, handles large exponents, and is concise.
Negative number
If the input is a negative number, the output will be positive because -a^2 = -a x -a and multiplication of negative values yields a positive value.
negative_number = -4 squared = negative_number**2 print(f"Square of {negative_number} is {squared}") # Output: Square of -4 is 16
Square of 0
The square of 0 yields 0 because 0 x 0 = 0.
zero = 0 squared = zero**2 print(f"Square of {zero} is {squared}") # Output: Square of 0 is 0
Floating-point value
If your input is a floating-point value, the precision is limited by IEEE 754 (about 15 decimal digits.
pi_approx = 3.14159 squared = pi_approx ** 2 print(f"Square of {pi_approx} is {squared}") # Output: Square of 3.14159 is 9.869587728099999
Complex numbers
The ** operator has built-in support for complex numbers, where it performs the squares of the real and imaginary parts accordingly.
complex_value = 3 + 4j squared = complex_value ** 2 print(squared) # Output: (3+4j)^2 = 9 + 24j + (4j)^2 = 9 + 24j - 16 = -7 + 24j
NaN (Not a Number) and Infinity
The square value of NaN is NaN, and infinity is infinity.
nan = float('nan') print(nan ** 2) # Output: nan inf = float('inf') print(inf ** 2) # Output: inf print((-inf) ** 2) # Output: inf (positive)
Non-Numeric Inputs
If you pass non-numeric inputs like a string, it will throw a TypeError.
try: "a" ** 2 except TypeError as e: print(e) # Output: unsupported operand type(s) for ** or pow(): 'str' and 'int'
You can either validate with the isinstance(x, (int, float, complex)) method or use the try-except mechanism to prevent the program from crashing.
Square of each element of a list
You can calculate the square of each element in a list, and you can use list comprehension.
It provides a concise and readable alternative to using a loop for applying operations.
my_list = [5, 10, 15, 20, 25] squared = [number ** 2 for number in my_list] print("Square of a list: ", squared) # Output: Square of a list: [25, 100, 225, 400, 625]
Other approaches
Using multiplication
Another straightforward way to find a square is to multiply the number by itself (number * number).
number = 7 squared = number * number print(f"Square of {number} is {squared}") # Output: Square of 7 is 49
Using math.pow()
The math.pow() function raises a number to a specified power.
It takes two arguments: the base number and the exponent, and returns a square, which is a floating-point number.
import math number = 7 squared = math.pow(number, 2) print(f"Square of {number} is {squared}") # Output: Square of 7 is 49.0
Using pow() function
The built-in pow() function is similar to the math.pow() function, but if you pass integers, it returns an integer instead of a float.
number = 7 squared = pow(number, 2) print(f"Square of {number} is {squared}") # Output: Square of 7 is 49
Using NumPy Arrays
The np.square() function calculates the square of each element in an array.
NumPy is particularly efficient when dealing with large arrays or when you need to perform additional numerical computations.
import numpy as np my_arr = np.array([5, 10, 15, 20, 25]) squared = np.square(my_arr) print("Square of the array:", squared) # Output: Square of the array: [ 25 100 225 400 625]
You can see that the output array is a square of each element of the input array.
That’s all!