PYTHON BASICS OF BEGINNER's (PART-2)

MATH FUNCTION:

1. We have a math module in python that will help us to access different type of math functions.

   To access and try all the math function check out this link: https://docs.python.org/3/library/math.html

 

import math

 

x=2.9

print(round(x))

print(abs(-2.9))

print(math.ceil(2.9))

print(math.floor(2.9))

print(math.remainder(4,3))

 

3

2.9

3

2

1.0

 

NESTED LOOPS:


1.

for x in range(4):

    for y in range(3):

        print("The number is {} {}".format(x,y))

        print(f'Our co-ordinate is ({x},{y})')

 

The number is 0 0

Our co-ordinate is (0,0)

The number is 0 1

Our co-ordinate is (0,1)

The number is 0 2

Our co-ordinate is (0,2)

The number is 1 0

Our co-ordinate is (1,0)

The number is 1 1

Our co-ordinate is (1,1)

The number is 1 2

Our co-ordinate is (1,2)

The number is 2 0

Our co-ordinate is (2,0)

The number is 2 1

Our co-ordinate is (2,1)

The number is 2 2

Our co-ordinate is (2,2)

The number is 3 0

Our co-ordinate is (3,0)

The number is 3 1

Our co-ordinate is (3,1)

The number is 3 2

Our co-ordinate is (3,2)


2. Program to print F using x.


number=[5,2,5,2,2]

for x in number:

    output=''

    for y in range(x):

        output+='x'

    print(output)

 

xxxxx

xx

xxxxx

xx

xx

 

2-D LIST:

 

1. How to input data in list.

 

list = []

 n = int(input("Enter number of elements : "))

for i in range(n):

    element = int(input())

    list.append(element)

     

print(list)


Enter number of elements : 6

1

2

3

4

5

6

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

 

2. How to iterate each element in 2-d list

 

matrix=[[1,2,3],

              [4,5,6],

              [7,8,9]]

 for x in matrix:

    print(x)

for x in matrix:

    for y in x:

        print(y)

 

[1, 2, 3]

[4, 5, 6]

[7, 8, 9]

1

2

3

4

5

6

7

8

9

 

TUPLES:

Tuples are similar to list but its im-mutable that means we cannot change them.

We can only get the informations about the tuples we cannot apply various operations on it like append, pop, remove etc. .

 

1.

number=(1,2,3,4,5)

number

 

(1, 2, 3, 4, 5)

 

2.

number=(1,2,3,4,5)

print(number[2])

print(number[3])

 

3

4

 

3. Unpacking: It means setting the values of tuples in a certain variable.

x,y,z = coordinates , as soon as the python interpreter see this line it will will get the first item in the tuple(coordiantes) and assign it to the first variable then it will get the second item in the tuple and assign it to the second variable and so on.

 

coordinates=(1,2,3)

x,y,z = coordinates  

print(x)

print(y)

print(z)

 

1

2

3


EXCEPTION:

Exceptions are used to run the program smoothly if an error occurs in the code in between.

Enter the error type of in the except block ,it will help the program to complete properly rather than showing error.

 

1. ValueError


age=int(input('Age:'))

print(age)

 

Age:age

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-3-a951aaeb6da1> in <module>

----> 1 age=int(input('Age:'))

      2 print(age)

 

ValueError: invalid literal for int() with base 10: 'age'

 

try:

    age=int(input('Age:'))

    print(age)

 

except ValueError:

    print('Invalid value')

 

Age:ade

Invalid value

 

2. ZeroDivisionError


age=int(input('Age:'))

income = 2000

risk = income/age

print(risk)

 

Age:0

---------------------------------------------------------------------------

ZeroDivisionError                         Traceback (most recent call last)

<ipython-input-5-cf2041b62496> in <module>

      1 age=int(input('Age:'))

      2 income = 2000

----> 3 risk = income/age

      4 print(risk)

 

ZeroDivisionError: division by zero

  

try:

    age=int(input('Age:'))

    income = 2000

    risk = income/age

    print(age)

 

except ZeroDivisionError:

    print("age cannot be zero")

 

Age:0

age cannot be zero

 

CLASSES:

We use classes to define new types of methods that we create inside a class and that can be called via class objects.

Python is an object oriented programming language. Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor, or a "blueprint" for creating objects.

Object is an instance of a class a class simply defines what operations you have to perform.

Each object has an different instance of class.(no two objects can have same name)

self is a refrence to the current object. Every method in our class should have self parameter .We can access the attributes and methods of the class in python, it binds the attributes with the given arguments.

 

1.

class Point:

    def move(self):

        print('Move')

    def draw(self):

        print('Draw')

 

point1=Point()

point1.move()

 

Move

 

2.

class Point:

    def move(self):

        print('Move')

    def draw(self):

        print('Draw')

 

point1=Point()

point1.x=10

point1.y=20

print(point1.x,point1.y)

 

10 20

 

3.

class Point:

    def move(self):

        print('Move')

    def draw(self):

        print('Draw')

 

point1=Point()

point2=Point()

point2.x=30

point2.y=40

print(point2.x,point2.y)

 

30 40


CONSTRUCTOR:

A constructor is a function that gets called at the time of creating an object.

In the body of this method (__init__(self,parameters) means initialize) we should read the values passed here to initialize our object

self is a reference to the current object.

 

1.       

class Point:

    def __init__(self,x,y):

        self.x=x              

        self.y=y   

    def move(self):

        print('Move')

    def draw(self):

        print('Draw')

       

point=Point(20,30)

print(point.x,',',point.y)

type(point.x)

 

20 , 30

int


2.

class Point:

    def __init__(self,x,y):

        self.x=x              

        self.y=y  

    def move(self):

        print(f'Move {self.x}')

    def draw(self):

        print('Draw')

       

point=Point(20,30)

point.move()

 

Move 20

 

INHERITENCE:

Inheritence is a mechanism for reusing code. As soon as we inherit the parent class to the sub class, the sub class will inherit all the methods that is defined in the parent class.

Python doesn’t allow us to keep the class empty so pass keyword is used in the class.

 

1.

class Mammal:

    def walk(self):

        print('walk')

 

class Dog(Mammal):

    pass

 

class Cat(Mammal):

    def meow(self):

        print('meow')

 

obj1=Dog()

obj1.walk()

obj2=Cat()

obj2.meow()

 

walk

meow

 

-----------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------

Comments

Post a Comment

Popular posts from this blog

SQL Course (PART-1)

Open_CV BASICS