

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Matrix creation of n*n in Python
When it is required to create a matrix of dimension n * n, a list comprehension is used.
Below is a demonstration of the same −
Example
N = 4 print("The value of N is ") print(N) my_result = [list(range(1 + N * i, 1 + N * (i + 1))) for i in range(N)] print("The matrix of dimension N * 0 is :") print(my_result)
Output
The value of N is 4 The matrix of dimension N * 0 is : [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
Explanation
The value of N is predefined.
It is displayed on the console.
It tells about the dimensions of the matrix.
The number is iterated over, and is converted into a list.
This is assigned to a variable.
It is displayed on the console.
- Related Questions & Answers
- Conversion to N*N tuple matrix in Python
- Maximum number of ones in a N*N matrix with given constraints in C++
- How to print a matrix of size n*n in spiral order using C#?
- Construct an identity matrix of order n in JavaScript
- How to rotate a matrix of size n*n to 90 degree using C#?
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n in C++
- Calculate n + nn + nnn + ? + n(m times) in Python
- Print n x n spiral matrix using O(1) extra space in C Program.
- How to rotate a matrix of size n*n to 90-degree k times using C#?
- Split String of Size N in Python
- Python program to print a checkboard pattern of n*n using numpy.
- Python program to print check board pattern of n*n using numpy
- Find (1^n + 2^n + 3^n + 4^n) mod 5 in C++
- Pow(x, n) in Python
- Sum of the series 1.2.3 + 2.3.+ … + n(n+1)(n+2) in C
Advertisements