How to calculate squares in Python

Posted in Python by Dirk - last update: Feb 15, 2024

In mathematics, the square of a number is the result of multiplying that number by itself. It is denoted by raising the number to the power of 2. For example, the square of 4 is written as 4^2, which equals 16, because 4 multiplied by 4 is 16. In Python, you can calculate squares using the exponentiation operator ** or the pow() (or `math.pow() function).

Here are some examples:

Squares of Positive Numbers

positive_number = 5
square_result = positive_number ** 2  # Using the exponentiation operator
print(f"The square of {positive_number} is: {square_result}")
print("Type: ", type(square_result))

# Or using the pow() function
square_result_pow = pow(positive_number, 2)
print(f"The square of {positive_number} using pow() is: {square_result_pow}")
print("Type: ",type(square_result_pow))

Output:

The square of 9 is: 81
The square of 9 using pow() is: 81

Note: the result of the operation is float or integer (depending on the type of the initial value)

Sqares of complex numbers

To calculate the square of a complex number in Python, you can use the same principles. The imaginary unit is denoted by j in Python. Here’s an example:

Example

import math

complex_number = 2 + 3j
square_result_complex = complex_number ** 2
print(f"The square of {complex_number} is: {square_result_complex}")
The square of (2+3j) is: (-5+12j)

The type of square_result_complex is complex.

Using pow() to calculate powers

The pow() function in Python can be used to calculate powers other than 2. The general syntax of the pow() function is as follows:

pow(x, y, z=None)
  • x: The base number.
  • y: The exponent to which x is raised.
  • z (optional): If provided, the modulus operation is applied, and the result is congruent to x**y modulo z. This parameter is optional, and if not specified, it defaults to None.

Here are some examples of using pow() for powers other than 2:

Cubes:

number = 3
cube_result = pow(number, 3)
print(f"The cube of {number} is: {cube_result}")

# Output: The cube of 3 is: 27

Fourth Powers:

number = 2
fourth_power_result = pow(number, 4)
print(f"The fourth power of {number} is: {fourth_power_result}")

# Output: The fourth power of 2 is: 16

Using Modulus:

base = 5
exponent = 3
modulus = 4
result_with_modulus = pow(base, exponent, modulus)
print(f"{base} to the power of {exponent} modulo {modulus} is: {result_with_modulus}")

# Output: 5 to the power of 3 modulo 4 is: 1

In this example, 5**3 is 125, and when you take the modulus 4, the result is 1 because 125 divided by 4 leaves a remainder of 1. The pow() function allows you to calculate the result of a power operation with a modulus, which can be useful in various mathematical and programming scenarios, such as cryptography and number theory.

pow() vs math.pow()

The pow() function and math.pow() function in Python are similar in purpose, but they have some differences in terms of usage and behavior.

  • Built-in vs import: pow() is a built-in function in Python while math.pow() is part of the math module, so you need to import the math module before using it.
  • Type of result: math.pow() always returns a floating-point result, regardless of the types of the arguments, while the result type of pow() depends on these types
  • Number of arguments: math.pow() only takes two arguments and calculates x**y, while pow can take three (x,y and z) and calculate the modulus (x**y) % z

Example using math.pow():

import math

# Only two arguments - always returns a float
result3 = math.pow(2, 3)
print(result3)  # Output: 8.0

Other articles