Python

Square a number in Python

The square value of a number can be calculated in Python by multiplying the number by itself or using the power operator (**) or pow() function. Different ways to calculate the square of a number in Python have been shown in this tutorial.

Calculate the square of a number:

Different ways to calculate the square of a number have been shown in this part of the tutorial.

Example-1: Calculate the square of a number by multiplication

Create a python file with the following script to calculate the square of a number by multiplying the number by itself. A number value will be taken from the user, and the isdigit() function will check the input value is valid or invalid. If the input value is valid, then the square of the value will be calculated and printed later; otherwise, an error message will be displayed.

# Take a number value from the user

number = input("Enter a number: ")

# Check the input value is number or not

if number.isdigit() == True:

# Convert the string into integer

number = int(number)

# Calculate the square value

sqr_val = number * number

else:

# Print the error message

print("you have to enter a number.")

# Terminate from the script

exit(0)

# Print the square value of the number

print("The square of {} is {}.".format(number, sqr_val))

Output:

The following output will appear after executing the above script for the valid input value, 5.

After executing the above script for the invalid input value, one, the following output will appear.

Example-2: Calculate the square of a number by power operator

Create a python file with the following script to calculate the square of a number by using a power operator. The power operator is defined by ‘**’. The power value will be 2 to calculate the square of a number. A number value will be taken from the user, and the try-except block is used to handle the error of the script. If the input value is valid, then the square of the value will be calculated and printed; otherwise, an error message will be displayed.

try:
# Take a number value from the user

number = int(input("Enter a number: "))

# Calculate the square value

sqr_val = number ** 2

# Print the square value of the number

print("The square of {} is {}.".format(number, sqr_val))

except:

# Print the error message

print("you have to enter a number.")

Output:

The following output will appear after executing the above script for the valid input value, 7.

After executing the above script for the invalid input value test, the following output will appear.

Example-3: Calculate the square of a number by using the pow() function

Create a python file with the following script to calculate the square of a number by using the pow() function. The pow() function takes two arguments. One is the base value, and another is the power value. The power value will be 2 to calculate the square of a number. A number value will be taken from the user, and the try-except block is used to handle the error of the script like the previous example.

try:
# Take a number value from the user

number = int(input("Enter a number: "))

# Calculate the square value

sqr_val = pow(number, 2)

# Print the square value of the number

print("The square of %d is %d." % (number, sqr_val))

except:

# Print the error message

print("you have to enter a number.")

Output:

The following output will appear after executing the above script for the valid input value, 3.

After executing the above script for the invalid input value, four of the following output will appear.

Conclusion:

Three different ways to calculate the square of a number with error handling have been shown in this tutorial by using simple python examples. Python users can use any of the ways mentioned in this tutorial to calculate the square of a number.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.