# How To Square in Python

In this article, we will discuss **how to square in Python**. **Squaring in Python** can be done in 5 ways. We can square a number in python by using the Exponentiation operator, pow(), multiply number by itself, Numpy square(), and Numpy power() method.

There are 5 ways to square a number in Python

- Python Exponentiation Operator ( ** ) is used to raise the number to the power of an exponent. To get the square we use power as 2. For example – 5 ** 2 = 25, here 5 is raised to the 2nd power.
- Python in-built pow() function. To get square we can use pow(number, 2).
- Multiply by itself. We can get the square of the number by multiplying itself. Example – n * n.
- The Python Numpy square() function returns the square of the number given as input. Example – numpy.square(5) = 25
- To get square we use the Numpy package power(). Example numpy.power(4, 2) = 16.

From above you can choose any method to square a number in Python. Each method is discussed in detail below with examples.

### Squaring in Python using Exponentiation Operator ( ** )

Python provides arithmetic operators. The exponentiation operator is one of the arithmetic operators provided by python. It is denoted by ****** ( 2 asterisks ). It returns the first raised to the power of an exponent.

Syntax:

Number ** exponent

To get a square of a number we take exponent value as 2.

**Python Examples**

```
# Defining some numbers to square
number1 = 5
number2 = 12
number3 = 14
number4 = 3
number5 = 10
# squaring using exponentiation operator
square1 = number1 ** 2
square2 = number2 ** 2
square3 = number3 ** 2
square4 = number4 ** 2
square5 = number5 ** 2
# print square of numbers in Python
print("Square of {0} is {1}".format(number1, square1))
print("Square of {0} is {1}".format(number2, square2))
print("Square of {0} is {1}".format(number3, square3))
print("Square of {0} is {1}".format(number4, square4))
print("Square of {0} is {1}".format(number5, square5))
```

**Output: **

Square of 5 is 25

Square of 12 is 144

Square of 14 is 196

Square of 3 is 9

Square of 10 is 100

### Using pow() to square a number in Python

Python provides an in-built pow() function that can be used to get the square of a number in Python. The python pow() function returns the value of x to the power of y where x is the number and y is the exponent.

Syntax:

power(number, exponent)

To get a square of a number we take exponent value as 2.

**Python Examples**

```
# Defining some numbers to square
number1 = 5
number2 = 12
number3 = 14
number4 = 3
number5 = 10
# squaring using exponentiation operator
square1 = pow(number1, 2)
square2 = pow(number2, 2)
square3 = pow(number3, 2)
square4 = pow(number4, 2)
square5 = pow(number5, 2)
# print square of numbers in Python
print("Square of {0} is {1}".format(number1, square1))
print("Square of {0} is {1}".format(number2, square2))
print("Square of {0} is {1}".format(number3, square3))
print("Square of {0} is {1}".format(number4, square4))
print("Square of {0} is {1}".format(number5, square5))
```

**Output: **

Square of 5 is 25

Square of 12 is 144

Square of 14 is 196

Square of 3 is 9

Square of 10 is 100

### Multiply by itself to get square of number

We can square a number by multiplying itself. To multiply a number in python we use **asterisk character** i.e *****. Hence for squaring in python we can perform number * number

**Python Examples**

```
# Defining some numbers to square
number1 = 5
number2 = 12
number3 = 14
number4 = 3
number5 = 10
# squaring using exponentiation operator
square1 = number1 * number1
square2 = number2 * number2
square3 = number3 * number3
square4 = number4 * number4
square5 = number5 * number5
# print square of numbers in Python
print("Square of {0} is {1}".format(number1, square1))
print("Square of {0} is {1}".format(number2, square2))
print("Square of {0} is {1}".format(number3, square3))
print("Square of {0} is {1}".format(number4, square4))
print("Square of {0} is {1}".format(number5, square5))
```

**Output: **

Square of 5 is 25

Square of 12 is 144

Square of 14 is 196

Square of 3 is 9

Square of 10 is 100

**Also read: **3 Ways to convert List to Set in Python

### Squaring in Python Numpy square()

Numpy is a library in Python, which is used for scientific computing. To know about Numpy click here.

We can use the Numpy square() function to get the square of a number in python.

Syntax-

numpy.square(number)

**Python Examples**

```
# importing numpy
import numpy
# Defining some numbers to square
number1 = 5
number2 = 12
number3 = 14
number4 = 3
number5 = 10
# squaring using exponentiation operator
square1 = numpy.square(number1)
square2 = numpy.square(number2)
square3 = numpy.square(number3)
square4 = numpy.square(number4)
square5 = numpy.square(number5)
# Print Square number in Python
print("Square of {0} is {1}".format(number1, square1))
print("Square of {0} is {1}".format(number2, square2))
print("Square of {0} is {1}".format(number3, square3))
print("Square of {0} is {1}".format(number4, square4))
print("Square of {0} is {1}".format(number5, square5))
```

