How to print square numbers in Python using for loop

Table of Contents

  • Introduction
  • Printing numbers square using while loop
  • Printing numbers square using for loop
  • Printing even numbers square using while loop
  • Conclusion

Introduction

Programs for printing square root of number, squaring only even numbers.

Printing numbers square using while loop

In while loop if variable index is less than equal to number then print statement executed square root of that number.
In python for squaring a number added two stars (**).

number = 5;
index = 0;
while index <= number:
   print(index**2)
   index+=1

#PYTHON OUTPUT
0
1
4
9
16
25
How to print square numbers in Python using for loop

Printing numbers square using for loop

We use range() function to loop using for loop and print square root.

number = 5
for index in range(0, number+1):
    print(index**2)

#PYTHON OUTPUT
0
1
4
9
16
25
How to print square numbers in Python using for loop

Printing even numbers square using while loop

Condition (index%2)==0 checks if the given number is even.

number = 8
index = 0
while index <= number:
    if (index%2)==0:
       print(index**2)
       index+=1

 #PYTHON OUTPUT
 0
 4
 16
 36
 64
How to print square numbers in Python using for loop

Conclusion

You have reached at the end of our post on `Python printing numbers square`.we appreciate you for going through this post.
If you find anything difficult with our way of explanation please leave us a comment and if you have enjoyed reading our post then help
us grow by sharing this post link. We Thank You With All Our Heart.

Top Python Posts

  • Python Lists
  • Python String Concatenation

an even better idea is to make a simple function

def do_square(x):
    return x*x

then just run a list comprehension on it

n = int(raw_input("Enter #:")) #this is your problem with the original code
#notice we made it into an integer
squares = [do_square(i) for i in range(1,n+1)]

this is a more pythonic way to do what you are trying to do you really want to use functions to define functional blocks that are easy to digest and potentially can be reused

you can extend this concept and create a function to get input from the user and do some validation on it

def get_user_int():
    #a function to make sure the user enters an integer > 0
    while True:
         try:
            n = int(raw_input("Enter a number greater than zero:"))
         except TypeError:
            print "Error!! expecting a number!"
            continue;
         if n > 0: 
            return n
         print "Error: Expecting a number greater than zero!"

and then you can build your input right into your list

 squares = [do_square(i) for i in range(1,get_user_int()+1)]

and really do_square is such a simple function we could easily just do it in our loop

squares = [x*x for x in range(1,get_user_int())]

Python is the most preferred programming language when it comes to dealing with a large amount of data. Python possesses numerable in-built libraries and various default methods which helps you to perform multiple operations on the data effectively and quickly. Squaring a number is one such operation where you multiple the number by itself. In this article, we will study various methods to square a number in python along with the example and outputs. So, let's get started!

How to Square a Number in Python?

Squaring a number means multiplying the number by itself. Below are six methods by which you can find the square of a number in python.

1) Multiplication

The first approach that comes to your mind for finding the square of a number is to multiply the number by itself. This is the easiest and simplest method to find the square of the number in python. Check out the example below for understanding the working of this method.

For example:

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

Output:

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”.

For example:

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

Output

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. pow() is one of the methods from the math library which can help you to find the square of a number in python. 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:

For example:

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

Output

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 as shown below:

For example:

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

Output

5) Using 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 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 as shown in the below example:

For example:

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

Output:

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 as shown in the below example:

For example:

import numpy as np

arr = np.array([2,4,6,8])
print("Square Value of arr : \n", np.square(arr))

Output

 Square Value of arr :
 [4 16 36 64]

Conclusion

As python is used in many data-driven domains like machine learning and artificial intelligence, performing mathematical operations in python is not an uncommon task. Therefore, we have presented various methods which will help you to find the square of the given number explicitly with the least possible steps. It is highly recommended to learn and make use of these steps and your programming efficient and faster. For more such programming knowledge, visit our blogs at Favtutor.

How do you print a square shaped number in python?

Square shape in Python.
for row in range(5): for col in range(5): ... .
input_number = int(input("Enter the side of square:")) for row in range(input_number): ... .
for row in range(5): for col in range(5): ... .
1 1 1 1 1. 2 2 2 2 2. ... .
for i in range(5): for j in range(5): ... .
val = int(input("Enter the number of rows:")) ... .
1 2 3 4 5..

How do you print a square of each number on a different line in python?

If you want to print the value in seperate line then simple use print(square_non(n),'\n') and add '\n' which will create new line.