How to print square of a number in python using for loop

Several patterns can be printed using Python, once we have a strong grip over the concepts involving loops. Here, we will be using simple for loops to generate a square pattern using numbers.

Description

A square is a plane figure consisting of four sides that are identical in terms of magnitude. It has four angles, all of which are 90 degrees.

To execute this figure using Python programming, we will be using two for loops:

  • An outer loop: To loop through the number of rows.
  • An inner loop: To print the patterns along the columns.

Code

Let’s look at the below code snippet.

# No of rows

n = 4

# Loop through rows

for i in range(n):

# Loop to print pattern

for j in range(n):

print('*' , end=' ')

print()

Explanation

  • In line 2, we take the input for the number of rows (i.e., length of the square).

  • In line 5, we create a for loop to iterate through the number of rows.

  • From lines 8 and 9, an inner nested loop runs, printing * along the rows and columns, thereby generating the given pattern. The end statement helps to stay on the same line.

  • In line 10, the print() statement is used to move to the next line.

CONTRIBUTOR

Vinisha Maheshwari

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a number, the task is to write a Python program to calculate square of the given number.

    Examples:

    Input: 4
    Output: 16
    
    Input: 3
    Output: 9
    
    Input: 10
    Output: 100

    We will provide the number, and we will get the square of that number as output. We have three ways to do so:

    • Multiplying the number to get the square (N * N)
    • Using Exponent Operator
    • Using math.pow() Method

    Method 1: Multiplication

    In this approach, we will multiply the number with one another to get the square of the number.

    Example:

    Python3

    n = 4

    square = n * n

    print(square)

    Output:

    16

    Method 2: Using Exponent Operator

    In this approach, we use the exponent operator to find the square of the number.

    Exponent Operator: **

    Return: a ** b will return a raised to power b as output.

    Example:

    Python3

    n = 4

    square = n ** 2

    print(square)

    Output:

    16

    Method 3: Using pow() Method

    In this approach, we will use the pow() method to find the square of the number. This function computes x**y and returns a float value as output.

    Syntax: float pow(x,y)

    Parameters :

    x : Number whose power has to be calculated.

    y : Value raised to compute power.
     

    Return Value : 
    Returns the value x**y in float.

    Example:

    Python3

    n = 4

    square = pow(n, 2)

    print(square)

    Output:

    16.0

    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
    • Square root function in python
    • Python square roots with list comprehension
    • How to do exponents in python

    If you are new to Python check out, How to download and Install Python.

    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.

    How to print square of a number in python using for loop
    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

    How to print square of a number in python using for loop
    How to square a number in Python

    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.

    How to print square of a number in python using for loop
    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.

    How to print square of a number in python using for loop
    How to 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.

    How to print square of a number in python using for loop
    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.

    How to print square of a number in python using for loop
    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.

    How to print square of a number in python using for loop
    How to 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.

    How to print square of a number in python using for loop
    Python square number while loop

    Square root function in python

    Python math module contains many useful functions and it also includes python square root function sqrt() for calculating the square root. Firstly, we need to import math module.

    import math
    a = math.sqrt(64)
    print(a)

    After writing the above code (square root function in python), Ones you will print ” a ” then the output will appear as an “ 8.0”. Here, we will use math.sqrt() for calculating the square roots and it will return floating-point numbers as output.

    How to print square of a number in python using for loop
    Square root function in python

    Python square roots with list comprehension

    To get the square roots from a list we will use list comprehension. We will first import the math module in which the math.sqrt() function available. Then we make the list with random numbers and the loop is used to iterate through all the elements in the list.

    import math
    val = [36, 49, 42, 25, 81]
    sqr_root = [math.sqrt(num) for num in val]
    print("Their square roots are:\n", sqr_root)

    After writing the above code (python square roots with list comprehension), Ones you will print “sqr_root” then the output will appear as an “[6.0, 7.0, 6.48074069840786, 5.0, 9.0]”. Here, we will use math.sqrt(num) for calculating the square roots of the list and the sqr_root list has the square root for each number in the original value list.

    How to print square of a number in python using for loop
    Python square roots with list comprehension

    How to do exponents in python

    The double asterisk ” ** “ operator is used to calculate the exponential value. The number to be multiplied by itself is called the base and the number of times it is to be multiplied is the exponent.

    Example:

    base = 3
    exponent = 2
    print("Exponential value is: ", base ** exponent)

    After writing the above code (how to do exponents in python), Once you will print ” base ** exponent “ then the output will appear as a “ 9 ”. Here, the base is 3 and the exponent is 2 and it will return the corresponding value. You can refer to the below screenshot on how to do exponents in python.

    How to print square of a number in python using for loop
    How to do exponents in python

    You may also like the following tutorials:

    • What is a Python Dictionary + Create a dictionary in Python
    • Python print without newline
    • Python Dictionary Methods + Examples
    • 11 Python list methods
    • How to create a list in Python
    • Python String Functions
    • How to convert an integer to string in python
    • How to concatenate strings in python
    • Object oriented programming python
    • Python Anonymous Function
    • Add string to list Python

    In this tutorial, you have learned the different ways to square a number in Python and also we have seen many examples like.

    • Python square a number
    • Python square a number list
    • Python square root of a number
    • Python square number check
    • Python square number while loop
    • Square root function in python
    • Python square roots with list comprehension
    • How to do exponents in python

    How to print square of a number in python using for loop

    Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.

    How do you print a square number in a for loop in python?

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

    How do you print square numbers in python?

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

    How do you print a square of n numbers in python while loop?

    Answer.
    n = int(input("Enter nth number : ")).
    sum = 0..
    for s in range(1, n+1):.
    sum = sum + (s*s).
    print("Sum of squares is : ", sum).

    How do you print a square pattern in python while loop?

    Pattern - 9: Square Pattern using with number.
    rows = int(input("Enter the number of rows: ")).
    for i in range(1, rows + 1):.
    for j in range(1, rows + 1):.
    # Check condition if value of j is smaller or equal than..
    # the j then print i otherwise print j..
    if j <= i:.
    print(i, end=' ').