
- 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
Initialize Matrix in Python
In this article, we will learn about how we can initialize matrix using two dimensional list in Python 3.x. Or earlier.
Let’s see the intuitive way to initialize a matrix that only python language offers. Here we take advantage of List comprehension. we initialise the inner list and then extend to multiple rows using the list comprehension.
Example
# input the number of rows N = 3 # input the number of columns M = 3 # initializing the matrix res = [ [ i*j for i in range(N) ] for j in range(M) ] # printing the matrix on screen row by row in a single line print("Inline representation:") [ [ print(res[i][j] ,end =" ") for i in range(N) ] for j in range(M) ] print("") # printing in multiple lines print("Multiline representation") for i in range(N): for j in range(M): print(res[i][j] ,end =" ") print("")
Output
Inline representation: 0 0 0 0 1 2 0 2 4 Multiline representation 0 0 0 0 1 2 0 2 4
Now let's see the general way which can be implemented in any language. This is the standard way of creating a matrix or multidimensional-array
Example
# input the number of rows N = 3 # input the number of columns M = 3 lis=[[0,0,0],[0,0,0],[0,0,0]] # initializing the matrix for i in range(N): for j in range(M): lis[i][j]=i # multiline representation for i in range(N): for j in range(M): print(lis[i][j],end=" ") print("")
Output
0 0 0 0 1 2 0 2 4
Conclusion
In this article, we learned how to implement logic gates in Python 3.x. Or earlier. We also learned about two universal gates i.e. NAND and NOR gates.
- Related Articles
- Initialize tuples with parameters in Python
- Initialize a window as maximized in Tkinter Python
- Different ways to initialize list with alphabets in python
- Python - Ways to initialize list with alphabets
- Python - Which is faster to initialize lists?
- Matrix manipulation in Python
- Rotate Matrix in Python
- Initialize HashSet in Java
- Initialize HashMap in Java
- Transpose a matrix in Python?
- Spiral Matrix II in Python
- Set Matrix Zeroes in Python
- Custom length Matrix in Python
- Vertical Concatenation in Matrix in Python
- Python – Convert Integer Matrix to String Matrix

Advertisements