matrix python code example

Example 1: numpy matrix in python 3

np.matrix([[1, 2], [3, 4]])

# matrix([[1, 2],
#         [3, 4]])

Example 2: how to print a matrix in python

import numpy as np
print(np.matrix(A))

Example 3: matrix python math

>>> x = np.array( ((2,3), (3, 5)) )
>>> y = np.matrix( ((1,2), (5, -1)) )
>>> np.dot(x,y)
matrix([[17,  1],
        [28,  1]])

Example 4: float python

#Float or int like this - 2.6, 5.7, 1.89 etc

a = 90.7
print(a, 'is a float')

Example 5: matrix using python

# A basic code for matrix input from user
# numpy is not used
R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))
  
# Initialize matrix
matrix = []
print("Enter the entries rowwise:")
  
# For user input
for i in range(R):          # A for loop for row entries
    a =[]
    for j in range(C):      # A for loop for column entries
         a.append(int(input()))
    matrix.append(a)
  
# For printing the matrix
for i in range(R):
    for j in range(C):
        print(matrix[i][j], end = " ")
    print()

Tags:

Html Example