
image credit:-mathonlymath.com
In Python, a matrix is a multi-dimensional array that is used to store and manipulate numerical values. Matrices are widely used in various mathematical and scientific applications, including linear algebra, computer graphics, and image processing. Matrices can be represented as two-dimensional arrays in Python, where each element is represented by a row and a column. Matrices can be easily created, manipulated, and transformed using various built-in functions and libraries in Python, making it an essential tool for data analysis and scientific computing. In Python, a matrix can be represented as a 2-dimensional list. A matrix in Python is a data structure that represents a table of elements, where each element can be a number, string, or any other object. It is a 2-dimensional arrangement of rows and columns, similar to a spreadsheet or a table in a database. A matrix can be represented in Python using a list of lists. For example:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix)
Output:-
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
The Main Python program to find Matrix of either 2*2 format or 3*3 format with using if-else statement given below :-
t=input("Which matrix you want :-\na.2*2\nb.3*3:-\n")
if t=='a':
for i in range(11,34,+1):
'''since matrix=[a11 a12 a13],
[a21 a22 a23],
[a31 a32 a33]'''
k=(i)
if(k==11):
a11=int(input("Enter a11:-"))
elif(k==12):
a12=int(input("Enter a12:-"))
elif(k==21):
a21=int(input("Enter a21:-"))
elif(k==22):
a22=int(input("Enter a22:-"))
else:
continue
m=(f"Matrix is:-\n{(a11)} {(a12)}\n{(a21)} {(a22)}\nProgram Finished!!")
print(m)
elif t=='b':
for i in range(11,34,+1):
k=(i)
if(k==11):
a11=int(input("Enter a11:-"))
elif(k==12):
a12=int(input("Enter a12:-"))
elif(k==13):
a13=int(input("Enter a13:-"))
elif(k==21):
a21=int(input("Enter a21:-"))
elif(k==22):
a22=int(input("Enter a22:-"))
elif(k==23):
a23=int(input("Enter a23:-"))
elif(k==31):
a31=int(input("Enter a31:-"))
elif(k==32):
a32=int(input("Enter a32:-"))
elif(k==33):
a33=int(input("Enter a33:-"))
else:
continue
m=(f"Matrix is:-\n{(a11)} {(a12)} {(a13)}\n{(a21)} {(a22)} {(a23)}\n{(a31)} {(a32)} {(a33)}\nProgram Finished!!")
print(m)
else:
print("Enter between a and b ")
OUTPUT:-
Which matrix you want :-
a.2*2
b.3*3:-
a
Enter a11:-5
Enter a12:-6
Enter a21:-9
Enter a22:-8
Matrix is:-
5 6
9 8
Program Finished!!