**Output: **

Square of 5 is 25

Square of 12 is 144

Square of 14 is 196

Square of 3 is 9

Square of 10 is 100

### Squaring in Python Numpy power()

We can use the Numpy power() function to get the square of a number in python.

Syntax-

numpy.power(number, 2)

**Python Examples**

```
# importing numpy
import numpy
# Defining some numbers to square
number1 = 5
number2 = 12
number3 = 14
number4 = 3
number5 = 10
# squaring using exponentiation operator
square1 = numpy.power(number1, 2)
square2 = numpy.power(number2, 2)
square3 = numpy.power(number3, 2)
square4 = numpy.power(number4, 2)
square5 = numpy.power(number5, 2)
# Print Square number in Python
print("Square of {0} is {1}".format(number1, square1))
print("Square of {0} is {1}".format(number2, square2))
print("Square of {0} is {1}".format(number3, square3))
print("Square of {0} is {1}".format(number4, square4))
print("Square of {0} is {1}".format(number5, square5))
```

**Output: **

Square of 5 is 25

Square of 12 is 144

Square of 14 is 196

Square of 3 is 9

Square of 10 is 100

**Also read: **14 Application of Python

## How to Square numbers in a list in Python?

Below we will discuss how to square numbers in list in Python.

Input List - [2, 5, 12, 14, 3, 10]

Output List - [4, 25, 144, 196, 9, 100]

In Python, the above problem statement can be solved in various ways. We will be solving this problem in 3 ways.

### Method 1- Using for loop

We can get the Square of the number in a list in Python using a for loop by following the steps given below:-

- Define the input with numbers to square
- Define an empty list to store square of numbers
- Use for loop to iterate on each number present in input list
- Square the number and store it in the output list
- Print the output list

**Python Program:**

```
# input list
input_list = [2, 5, 12, 14, 3, 10]
# defining output string
square_list = []
# using for loop to iterate on input list
# And appending the square of number in output string
for i in input_list:
square_list.append(i ** 2)
# Printing the output string
print(square_list)
# Print square of number python
for i in range(len(input_list)):
print("Square of {0} is {1}".format(input_list[i], square_list[i]))
```

**Output: **

[4, 25, 144, 196, 9, 100]

Square of 2 is 4

Square of 5 is 25

Square of 12 is 144

Square of 14 is 196

Square of 3 is 9

Square of 10 is 100

### Method 2- Using list comprehension

In this method, to get the square of all elements in a list we will be using list comprehension. You can know about list comprehension from here.

**Python Program**

```
# input list
input_list = [2, 5, 12, 14, 3, 10]
# Using list comprehension
square_list = [ number**2 for number in input_list ]
# printing square list
print(square_list)
# print number and its square
for i in range(len(input_list)):
print("Square of {0} is {1}".format(input_list[i], square_list[i]))
```

**Output: **

[4, 25, 144, 196, 9, 100]

Square of 2 is 4

Square of 5 is 25

Square of 12 is 144

Square of 14 is 196

Square of 3 is 9

Square of 10 is 100

### Method 3- Using lambda function to square elements in list

Lambda function is also called as Anonymous function, it is similar to the regular function in python, but it can be defined without the name. To know more about the lambda function click here.

**Python Program**

```
# input list
input_list = [2, 5, 12, 14, 3, 10]
# Using lambda function
square_list = list(map(lambda x: x ** 2, input_list))
# Printing square list
print(square_list)
# print number and its square
for i in range(len(input_list)):
print("Square of {0} is {1}".format(input_list[i], square_list[i]))
```

**Output: **

[4, 25, 144, 196, 9, 100]

Square of 2 is 4

Square of 5 is 25

Square of 12 is 144

Square of 14 is 196

Square of 3 is 9

Square of 10 is 100

## How to square numbers in a range in Python

Below we will discuss the square of numbers in the given range.

Input Range - start = 1, end = 10

Output - 1,4,9,16,25,36,49,64,81,100

To get the square of the number in the given range we can use for loop with **range() function**.

The range() function returns the sequence from the start number and stops before the end number.

Syntax

range(start_number, end_number, jump)

Here jump default value is 1.

**Python Program**

```
# define range
start = 1
end = 10
# Using range function with for loop
# and squaring in python
print("Square of the numbers the range from {0} and {1} are:".format(start, end))
for num in range(start, end):
print(num ** 2)
```

**Output: **

Square of the numbers the range from 1 and 10 are:

1

4

9

16

25

36

49

64

81

## Conclusion

We can perform squaring in python by using exponentiation operator (**), pow(), multiply number by itself, Numpy square(), and NumPy power().

##### About Author

I am Passionate Computer Engineer. Writing articles about programming problems and concepts allows me to follow my passion for programming and helping others.