Monday, November 4, 2019

Learning python -Day 8


Class is a collection of data members and member functions
Objects is an instance of the classs. Objects contains all the information of the class.

OOP:
1)      Encapsulation: wraping and binding of data
2)      Polymorphism: many forms
3)      Abstarction: hiding of details
4)      Inheritance: reusability of old. Types of inheritance:
a)      Single inheritance
b)      Multilevel inheritance
c)      Tree
d)      Multiple
e)      Hybrid

class Emp:
    comp=
"TCS"
   
def disp(self):
       
print("Hello",self.name)


ravi=Emp()
ravi.name=
"Ravi Kumar"
ravi.age=29
ravi.id=1234
ravi.city="Noida"

print(ravi.name,ravi.age,ravi.id,ravi.city)

tom=Emp()
tom.name=
"Tom"
tom.id=4763
tom.degree="BCA"
print(tom.name,tom.id,tom.degree)
print(ravi.comp,tom.comp,Emp.comp)
run the function CTL+shift+fn+F10
Ravi Kumar 29 1234 Noida
Tom 4763 BCA

class Emp:
    comp=
"TCS"
   
def setValues(self,nname,nid,ncity):
       
self.name=nname
       
self.id=nid
       
self.city=ncity
   
def disp(self):
       
print("Hello",self.name,self.id,self.city)


ravi=Emp()
ravi.setValues(
"Ravi Kumar",1234,'Noida')
ravi.disp()

tom=Emp()
tom.setValues(
"Tom",94845,"Delhi")
tom.disp()
run the function CTL+shift+fn+F10

Hello Ravi Kumar 1234 Noida
Hello Tom 94845 Delhi
class Emp:
    comp=
"TCS"
   
def __init__(self,nname,nid,ncity):
       
self.name=nname
       
self.id=nid
       
self.city=ncity
   
def disp(self):
       
print("Hello",self.name,self.id,self.city)


ravi=Emp(
"Ravi Kumar",1234,'Noida')
ravi.disp()

tom=Emp(
"Tom",94845,"Delhi")
tom.disp()
run the function CTL+shift+fn+F10
Hello Ravi Kumar 1234 Noida
Hello Tom 94845 Delhi

Single inheritance:
class A:
    x=
10
   
def sum(self,a,b):
       
print('Sum=',(a+b))

class B(A):
    y=
20
   
def mul(self,a,b):
       
print("Mul=",a*b)

o1=B()
print(o1.x,o1.y)
o1.sum(
15,60)
o1.mul(
18,5)
run the function CTL+shift+fn+F10
10 20
Sum= 75
Mul= 90

Multi Level inheritance
class A:
    x=
10
   
def sum(self,a,b):
       
print('Sum=',(a+b))

class B(A):
    y=
20
   
def mul(self,a,b):
       
print("Mul=",a*b)
class C(B):
    z=
30
   
def mod(self,a,b):
       
print("Mod=",a%b)

o1=C()
print(o1.x,o1.y,o1.z)
o1.sum(
15,60)
o1.mul(
18,5)
o1.mod(
19,4)
run the function CTL+shift+fn+F10
10 20 30
Sum= 75
Mul= 90
Mod= 3
Tree Inheritance
class A:
    x=
10
   
def sum(self,a,b):
       
print('Sum=',(a+b))

class B(A):
    y=
20
   
def mul(self,a,b):
       
print("Mul=",a*b)
class C(A):
    z=
30
   
def mod(self,a,b):
       
print("Mod=",a%b)

o1=C()
print(o1.x,o1.z)
o1.sum(
15,60)
o1.mod(
19,4)
run the function CTL+shift+fn+F10

10 30
Sum= 75
Mod= 3

Multile inheritance
class A:
    x=
10
   
def sum(self,a,b):
       
print('Sum=',(a+b))
   
def disp(self):
       
print("Gud Mrng")

class B:
    y=
20
   
def mul(self,a,b):
       
print("Mul=",a*b)
   
def disp(self):
       
print("Gud Evng")

class C(A,B):
    z=
30
   
def mod(self,a,b):
       
print("Mod=",a%b)

o1=C()
print(o1.x,o1.y,o1.z)
o1.sum(
15,60)
o1.mul(
18,5)
o1.mod(
19,4)
o1.disp()
run the function CTL+shift+fn+F10
10 20 30
Sum= 75
Mul= 90
Mod= 3
Gud Mrng


   
def disp(self):
       
print("Gud Evng")

class C(A):
    z=
30class A:
    x=
10
   
def sum(self,a,b):
       
print('Sum=',(a+b))
   
def disp(self):
       
print("Gud Mrng")

class B(A):
    y=
20
   
def mul(self,a,b):
       
print("Mul=",a*b)
   
def mod(self,a,b):
       
print("Mod=",a%b)

class D(B,C):
    w=
40
   
def div(self,a,b):
       
print("Div=",a/b)

o1=D()
print(o1.x,o1.y,o1.z,o1.w)
o1.sum(
15,60)
o1.mul(
18,5)
o1.mod(
19,4)
o1.div(
16,7)
o1.disp()

For security, make variables private. Put  double underscore__
class Emp:
    comp=
"TCS"
   
def __init__(self,nname,nid,ncity):
       
