Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Square a Number in Python

  • 02 Sep, 2025
  • Com 0
Square of a Number in Python

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 (**).

Finding a square of a number using ** operator in Python

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

Square of a 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.

Square of a 0 in Python

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.

Square of Floating-point value

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 (Not a Number) and 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.

Square of each element in Python list

 

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).

Squaring a number in Python

 

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.

Using math.pow() method

 

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.

Using pow() function

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.

Calculating square of numpy array

 

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!

Post Views: 7
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Remove Key from Dictionary in Python
Python max() Function

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend