python training
python training
def m3():
print("static method")
print(m3)
NOTE: If we change the value of static variable by using either self or object
reference variable, then the value of static variable won't be changed, just a new
instance variable with that name will be added to that particular object.
class Test:
Test
a=10
a=888
a=10
def m1(self):
self.a=888
t=Test()
11.m10
print(Test.a)
print(t1.a)
Output
10888
class Test:
x=10 def init _(self):
self.y=20
t1=Test()
t2=Test()
print('t1:,t1.x,t1.y)
print('t2:,(2.x, t2.y)
t1.X=888
t1.y=999
print('t1:,t1.x,t1.y)
print('t2:, t2.x, t2.y)
M -
x=888
¥-999
Test
x=10
y=20
12
Output
M1: 10 20
t2: 10 20
M: 888 999
t2: 10 20
Local Varlables:
> Sometimes to meet temporary requirements of programmer, we can declare variables
Inside a method directly, such type of varlables are called local variable or
temporary variables.
> Local variables will be created at the time of method execution and destroyed
once method completes.
> Local variables of a method cannot be accessed from outside of method. class
Test:
class Test:
def mi(sell):
del m1(self):
a=1000
221000
print(a)
print(a))
def m2(self):
def m2(sell):
b=2000
b=2000
print(b)
print(a)
l=Test0)
Output
1000
2000
print(b)
NameError: namo 'a' is not defined
(=Test)
t.m1(
t.m2()
t.m2()
Types of Methods:
Instance Methods:
> Inside method implementation if we are using instance variables then such type of
methods are called instance methods.
›Inside instance method declaration, we have to pass self variable.
*def m1(self):
›By using self variable inside method we can able to access instance variables.
›Within the class we can call instance method by using self variable and from
outside of the class we can call by using object reference.
print ("RNO:"
ISelf. rno)
self. grade ()
def
grade (self) :
if self.marks<=100:
if self.marks>80
and self.marks<=100:
print ("TOPPER")
eli£
self.marks>60 and self.marks<=80:
print ("1St class")
fuser code
rno=int (input ("Enter Rno: "))
name=input ("Enter name: ")
marks=int (input ("Enter marks: ")) s=Student (rno, name, marks)
S. std
_details ()
Class Methods:
> Inside method implementation if we are using only class variables (static
variables), then such type of methods we should declare as class method.
• We can declare class method explicitly by using @classmethod decorator.
> For class method we should provide els variable at the time of declaration
> We can call classmethod by using classname or object reference variable.
class test:
count=0
def__init__(self)
text.count+=1;
@classmethod
def noofobjrct(cls):
print("no of objecrt:",Test.count):
t1=test()
t2=test()
t3=test()
Static Methods:
In general these methods are general utility methods.
Inside these methods we w
on't use any instance
›Here we won't provide self or cls arguments at the time of declaration.
>We can declare static method explicitly by using @staticmethod decorator
>We can access statio methods by using classname or object reference
class TwinkleMath:
@staticmethod def add(x,y):
print('The Sum:',x+y)
@staticmethod def prodcuct(x,y):
print(The Product:',x*y)
@staticmothod
del average(x, y)
print/'The averager
TwinkleMath.add(10,20)
TwinkleMath.product/10,2
TwinkleMath.average(10,20)
the sum=30
the product=200
the average=15.0
Note:- in general we can use only instance and static method inside static method
we can acess
class level variable by using class name
import os
import time class Bank:
b_name="TWINKLE"
def
init__(self, _name, bal=0.0) :
self.cname=c_name
self.balance=bal
def deposite (self, amt) :
self.balance+=amt
def withdraw(self, amt) :
self.balance-=amt
def cust
_details (self) :
print ("Cutoner
_hamle:"
, Self.c_name)
print ("Cutomer
Balance:", self.balance)
print("*
**WELCOME TO", Bank.b_name,
name=input ("Enter Customer name: ")
b=Bank (name)
while True:
os. system ("cls")
print("*n
***WELCOME TO", Bank.b.
_name,
**).
b. cust _details ()
print ("\n\nl.deposit\n2.withdraw \n3.Cutomer details\n4.Exit")
op=int (input ("Choose your option: "))
if op==1:
b. deposite (amt)
elIt op==2:
if b. balance › amt:
b. withdraw (amt)
else:
A stack is a linear data structure that follows the principle of Last In First Out
(LIFO). This means the last element inserted inside the stack is removed first.