self.__name=nname
       
self.__id=nid
       
self.__city=ncity
   
def disp(self):
       
print("Hello",self.__name,self.__id,self.__city)

   
@classmethod
   
def changeValue(cls,ncom):
       
cls.comp=ncom


ravi=Emp(
"Ravi Kumar",1234,'Noida')
ravi.disp()

tom=Emp(
"Tom",94845,"Delhi")
tom.disp()
tom.changeValue(
"Tata")
print(ravi.comp)
run the function CTL+shift+fn+F10
Hello Ravi Kumar 1234 Noida
Hello Tom 94845 Delhi
Tata

You use classmethod to change the vaue of comp in above environment
class Emp:
    comp=
"TCS"
   
def __init__(self,nname,nid,ncity):
       
self.name=nname
       
self.id=nid
       
self.city=ncity
   
def disp(self):
       
print("Hello",self.name,self.id,self.city)

   
@classmethod
   
def changeValue(cls,ncom):
       
cls.comp=ncom


ravi=Emp(
"Ravi Kumar",1234,'Noida')
ravi.disp()

tom=Emp(
"Tom",94845,"Delhi")
tom.disp()
tom.changeValue(
"Tata")
print(ravi.comp)
run the function CTL+shift+fn+F10
Hello Ravi Kumar 1234 Noida
Hello Tom 94845 Delhi
Tata
Random Modules are pre defined modules
import random

for i in range(10):
   
print(random.randint(1,10))
run the function CTL+shift+fn+F10
9
2
2
1
5
4
4
3
4
8
Randint is a functionnwhich gives you a random value within the range
random.randrange(1,10) generate a number between 1 and 9
random.uniform(1,10) will give you unique value

import random
list1=[
10,20,30,40,50]
print(random.choice(list1))
run the function CTL+shift+fn+F10
50
import random
list1=[
10,20,30,40,50]
random.shuffle(list1)
print(list1)

run the function CTL+shift+fn+F10
 [40, 20, 10, 50, 30]
import secrets
list1=[
10,20,30,40,50]
print(secrets.choice(list1))
run the function CTL+shift+fn+F10
20
import time

print(time.time())
print(time.ctime())
print(time.gmtime())

import calendar
print(calendar.calendar(2019))
print(calendar.month(2019,10))
print(calendar.isleap(2020))
run the function CTL+shift+fn+F10
1572931743.445168
Tue Nov  5 10:59:03 2019
time.struct_time(tm_year=2019, tm_mon=11, tm_mday=5, tm_hour=5, tm_min=29, tm_sec=3, tm_wday=1, tm_yday=309, tm_isdst=0)
                                  2019

      January                   February                   March
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6                   1  2  3                   1  2  3
 7  8  9 10 11 12 13       4  5  6  7  8  9 10       4  5  6  7  8  9 10
14 15 16 17 18 19 20      11 12 13 14 15 16 17      11 12 13 14 15 16 17
21 22 23 24 25 26 27      18 19 20 21 22 23 24      18 19 20 21 22 23 24
28 29 30 31               25 26 27 28               25 26 27 28 29 30 31

       April                      May                       June
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7             1  2  3  4  5                      1  2
 8  9 10 11 12 13 14       6  7  8  9 10 11 12       3  4  5  6  7  8  9
15 16 17 18 19 20 21      13 14 15 16 17 18 19      10 11 12 13 14 15 16
22 23 24 25 26 27 28      20 21 22 23 24 25 26      17 18 19 20 21 22 23
29 30                     27 28 29 30 31            24 25 26 27 28 29 30

        July                     August                  September
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7                1  2  3  4                         1
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       2  3  4  5  6  7  8
15 16 17 18 19 20 21      12 13 14 15 16 17 18       9 10 11 12 13 14 15
22 23 24 25 26 27 28      19 20 21 22 23 24 25      16 17 18 19 20 21 22
29 30 31                  26 27 28 29 30 31         23 24 25 26 27 28 29
                                                    30

      October                   November                  December
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6                   1  2  3                         1
 7  8  9 10 11 12 13       4  5  6  7  8  9 10       2  3  4  5  6  7  8
14 15 16 17 18 19 20      11 12 13 14 15 16 17       9 10 11 12 13 14 15
21 22 23 24 25 26 27      18 19 20 21 22 23 24      16 17 18 19 20 21 22
28 29 30 31               25 26 27 28 29 30         23 24 25 26 27 28 29
                                                    30 31

    October 2019
Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

True

User defined modules

def sum(x,y):
   
print("Sum=",x+y)

def sub(x,y):
   
print("Sub=",x-y)

if __name__=="__main__":
   
print("This is main module")
run the function CTL+shift+fn+F10

This is main module
import module1



def mul(x,y):

    print("Mul=",x*y)



module1.sum(10,30)

module1.sub(19,3)

mul(15,5)
run the function CTL+shift+fn+F10
Sum= 40
Sub= 16
Mul= 75
from module1 import sum

sum(
10,23)
run the function CTL+shift+fn+F10
Sum= 33

Learning python -Day 9

No comments:

Post a Comment

Featured Post

Ichimoku cloud

Here how you read a ichimoku cloud 1) Blue Converse line: It measures short term trend. it also shows minor support or resistance. Its ve...