# Python square a number

By: On:

In this python tutorial, you will learn about how to square a number in python, which is used to multiply two numbers, and also we will see different ways.

• Python square a number
• Python square a number list
• Python square root of a number
• Python square number check
• Python square number while loop

If you are new to Python check out the below python tutorials:

Contents

## Python square a number

Now, we will see how to square a number in Python. In python, we can define a function that can return a square of the number.

Example:

``````def number(x):
return x*x
print(number(4))``````

After writing the above code (python square a number), Ones you will print ” number(4) “ then the output will appear as a “ 16 ”. Here, def is used to define a function and “x” is the number which will multiply. You can refer to the below screenshot for python square a number.

We can also square a number by using exponent operator ” ** “ and it will multiply the number in easy way.

Example:

``````a = 5**2
print(a)``````

After writing the above code (python square a number), Ones you will print ” a “ then the output will appear as a “ 25 ”. You can refer to the below screenshot for python square a number

## Python square a number list

To square a number list in python, it will square each number in the list and it will multiply each number by itself.

Example:

``````my_list = [1, 3, 5, 7]
for v in my_list:
print (v**2)``````

After writing the above code (python square a number list), Ones you will print ” v**2 “ then the output will appear as a “ 1 9 25 49 ”. Here, the list will iterate and it will be multiplied by 2. You can refer to the below screenshot for python square a number list.

We can also square a number list by using list comprehension to square each number in the list.

Example:

``````list1 = [1, 3, 5, 7]
value = [number ** 2 for number in list1]
print(value)``````

After writing the above code (python square a number list), Ones you will print ” value “ then the output will appear as a “ [1 9 25 49] ”. Here, the list number will be multiplied by 2. You can refer to the below screenshot for python square a number list.

## Python square root of a number

In python, we can calculate the square root of a number in python, by using the exponent operators.

Example:

``````number = 9
number_square = number ** 0.5
print('Square root of %0.3f is %0.3f'%(number ,number_square))``````

After writing the above code (python square root of a number), Ones you will print “( number, number_square)” then the output will appear as a “ Square root of 9.000 is 3.000 ”. Here, we store the number and the exponent operator will find the square root. You can refer to the below screenshot for the python square root of a number.

## Python square number check

In Python, to check whether the number is a perfect square or not, use the power operator ” ** “ with exponent as 0.5 and also modulus operator % “ to get the remainder.

It will check and return the output as true or false respectively.

Example:

``````number1 = 9
squareroot = number1 ** 0.5
remainder = squareroot % 1
result = remainder == 0
print(result)``````

After writing the above code (python square number check), Ones you will print ” result “ then the output will appear as a “ True ”. Here, 9 is the perfect square and the remainder is 0 because it is the whole number. You can refer to the below screenshot for the python square number check.

We will also see when the number is not a perfect square and the remainder is not equal to 0. Then it will give false as an output.

Example:

``````number1 = 8
squareroot = number1 ** 0.5
remainder = squareroot % 1
result = remainder == 0
print(result)``````

Here, 8 is not the perfect square and the remainder is not equal to 0. So, the output is ” False ” as it is not the perfect square. You can refer to the below screenshot for the python square number check.

## Python square number while loop

In python, while loop repeats the sequence many times until the condition gets false. Here, the square of a number using while loop works till the condition gets false and it will return the output as a square of the number.

Example:

``````number1 = 1
while number1 <= 4:
print (number1, '\t', number1 ** 2)
number1 += 1``````

After writing the above code (python square number while loop), Ones you will print ” (number1 ** 2)” then the output will appear as a “ 1 4 9 16 ” which is square of the number. Here, the variable number1 inside the loop iterates from 1 to 4. You can refer to the below screenshot for python square number while loop.