Square in python math

How to square numbers in Python?

In this short tutorial, we look at how we can use Python to square a number. We look at all the various methods and break them down for you.

Table of Contents- Python Square

Squaring numbers in Python:

The square of a number is the result of multiplying the number by itself. In Python, we deal with a lot of square numbers — Data, Financial Analysis use them the most. Because of this high usage Python provides three methods to square a number: they are:

The last method can also be used with the math module which has a lot of other handy mathematical functions as well.

Python Square using **:

The Python exponent operator ** is used to perform an exponential calculation. It takes in 2 real numbers. The syntax is as follows.

Here “N” is the number and “power” is the number of times you would like to exponent the number. Since we are looking to square the number, we can pass 2 as the power. Similarly, If we were looking to cube the value we would pass 3.

Читайте также:  Как ловить исключения python

Code and Explanation:

n = 2 n2 = 3 print(n**2) print(n2**2) 

The above code snippet returns the following values which are the square of the entered values. This is how you use exponents to calculate the square of a number.

Squaring using the pow() method:

The pow() method is another method in Python that can be used to square a number. This method also takes in two arguments in the following syntax.

This method is equivalent to the exponent methods and you can choose which method you would like to use. The pow() method supports a third argument which I would recommend reading about once you have practiced using it with two arguments.

Code and Explanation:

n = 2 n2 = 3 print(pow(2,2)) print(pow(3,2)) 

Similar to the method above, the square values of the argument are returned. This is how you use the pow() method to calculate the square of a number.

As mentioned above the pow() method can also be used in the math module, however, it needs to be imported before it is used. Hence I would not recommend using it unless you are intending to use other methods from the module as well.

Multiplying the number by itself:

This is the most fundamental method of calculating the square of a number. Here we square the number by multiplying the number by itself.

Code and Explanation:

n = 2 n2 = 3 print(n*n) print(n2*n2) 

The above code returns the square of the number.

Closing thoughts — Python Square

All the method methods above can be used to calculate the Python square of a number. Although 3 works well, I personally prefer using methods 1 or 2.

Once you are done with the tutorial I would recommend that you check out the Python square root method that is used to find the square root of a number.

Источник

How to Square a Number in Python? 6 Ways (with code)

How to Square a Number in Python? 6 Ways (with code)

Python is the most preferred programming language when it comes to dealing with a large amount of data. It possesses many in-built libraries which help you to perform multiple operations such as squaring a number. In this article, we will learn about all the methods to Square a Number in Python with various use cases. So, let’s get started!

6 Ways to Square a Number in Python

Before moving to the techniques, let’s first revise what we are going to do. Squaring a number means multiplying the number by itself. Let’s say you have a number x, then its square is x².

Below are 6 methods by which you can find the square of a number:

1) Multiplication

The simplest approach to finding the square of a number in Python is to multiply the number by itself. This is the easiest way out of all of them where we use the multiplication operator ‘*’.

def number(a): return a*a print(number(5))

2) Using an Exponent Operator

You can also find the square of a given number using the exponent operator in Python. It is represented by «**». While applying this method, the exponent operator returns the exponential power resulting in the square of the number. Note that the statement “a**b” will be defined as “a to the power of b”.

n = 5 result = n ** 2 print(result)

3) Using the pow() method

Python possesses an in-built library named “math” which helps you to perform all types of math operators on given data. The pow() is one of the methods from the math library which can help you to find the square of a number. You can also use the pow() method to find other exponential power of a given number.

To use this method, we have to first import the math library using the “import” keyword. Later, the pow() method intakes a two-parameter, where the first parameter is the number and the second parameter suggests the exponential power to the number.

In our case, the second parameter will be “2” as we want to find the square of the number. Take a look at the below example for a better understanding of the pow() method:

n = 5 result = pow(n, 2) print(result)

4) Square a List of the Number

The list is one of the data structures in Python which helps to store multiple elements under a single variable. When the list possesses an integer value, you can find the square of each number inside the list by multiplying by itself using the for loop.

sample_list = [2,4,6,8] result = [number ** 2 for number in sample_list] print(result)

5) Using a while loop

One of the least used methods to find the square of a number in Python is by making use of a while loop. While the loop repeats the block of code until the given condition gets false. Therefore, we will find the square of the number using the while loop till the condition becomes false.

n = 1 while n  5: print (n, '\t', n ** 2) n += 1 

6) Square of Array

To find the square of the array containing the integer values, the easiest way is to make use of the NumPy library. Numpy is an in-built python library that helps to perform all kinds of numerical operations on data with simple and efficient steps.

The NumPy square method will help you to calculate the square of each element in the array and provide you with the final output. To make use of the NumPy library, you have to import it using the “import” keyword.

import numpy as np arr = np.array([2,4,6,8]) print("Square Value of arr : \n", np.square(arr))
 Square Value of arr : [4 16 36 64] 

Program to Input a Number from the User

This is one of the beginner-level Python problems that any programmer solves. In the below program, an integer is given by the user as the input, and the square of that integer is printed as the output. Here we have used the multiplication method to find the square. Check the code below:

n = int(input("Enter the integer to square: ")) output =n*n print(output)
Enter the integer to square: 45 2025

Program for Square of N numbers

Sometimes, we need a list of squared numbers. First, the ‘n’ number is defined by the user. The following Python program prints the squares of all the numbers till n. Here we used the exponent operator with the for loop.

n = int(input("Enter the number of integers to square: ")) for i in range(1, n+1): square = i ** 2 print(square)
Enter the number of integers to square: 3 1 4 9

Program to Square Numbers in a Range

After we know how to print squares of ‘n’ numbers, we can also use this for a range of numbers and print them. In this case, a range of numbers is defined by the user.

n = int(input("Enter the lower range to square: ")) m = int(input("Enter the upper range to square: ")) for i in range(n, m+1): square = i ** 2 print(square)
Enter the lower range to square: 4 Enter the upper range to square: 8 16 25 36 49 64

Conclusion

We have presented various methods which will help you to find the square of the number in Python. It is highly recommended to understand all of them to make your programming efficient. You can now learn how to round down numbers in Python as well. Happy Learning 🙂

Источник

Оцените статью