Tuesday, April 5, 2022

Matrix

 Matrix in python:

1) take input matrix :


    m=[]

    r,c=map(int,input().split())

    for i in range(r):

        a=[]

        for j in range(c):

            a.append(int(input()))

        m.append(a)

    print(m)

    output:

            2 4

            

            2

            3

            4

            5

            6

            7

            8

            [[1, 2, 3, 4], [5, 6, 7, 8]]


                                    OR

           r,c=map(int,input().split())

        m=[[int(input()) for i in range(r)] for j in range(c)]

        print(m)

                                            OR

    R = int(input("Enter the number of rows:"))

    C = int(input("Enter the number of columns:"))

    matrix = []

    print("Enter the entries rowwise:")

    for i in range(R):  

a =[]

for j in range(C):  

a.append(int(input()))

matrix.append(a)

    for i in range(R):

for j in range(C):

print(matrix[i][j], end = " ")

print()


                                             OR

        

        import numpy as np

        r,c=map(int,input().split())

        l=list(map(int,input().split()))

        m=np.array(l).reshape(r,c)

        print(m)






Python modules

Python modules:

syntax:import module

math:
      import math
        print(math.sqrt(25))         #5.0
        print(math.factorial(5))    #120

    or
      from math import sqrt,factorial
        print(sqrt(25))                            #5.0
        print(factorial(5))                       #120


import all names from module use --> *
    
        from math import *
        print(sqrt(25))                #5
        print(factorial(5))            #120

Renaming the module:

        import math as m
        print(m.sqrt(36))            #6
        print(m.factorial(4))        #24

Random number:

        import random
        print(random.randint(4,55))

calendar:

        import calendar
        print (calendar.calendar(2022))