Python Exercise: N x N square consisting only of the integer N
Python Basic - 1: Exercise-149 with Solution
Write a Python program that takes a positive integer and creates an N x N square filled with the integer N. Display the N x N square.
Sample Data:
(2) -> [[2, 2], [2, 2]]
(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]]
(-6) -> []
Sample Solution-1:
Python Code:
def test(N):
result = []
for i in range(N):
result.append([N]*N)
return result
N = int(input("Input an integer : "))
print(test(N))
Sample Output:
Input an integer : 4 [[4, 4, 4, 4], [4, 4, 4, 4], [4, 4, 4, 4], [4, 4, 4, 4]] Input an integer : -4 []
Flowchart:

Sample Solution-2:
Python Code:
def test(N):
result = [[N]*N]*N
return result
N = int(input("Input an integer : "))
print(test(N))
Sample Output:
Input an integer : 4 [[4, 4, 4, 4], [4, 4, 4, 4], [4, 4, 4, 4], [4, 4, 4, 4]]
Flowchart:

Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Check the numbers that are higher than the previous
Next: Iterated Cube Root.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join