
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 Articles
- Conversion to N*N tuple matrix in Python
- Maximum number of ones in a N*N matrix with given constraints in C++
- Print Matrix in spiral way\n
- How to print a matrix of size n*n in spiral order using C#?
- Construct an identity matrix of order n in JavaScript
- Raise a square matrix to the power n in Linear Algebra in Python
- How to rotate a matrix of size n*n to 90 degree using C#?
- How to match nonword characters in Python using Regular Expression?\n\n\n\n
- Print n x n spiral matrix using O(1) extra space in C Program.
- Creation of virtual environments using Python
- How to rotate a matrix of size n*n to 90-degree k times using C#?
- Calculate n + nn + nnn + ? + n(m times) in Python
- How regular expression anchors work in Python?\n\n
- What does setattr() function do in Python?\n\n
- What does delattr() function do in Python?\n\n

Advertisements