OOPS in PYTHON
] CLASS i 4 ciass is @ bluepsint for the object.
We can think oF class as q sketch of a payyot with
labels. It contains all the details about the name,
Colours, Size etc.
Example ie
Class payvor:
pass
Class Keyword fo define an empty class paryot-
J-_OBSECT=- An object ( instance ) 1§ an {nstontiation
oF Q Class. When class is defined, Only descsiption
fox object is defined, no memory or Stovage fs
allocated -
Example i
Class Vehicle $
def__init..Cseif, byand,model,type) +
Seif. byand = byand
Seif. JaS_tank-Size=1&
Vehicle object = vehicle C“Honda’, ‘tyuck?)
3). INHERITANCE %- Thrhevitance is a way of creating
Qa nen class fox using detells oF an existing
Class wilthout modifying it.Class Parent ():
def FivstC Seif 3
Print (6 First Function %)
Class chiidC Parent >:
def Second CselF >:
Print C6 Second Puncton’ )
Ob =Chitd C_)
Ob. Fiyst (
Ob. Second ( )
Output :-_Flyst Function
Second Function
4): ENCAPSULATION 2- Using OOP In python, We can
yestsict access 40 Methods and vaviable. This pyevent
data from divect modification vwihich is called as
Encapsulation .
Class EmPlovee 3
der__init__ (Seif, name ,Salasy , Project):
Se\f.name = name
SelP. salary = Salary
Sell. Prosect = Project
def Show (Seif _):
Print C'Namet , Seif.name, Salary *,, Sele Salayy >
def work Seif 7m
Print (Ser.name,* is wosking on? , self. Project)
4: Creating object of a class.
ae ane re nt
EMP = EmPlonee (*Ram 7s 10,000, *Python >)
HE Calling Public method
CONE STOW 2 scsi iliac ahaaeriaacaiea nies
emr. show C )
emP.work( )Outpur:-
Name : Ram Salary * 10,000
Ram iS working on python.
5I. ABSTRACTION - Abstyaction is used 40 hide +he
internal Functlonality oF +he function From the
users. Abstvaction can be achieved by using abstract
Classes and interfaces.
Example!-
Print (“Passed value :??, X)
Psint (wie ave inside Absclass task")
+est-obj =test. ClassC) 2
Outper:- We are Inside +est- class task
ie Passed Value : [00
€). POLYMORPHISM + “Iie IHtreval meaning of pelymosphism
is condition Z accuvance $n different forms.
Polymorphism Means a use of Single type entity
(Method , Opevator,oy Object) +o vepvesent dierevent
+upes in different scenayids-Class Rabbit ( )+
def age (Sele ):
Print C" determines 43 OF wabbit 7?)
def colour CSelF ):
Print C “determines colour oF yabbit”)
Class Horse CD:
derage (self):
Pyint (“determines age of horse)
def colour (Self Dt
Pyint determines Colour oF horse”)
obj 1 = Rabbit C
Obj 2 = HorseC )
For type In Cob} 1,0b5 2 2
type.age lL )
type. Colour )
Outpur:-
determines age of yabblit.
determines Colouy of yabbit.
determines age of norse-
determines colour of horse-
Ste Length of Sting
dieta
Dsinon OOPS Concepts
Fomon te 9 mutiparadigm programming fanguage. It
Seppoats afferent Programm|ng appyoches.
One of tre most popular approcher fo solve programming
problem by Creating objets This 1s fknoWn as objet
Ortented programming ( OOP).
“OOP hos 7190 chavacterisHes
D> Attrivutes
2). Behavior
Eyample A parrot }s an objec, as it has Following
Properties.
* name, age, Color as attyibures.
* Singing , Sanding a6 behavior.
The conupts of oor: In Fytnon focuses on creating
reusable Code. This Concept js also known Qs DPy
CDon't Repeat Yourself).
& Class is a blueprint for the Object. we can think of
Class as a skech of parrot With labels. [F contains gil
duals Ohout the name, colors, size ete.
oy.
Class farxrot:
Pass.
ATH KUMBRC CINKED IN)
FECES RAM NOTES GALLERY,
SpWbubbbebbidccdddddeWdeweeres
:
eHerve class Keyword detiné on empty class parrot from
Class We construc fnctances (-)n Instance js a Speelele
Objet created From a Particujar class.
ru Korn
ELEY RIN - NOS GALLERY.
#m objet Cinsrante) }s an instantiation of a class. blhen
Cc eee fs defined only phe aiseiption Fos phe Class Objet
£ defined. Therefore, nO memory or «forege Is allocated.
exi-
Obj = Parrot ¢ )
Here Objet fs On objet of class parrot:
Suppose We hove details of parrots. alow (02 axe geing
to show howd ¢0 bubld the class and Obj er” oF parrots.
we can access the Class attribure using — Class_ species -
Gonerivane }s qa Way of Creating new class for using
details of an existing class without modifylng it. The
newly formed clots fs derived class, Simillarly , +h2
existing class Is a base class»
Ex > Yse of Jnnevifana in python -
Class Bird:
der_Init_ (sere):
Print ("Bird ie ready")
ek culm (ree):
Print (" susie Fasper")
Class Penguin ( Bird)?
Jk —Init ( sere):
SUPE ( ),-Init_( 7
Bh xu (see):
Pz fen fone o) Boia Cin ea tatty)
gp. swam)
rant)
“ATUL KuMAR (INKED IAL «MOC ddd ddeWveeoeoecer
Output
Stalm Faster
yun Fast
lAle can use ghe fupey () Punetton Inside pne—Init_ Cl)
Method+ This aloWsS us fo yun — nit_() method.
Using OOP fn python , we can yettrict access 40 methods
and Vaylables. Tris prevents deta from dived mouFication
Which 5 cated encapsulation. In python We donate private
attribute using — at tht prefix. fe Single — ov double —-
Glass Computer?
def__fnit.. (See):
Selk__ Maxprice = 900
ek setmoxpniee ( sert , Price ):
Sak, 5 MAxPslee = price,
C= Compurer( )
C.__ Maxpricz .j000
C. Let max Prin (100)
ble used_init.-( ) method fo tose the maximum felling
Price of Computer.
Cc. Max price =1000
Varlable
Class Member Access From own | Arccessthte cessiole
access spefhey
Ud WOMAR (AINKED x1).NOUN L LLL Lacaddddveneeeereser
Poly me rpm fan ability to ULe a Common Interfau
for mutiple forms (data types) Folymorprism tn python
defines methods jn the child class thet have the same
name as the method in phe pasent Class. fF 1s possible
+0 modify 9 Method In qa child class tht Mr has
Inherited from Cid class ,. Parent Class
Class parrot ;
def Fly (ser):
Print (" Paryot conkiy")
def swim (ref)
Print (“parrot can't ststm")
Clase penguin:
def Fly ( sete):
Print (“penguin can't Fly")
de rulm¢ cere):
Print (“Penguin can stoim")
der Fly ng_ test (bird):
bird. Fly( )
biue = Parrott )
feg = penguin)
Plytng— test (blu)
Fuying — test (Peg)
Ovtpur
Poyron Can Fly.
Penguin Can't Puy.
ATU Lumar CLINKEDIN)lthet are Funcefons 2
# Fanetlon ic a plou of lode Only suns bhen Is called:
Dh Python a Funetlon he defined using pre de keyrord .
def my — Furetion ():
print C“yeno")
Arguments fn a Funceton
Arguments are specified after the function name,
inside the parentheses.
You can add 4S many argumentr as you tant
Leparate ¢hem 9th q Comma.
deF my — Rinetton CF name)
Print (fname + “ ReFsnes")
my Faretton C"Emit")
my - Funeton ("Tobias")
my — Function Chinas")
TECELRAN- NETES CALEERY.
Arbitary Arguments
Se you do not know) how many arguments that toil) be
passed inte your Funution, add a bebore the parameter NIme
in the function definition.
eF ry — Funurton (*kids) :
print ("the youngest child i¢"+ kidsl0)
Mg-Fantlon(" Sour” "9031", “txpark")
MAK (LINKEDIN) .
sbbbbhukrrrreerrreeeeeecre.DUWNULLL UL beTUddddddweeeeceser
Keyword Arguments ( Kwaxgs)
You can alsO sond arguments Lolth the Key >= value
Syntax -
def my- Rineeton (onias, CNida , CMidz) :
Print (“tne Youngest cntia it"+ Old 3)
my — baneeton Cobiidy = "mth" childa ="bea", Childs =‘bte")
E ATUL KOM BR CLINKEDIN)
Arbitary Keyword Arguments ** Kwargs
co not Kn0W ho many Keyword arguments
that wil) be pacred Into your Function, add 7W2 Qsterisk:
* * before the parameter name }n the Function definition .
dek my_ Function (#8 Gd):
print (“nic last name se" Kd E“tname I)
my fanetion (fname = “Tobias”, tname = “ReFenes”)
Atul Kumark CLINKEDAl)-
Easy French - 2 Books in 1 French Language + Short Stories For Beginners. A Complete Step-By-step Guide To Learn and Speak French Quick and Easy Starting From Zero