[go: up one dir, main page]

0% found this document useful (0 votes)
3 views6 pages

python training

The document explains how to access static variables in Python through various methods including constructors, instance methods, class methods, and static methods. It also discusses local variables, types of methods (instance, class, and static), and provides examples of each. Additionally, it introduces data structures, differentiating between linear and non-linear types, and explains the concept of a stack as a Last In First Out (LIFO) data structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

python training

The document explains how to access static variables in Python through various methods including constructors, instance methods, class methods, and static methods. It also discusses local variables, types of methods (instance, class, and static), and provides examples of each. Additionally, it introduces data structures, differentiating between linear and non-linear types, and explains the concept of a stack as a Last In First Out (LIFO) data structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

How to access Static Variables:

> inside constructor: by using either self or classname


• inside instance method: by using either self or classname
• inside class method: by using either els variable or classname
• inside static method: by using classname
From outside of class: by using either object reference or classname
program:-
class Test:
x=99
def__init__(self):
print ("Constructor")
print (Test.x)
def
ml (self) :
print ("Instance method")
print (Test. x)
@classmethod def
m2 (cls) :
print ("Classmethods")
print (Test. x)
print (cls.x)

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:

Inside Python class 3 types of methods are allowed


1) Instance Methods
2) Class Methods
3) Static 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.

#driver code class


Student:
def
init
(self, no, name, marks) :
self. rno=rno
self. name=name self.marks=marks
def std _details (self) :
print ("RNO:", self. rno)
print ("NAME:", self, name)
self. grade ()
def
grade (self) :
if self.marks<=100:

self.marks>80
and self.marks<=100:

def std details (self) :

print ("RNO:"
ISelf. rno)

print (" NAME:"


, self. name)

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

elif self.marks>50 and


seLf.narks<=60:

print ("2nd class")

elif self.marks>35 and self.marks<=50:


else:
else:
print ("Fail")
print ("'Invalid Marks")

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:

amt=float (input ("Enter Amount: "))

b. deposite (amt)

elIt op==2:

amt=float (input ("Enter amount: "))

if b. balance › amt:

b. withdraw (amt)

else:

print ("Insufficent Funds")

what is data Structure?


ans:- a data structure is a data organization management,and storage format that
enables efficent and modifications.

Types of data structure:-


1.primitive data structure: integer, float, character, boolean

2. Non-primitive data structure


a. Linear Data Structure:
Arrays LinkedList stack queue
b. Non Linear Data Structure:
Graphs Trees

Linear Data structure


1.In a linear data structure, data elements are arranged in a linear order where
each and every element is attached to its previous and next adjacent.
2.In linear data structure, single level is involved.
3.Its implementation is easy in comparison to non-linear data structure.
4. In a linear data structure, memory is not utilized in an efficient way.
5.Applications of linear data structures are mainly in application software

2.non-linear data structure


1. non-linear data structure, data elements are attached in hierarchically
manner.
2.Whereas in non-linear data structure, multiple levels are involved.
3.While its implementation is complex in comparison to linear data structure.
4.While in a non-linear data structure, momory is utilized in an efficient way,
5.Applications of non-linear
data structures are in Artificial Intelligence and image processing.

What is the stack?

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.

There are some basic operations that allow us to pentorm


different actions on a stack. (Stack ADT (Abstract Data type) )

«Push: Add an element to the top of a stack


•Pop: Remove an element from the top of a stack
•IsEmpty: Check if the stack is empty
•IsFull: Check if the stack is full
•Peek: Get the value of the top element without removing it

You might also like