Write a Python Program to Print Square Number Pattern using While Loop and For Loop with an example.
Python Program to Print Square Number Pattern using For Loop
This Python program allows users to enter any side of a square. This side decides the number of rows and columns of a square. Next, this program use For Loop to print 1’s until it reaches to the user-specified rows, and columns.
side = int(input("Please Enter any Side of a Square : "))
print("Square Number Pattern")
for i in range(side):
for i in range(side):
print('1', end = ' ')
print()

If you want to print 0, please replace 1 in print statement with 0.
side = int(input("Please Enter any Side of a Square : "))
print("Square Number Pattern")
for i in range(side):
for i in range(side):
print('0', end = ' ')
print()
Please Enter any Side of a Square : 10
Square Number Pattern
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
>>>
Python Program to return Square Number Pattern using While Loop
In this square number pattern example, we just replaced the Python nested For Loop with a Python while statement. For more, please. refer to the Python tutorial page.
side = int(input("Please Enter any Side of a Square : "))
i = 0
print("Square Number Pattern")
while(i < side):
j = 0
while(j < side):
j = j + 1
print('5', end = ' ')
i = i + 1
print()
Please Enter any Side of a Square : 10
Square Number Pattern
5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5
5 5 5 5 5 5 5 5 5 5
>>>
Python Program to Display Square Number Pattern
This Python program asks the user to enter any number. Next, it prints that number in a square pattern. For more patterns, please refer to the Python Number pattern programs.
side = int(input("Please Enter any Side of a Square : "))
number = int(input("Please Enter any Number : "))
print("Square Number Pattern")
for i in range(side):
for i in range(side):
print(number, end = ' ')
print()
Please Enter any Side of a Square : 12
Please Enter any Number : 9
Square Number Pattern
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9 9 9
>>>
Also Read
- Python Program to Print Downward Triangle Mirrored Numbers
- Python Program to Print Same Numbers on all Sides of a Square