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))







Monday, March 28, 2022

programming in python

 

Intro to Programming with Python

Software

Software is a set of instructions to the hardware.

Programming

Programming means writing the instructions to create a software.

Code

The instructions that we write to create software is called Code.

Syntax

Similar to Grammar rules in English, Hindi, each programming language has a unique set of rules. These rules are called the Syntax of a Programming Language.

Why Python

Python is an easy to learn, powerful programming language. With Python, it is possible to create programs with minimal amount of code. Look at the code in Java and Python used for printing the message "Hello World"

Java:

    class Main {

        public static void main(String[] args) {

           System.out.println("Hello World");

    }

}

Python:

        print("Hello World")

Applications of Python

Python is a versatile language which has applications in almost every field

  • Artificial intelligence (AI)
  • Machine Learning (ML)
  • Big Data
  • Smart Devices/Internet of Things (IoT)
  • Cyber Security
  • Game Development
  • Backend Development, etc.

Career Opportunities

Python developers have plenty of opportunities across the world

  • DevOps Engineer
  • Software Developer
  • Data Analyst
  • Data Scientist
  • Machine Learning (ML) Engineer
  • AI Scientist, etc.

Hello World Program in Python

Here is a simple Python code that you can use to display the message "Hello World"

        print("Hello World!")        #Hello World!

Possible Mistakes

Possible mistakes you may make while writing Python code to display the message "Hello World"

  • Spelling Mistake in print
prnt("Hello World!")
PYTHON
  • Uppercase ‘P’ in Print
Print("Hello World!")
PYTHON
  • Missing quotes
print(Hello World!)
PYTHON
  • Missing parentheses
  • print("Hello World!"
    PYTHON

    Printing Without Quotes

    If we want to print the numerical result of 2 + 5, we do not add quotes.

    Code

    print(2 + 5)
    PYTHON

    Output

    7

    If we want to print the exact message "2 + 5", then we add the quotes.

    Code

    print("2 + 5")
    PYTHON

    Output

    2 + 5

    Calculations with python

    Addition

    Addition is denoted by

    +
    sign. It gives the sum of two numbers.

    Code

    print(2 + 5)
    print(1 + 1.5)
    PYTHON

    Output

    7
    2.5

    Subtraction

    Subtraction is denoted by

    -
    sign. It gives the difference between two numbers.

    Code

    print(5 - 2)
    PYTHON

    Output

    3

    Multiplication

    Multiplication is denoted by

    *
    sign.

    Code

    print(2 * 5)
    print(5 * 0.5)
    PYTHON

    Output

    10
    2.5

    Division

    Division is denoted by

    /
    sign.

    Code

    print(5 / 2)
    print(4/2)
    PYTHON

    Output

    2.5  2.0