SlideShare a Scribd company logo
Classes And Objects
Team Emertxe
Creation of Class
Creation of Class
General Format

Class is a model or plan to create the objects

Class contains,

Attributes: Represented by variables

Actions : Performed on methods

Syntax of defining the class,
Syntax Example
class Classname(object):
"""docstrings"""
Attributes
def __init__(self):
def method1():
def method2():
class Student:
"""The below block defines attributes"""
def __init__(self):
self.name = "Ram"
self.age = 21
self.marks = 89.75
"""The below block defines a method"""
def putdata(self):
print("Name: ", self.name)
print("Age: ", self.age)
print("Marks: ", self.marks)
Creation of Class
Program
#To define the Student calss and create an Object to it.
#Class Definition
class Student:
#Special method called constructor
def __init__(self):
self.name = "Ram"
self.age = 21
self.marks = 75.90
#This is an instance method
def putdata(self):
print("Name: ", self.name)
print("Age: ", self.age)
print("Marks: ", self.marks)
#Create an instance to the student class
s = Student()
#Call the method using an Object
s.putdata()
The Self Variable
The Self Variable

ā€˜Self’ is the default variable that contains the memory address of the instance of the
current class
s1 = Student() ļ‚· s1 contains the memory address of the instance
ļ‚· This memory address is internally and by default passed to
ā€˜self’ variable
Usage-1:
def __init__(self):
ļ‚· The ā€˜self’ variable is used as first parameter in the
constructor
Usage-2:
def putdata(self):
 The ā€˜self’ variable is used as first parameter in the
instance methods
Constructor
Constructor
Constructor with NO parameter

Constructors are used to create and initialize the 'Instance Variables'

Constructor will be called only once i.e at the time of creating the objects

s = Student()
Example def __init__(self):
self.name = "Ram"
self.marks = 99
Constructor
Constructor with parameter
Example def __init__(self, n = "", m = 0):
self.name = n
self.marks = m
Instance-1 s = Student()
Will initialize the instance variables with default parameters
Instance-2 s = Student("Ram", 99)
Will initialize the instance variables with parameters passed
Constructor
Program
#To create Student class with a constructor having more than one parameter
class Student:
#Constructor definition
def __init__(self, n = "", m = 0):
self.name = n
self.marks = m
#Instance method
def putdata(self):
print("Name: ", self.name)
print("Marks: ", self.marks)
#Constructor called without any parameters
s = Student()
s.putdata()
#Constructor called with parameters
s = Student("Ram", 99)
s.putdata()
Types of Variables
Types Of Variables

Instance variables

Class / Static variables
Types Of Variables
Instance Variables

Variables whose separate copy is created for every instance/object

These are defined and init using the constructor with 'self' parameter

Accessing the instance variables from outside the class,

instancename.variable
class Sample:
def __init__(self):
self.x = 10
def modify(self):
self.x += 1
#Create an objects
s1 = Sample()
s2 = Sample()
print("s1.x: ", s1.x)
print("s2.x: ", s2.x)
s1.modify()
print("s1.x: ", s1.x)
print("s2.x: ", s2.x)
Types Of Variables
Class Variables

Single copy is created for all instances

Accessing class vars are possible only by 'class methods'

Accessing class vars from outside the class,

classname.variable
class Sample:
#Define class var here
x = 10
@classmethod
def modify(cls):
cls.x += 1
#Create an objects
s1 = Sample()
s2 = Sample()
print("s1.x: ", s1.x)
print("s2.x: ", s2.x)
s1.modify()
print("s1.x: ", s1.x)
print("s2.x: ", s2.x)
Namespaces
Namespaces
Introduction

Namespace represents the memory block where names are mapped/linked to objects

Types:

Class namespace

- The names are mapped to class variables

Instance namespace

- The names are mapped to instance variables
Namespaces
Class Namespace
#To understand class namespace
#Create the class
class Student:
#Create class var
n = 10
#Access class var in class namespace
print(Student.n)
#Modify in class namespace
Student.n += 1
#Access class var in class namespace
print(Student.n)
#Access class var in all instances
s1 = Student()
s2 = Student()
#Access class var in instance namespace
print("s1.n: ", s1.n)
print("s2.n: ", s2.n)
10n
Class Namespace
10n 10n
Instance NamespaceInstance Namespace
11n
Class Namespace
11n 11n
Instance NamespaceInstance Namespace
Before modifyng class variable ā€˜n’
After modifyng class variable ā€˜n’
If class vars are modified in class namespace, then it reflects to all instances
Namespaces
Instance Namespace
#To understand class namespace
#Create the class
class Student:
#Create class var
n = 10
s1 = Student()
s2 = Student()
#Modify the class var in instance namespace
s1.n += 1
#Access class var in instance namespace
print("s1.n: ", s1.n)
print("s2.n: ", s2.n)
10n
Class Namespace
10n 10n
Instance NamespaceInstance Namespace
10n
Class Namespace
11n 10n
Instance NamespaceInstance Namespace
Before modifyng class variable ā€˜n’
After modifyng class variable ā€˜n’
If class vars are modified in instance namespace, then it reflects only to
that instance
Types of Methods
Types of Methods

Types:

Instance Methods

- Accessor

- Mutator

Class Methods

Static Methods
Types of Methods
Instance Methods
ā—
Acts upon the instance variables of that class
ā—
Invoked by instance_name.method_name()
#To understanf the instance methods
class Student:
#Constructor definition
def __init__(self, n = "", m = 0):
self.name = n
self.marks = m
#Instance method
def putdata(self):
print("Name: ", self.name)
print("Marks: ", self.marks)
#Constructor called without any parameters
s = Student()
s.putdata()
#Constructor called with parameters
s = Student("Ram", 99)
s.putdata()
Types of Methods
Instance Methods: Accessor + Mutator
#To understand accessor and mutator
#Create the class
class Student:
#Define mutator
def setName(self, name):
self.name = name
#Define accessor
def getName(self):
return self.name
#Create an objects
s = Student()
#Set the name
s.setName("Ram")
#Print the name
print("Name: ", s.getName())
Accessor Mutator
ā— Methods just reads the instance variables,
will not modify it
ā— Generally written in the form: getXXXX()
ā— Also called getter methods
ā— Not only reads the data but also modifies
it
ā— Generally wriiten in the form: setXXXX()
ā— Also called setter methods
Types of Methods
Class Methods
#To understand the class methods
class Bird:
#Define the class var here
wings = 2
#Define the class method
@classmethod
def fly(cls, name):
print("{} flies with {} wings" . format(name, cls.wings))
#Call
Bird.fly("Sparrow")
Bird.fly("Pigeon")
ā— This methods acts on class level
ā— Acts on class variables only
ā— Written using @classmethod decorator
ā— First param is 'cls', followed by any params
ā— Accessed by classname.method()
Types of Methods
Static Methods
#To Understand static method
class Sample:
#Define class vars
n = 0
#Define the constructor
def __init__(self):
Sample.n = Sample.n + 1
#Define the static method
@staticmethod
def putdata():
print("No. of instances created: ", Sample.n)
#Create 3 objects
s1 = Sample()
s2 = Sample()
s3 = Sample()
#Class static method
Sample.putdata()
ā— Needed, when the processing is at the class level but we need not involve the class or
instances
ā— Examples:
- Setting the environmental variables
- Counting the number of instances of the class
ā— Static methods are written using the decorator @staticmethod
ā— Static methods are called in the form classname.method()
Passing Members
Passing Members
ā— It is possible to pass the members(attributes / methods) of one class to another
ā— Example:
e = Emp()
ā— After creating the instance, pass this to another class 'Myclass'
ā— Myclass.mymethod(e)
- mymethod is static
Passing Members
Example
#To understand how members of one class can be passed to another
#Define the class
class Emp:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def putdata(self):
print("Name: ", self.name)
print("Salary: ", self.salary)
#Create Object
e = Emp("Ram", 20000)
#Call static method of Myclass and pass e
Myclass.mymethod(e)
#Define another class
class Myclass:
@staticmethod
def mymethod(e):
e.salary += 1000
e.putdata()
Passing Members
Exercise
1. To calculate the power value of a number with the help of a static method
Inner Class
Inner Class
Introduction
ā— Creating class B inside Class A is called nested class or Inner class
ā— Example:
Person's Data like,
- Name: Single value
- Age: Single Value
- DoB: Multiple values, hence separate class is needed
Inner Class
Program: Version-1
#To understand inner class
class Person:
def __init__(self):
self.name = "Ram"
self.db = self.Dob()
def display(self):
print("Name: ", self.name)
#Define an inner class
class Dob:
def __init__(self):
self.dd = 10
self.mm = 2
self.yy = 2002
def display(self):
print("DoB: {}/{}/{}" . format(self.dd,
self.mm, self.yy))
#Creating Object
p = Person()
p.display()
#Create inner class object
i = p.db
i.display()
Inner Class
Program: Version-2
#To understand inner class
class Person:
def __init__(self):
self.name = "Ram"
self.db = self.Dob()
def display(self):
print("Name: ", self.name)
#Define an inner class
class Dob:
def __init__(self):
self.dd = 10
self.mm = 2
self.yy = 2002
def display(self):
print("DoB: {}/{}/{}" . format(self.dd,
self.mm, self.yy))
#Creating Object
p = Person()
p.display()
#Create inner class object
i = Person().Dob()
i.display()
THANK YOU

More Related Content

What's hot (20)

Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Kamal Acharya
Ā 
Python Modules
Python ModulesPython Modules
Python Modules
Nitin Reddy Katkam
Ā 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
Ā 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
Ā 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
Ā 
Database connectivity in python
Database connectivity in pythonDatabase connectivity in python
Database connectivity in python
baabtra.com - No. 1 supplier of quality freshers
Ā 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and Sets
Nicole Ryan
Ā 
Python Functions
Python   FunctionsPython   Functions
Python Functions
Mohammed Sikander
Ā 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
Ā 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
Ā 
Python Lambda Function
Python Lambda FunctionPython Lambda Function
Python Lambda Function
Md Soyaib
Ā 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Mohammed Sikander
Ā 
Templates
TemplatesTemplates
Templates
Pranali Chaudhari
Ā 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
Ā 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
•sreejith •sree
Ā 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
Ā 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
Ā 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
RubaNagarajan
Ā 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programming
sambitmandal
Ā 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
Ā 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
Kamal Acharya
Ā 
Python: Modules and Packages
Python: Modules and PackagesPython: Modules and Packages
Python: Modules and Packages
Damian T. Gordon
Ā 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
Ā 
Python Dictionaries and Sets
Python Dictionaries and SetsPython Dictionaries and Sets
Python Dictionaries and Sets
Nicole Ryan
Ā 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
Ā 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
Ā 
Python Lambda Function
Python Lambda FunctionPython Lambda Function
Python Lambda Function
Md Soyaib
Ā 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
Haresh Jaiswal
Ā 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
Ā 
Constructors in C++
Constructors in C++Constructors in C++
Constructors in C++
RubaNagarajan
Ā 
Modules in Python Programming
Modules in Python ProgrammingModules in Python Programming
Modules in Python Programming
sambitmandal
Ā 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
Devashish Kumar
Ā 

Similar to Python programming : Classes objects (20)

Python_Unit_3.pdf
Python_Unit_3.pdfPython_Unit_3.pdf
Python_Unit_3.pdf
alaparthi
Ā 
Regex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overlodingRegex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overloding
sangumanikesh
Ā 
Unit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptxUnit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
Ā 
Module-5-Classes and Objects for Python Programming.pptx
Module-5-Classes and Objects for Python Programming.pptxModule-5-Classes and Objects for Python Programming.pptx
Module-5-Classes and Objects for Python Programming.pptx
YogeshKumarKJMIT
Ā 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
Ā 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
Ā 
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov
Ā 
what is class in C++ and classes_objects.ppt
what is class in C++ and classes_objects.pptwhat is class in C++ and classes_objects.ppt
what is class in C++ and classes_objects.ppt
malikliyaqathusain
Ā 
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
thuy27042005thieu
Ā 
Object_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdfObject_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdf
Koteswari Kasireddy
Ā 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.ppt
muneshwarbisen1
Ā 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMINGOOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
Ā 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
NuurAxmed2
Ā 
Lecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.pptLecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.ppt
Reji K Dhaman
Ā 
Lecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.pptLecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
Ā 
Python 2. classes- cruciql for students objects1.pptx
Python 2. classes- cruciql for students objects1.pptxPython 2. classes- cruciql for students objects1.pptx
Python 2. classes- cruciql for students objects1.pptx
KiranRaj648995
Ā 
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptxPython_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Koteswari Kasireddy
Ā 
slides11-objects_and_classes in python.pptx
slides11-objects_and_classes in python.pptxslides11-objects_and_classes in python.pptx
slides11-objects_and_classes in python.pptx
debasisdas225831
Ā 
Python Lecture 13
Python Lecture 13Python Lecture 13
Python Lecture 13
Inzamam Baig
Ā 
Python lec4
Python lec4Python lec4
Python lec4
Swarup Ghosh
Ā 
Python_Unit_3.pdf
Python_Unit_3.pdfPython_Unit_3.pdf
Python_Unit_3.pdf
alaparthi
Ā 
Regex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overlodingRegex,functions, inheritance,class, attribute,overloding
Regex,functions, inheritance,class, attribute,overloding
sangumanikesh
Ā 
Unit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptxUnit – V Object Oriented Programming in Python.pptx
Unit – V Object Oriented Programming in Python.pptx
YugandharaNalavade
Ā 
Module-5-Classes and Objects for Python Programming.pptx
Module-5-Classes and Objects for Python Programming.pptxModule-5-Classes and Objects for Python Programming.pptx
Module-5-Classes and Objects for Python Programming.pptx
YogeshKumarKJMIT
Ā 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
Ā 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
Ā 
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov, Introduction to Python, Lecture5
Anton Kasyanov
Ā 
what is class in C++ and classes_objects.ppt
what is class in C++ and classes_objects.pptwhat is class in C++ and classes_objects.ppt
what is class in C++ and classes_objects.ppt
malikliyaqathusain
Ā 
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
2_Classes.pptxjdhfhdfohfshfklsfskkfsdklsdk
thuy27042005thieu
Ā 
Object_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdfObject_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdf
Koteswari Kasireddy
Ā 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.ppt
muneshwarbisen1
Ā 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMINGOOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
Ā 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
NuurAxmed2
Ā 
Lecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.pptLecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.ppt
Reji K Dhaman
Ā 
Lecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.pptLecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
Ā 
Python 2. classes- cruciql for students objects1.pptx
Python 2. classes- cruciql for students objects1.pptxPython 2. classes- cruciql for students objects1.pptx
Python 2. classes- cruciql for students objects1.pptx
KiranRaj648995
Ā 
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptxPython_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Koteswari Kasireddy
Ā 
slides11-objects_and_classes in python.pptx
slides11-objects_and_classes in python.pptxslides11-objects_and_classes in python.pptx
slides11-objects_and_classes in python.pptx
debasisdas225831
Ā 
Python Lecture 13
Python Lecture 13Python Lecture 13
Python Lecture 13
Inzamam Baig
Ā 
Ad

More from Emertxe Information Technologies Pvt Ltd (20)

Career Transition (1).pdf
Career Transition (1).pdfCareer Transition (1).pdf
Career Transition (1).pdf
Emertxe Information Technologies Pvt Ltd
Ā 
10_isxdigit.pdf
10_isxdigit.pdf10_isxdigit.pdf
10_isxdigit.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
01_student_record.pdf
01_student_record.pdf01_student_record.pdf
01_student_record.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
02_swap.pdf
02_swap.pdf02_swap.pdf
02_swap.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
01_sizeof.pdf
01_sizeof.pdf01_sizeof.pdf
01_sizeof.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
07_product_matrix.pdf
07_product_matrix.pdf07_product_matrix.pdf
07_product_matrix.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
06_sort_names.pdf
06_sort_names.pdf06_sort_names.pdf
06_sort_names.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
05_fragments.pdf
05_fragments.pdf05_fragments.pdf
05_fragments.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
04_magic_square.pdf
04_magic_square.pdf04_magic_square.pdf
04_magic_square.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
03_endianess.pdf
03_endianess.pdf03_endianess.pdf
03_endianess.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
02_variance.pdf
02_variance.pdf02_variance.pdf
02_variance.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
01_memory_manager.pdf
01_memory_manager.pdf01_memory_manager.pdf
01_memory_manager.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
09_nrps.pdf
09_nrps.pdf09_nrps.pdf
09_nrps.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
11_pangram.pdf
11_pangram.pdf11_pangram.pdf
11_pangram.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
10_combinations.pdf
10_combinations.pdf10_combinations.pdf
10_combinations.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
08_squeeze.pdf
08_squeeze.pdf08_squeeze.pdf
08_squeeze.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
07_strtok.pdf
07_strtok.pdf07_strtok.pdf
07_strtok.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
06_reverserec.pdf
06_reverserec.pdf06_reverserec.pdf
06_reverserec.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
05_reverseiter.pdf
05_reverseiter.pdf05_reverseiter.pdf
05_reverseiter.pdf
Emertxe Information Technologies Pvt Ltd
Ā 
Ad

Recently uploaded (20)

Measuring Microsoft 365 Copilot and Gen AI Success
Measuring Microsoft 365 Copilot and Gen AI SuccessMeasuring Microsoft 365 Copilot and Gen AI Success
Measuring Microsoft 365 Copilot and Gen AI Success
Nikki Chapple
Ā 
Securiport - A Border Security Company
Securiport  -  A Border Security CompanySecuriport  -  A Border Security Company
Securiport - A Border Security Company
Securiport
Ā 
Introducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRCIntroducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRC
Adtran
Ā 
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Nikki Chapple
Ā 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
Ā 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
Ā 
Evaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical ContentEvaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical Content
Paul Groth
Ā 
Grannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI ExperiencesGrannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI Experiences
Lauren Parr
Ā 
Microsoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentationMicrosoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentation
Digitalmara
Ā 
UiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build PipelinesUiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build Pipelines
UiPathCommunity
Ā 
Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025
Prasta Maha
Ā 
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath InsightsUiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPathCommunity
Ā 
Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...
pranavbodhak
Ā 
Cyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptxCyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptx
Ghimire B.R.
Ā 
Let’s Get Slack Certified! šŸš€- Slack Community
Let’s Get Slack Certified! šŸš€- Slack CommunityLet’s Get Slack Certified! šŸš€- Slack Community
Let’s Get Slack Certified! šŸš€- Slack Community
SanjeetMishra29
Ā 
Contributing to WordPress With & Without Code.pptx
Contributing to WordPress With & Without Code.pptxContributing to WordPress With & Without Code.pptx
Contributing to WordPress With & Without Code.pptx
Patrick Lumumba
Ā 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
Ā 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
Ā 
Maxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing placeMaxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing place
usersalmanrazdelhi
Ā 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
Ā 
Measuring Microsoft 365 Copilot and Gen AI Success
Measuring Microsoft 365 Copilot and Gen AI SuccessMeasuring Microsoft 365 Copilot and Gen AI Success
Measuring Microsoft 365 Copilot and Gen AI Success
Nikki Chapple
Ā 
Securiport - A Border Security Company
Securiport  -  A Border Security CompanySecuriport  -  A Border Security Company
Securiport - A Border Security Company
Securiport
Ā 
Introducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRCIntroducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRC
Adtran
Ā 
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Protecting Your Sensitive Data with Microsoft Purview - IRMS 2025
Nikki Chapple
Ā 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
Ā 
6th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 20256th Power Grid Model Meetup - 21 May 2025
6th Power Grid Model Meetup - 21 May 2025
DanBrown980551
Ā 
Evaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical ContentEvaluation Challenges in Using Generative AI for Science & Technical Content
Evaluation Challenges in Using Generative AI for Science & Technical Content
Paul Groth
Ā 
Grannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI ExperiencesGrannie’s Journey to Using Healthcare AI Experiences
Grannie’s Journey to Using Healthcare AI Experiences
Lauren Parr
Ā 
Microsoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentationMicrosoft Build 2025 takeaways in one presentation
Microsoft Build 2025 takeaways in one presentation
Digitalmara
Ā 
UiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build PipelinesUiPath Community Zurich: Release Management and Build Pipelines
UiPath Community Zurich: Release Management and Build Pipelines
UiPathCommunity
Ā 
Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025Kubernetes Cloud Native Indonesia Meetup - May 2025
Kubernetes Cloud Native Indonesia Meetup - May 2025
Prasta Maha
Ā 
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath InsightsUiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPathCommunity
Ā 
Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...Cyber security cyber security cyber security cyber security cyber security cy...
Cyber security cyber security cyber security cyber security cyber security cy...
pranavbodhak
Ā 
Cyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptxCyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptx
Ghimire B.R.
Ā 
Let’s Get Slack Certified! šŸš€- Slack Community
Let’s Get Slack Certified! šŸš€- Slack CommunityLet’s Get Slack Certified! šŸš€- Slack Community
Let’s Get Slack Certified! šŸš€- Slack Community
SanjeetMishra29
Ā 
Contributing to WordPress With & Without Code.pptx
Contributing to WordPress With & Without Code.pptxContributing to WordPress With & Without Code.pptx
Contributing to WordPress With & Without Code.pptx
Patrick Lumumba
Ā 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
Ā 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
Ā 
Maxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing placeMaxx nft market place new generation nft marketing place
Maxx nft market place new generation nft marketing place
usersalmanrazdelhi
Ā 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
Ā 

Python programming : Classes objects

  • 3. Creation of Class General Format  Class is a model or plan to create the objects  Class contains,  Attributes: Represented by variables  Actions : Performed on methods  Syntax of defining the class, Syntax Example class Classname(object): """docstrings""" Attributes def __init__(self): def method1(): def method2(): class Student: """The below block defines attributes""" def __init__(self): self.name = "Ram" self.age = 21 self.marks = 89.75 """The below block defines a method""" def putdata(self): print("Name: ", self.name) print("Age: ", self.age) print("Marks: ", self.marks)
  • 4. Creation of Class Program #To define the Student calss and create an Object to it. #Class Definition class Student: #Special method called constructor def __init__(self): self.name = "Ram" self.age = 21 self.marks = 75.90 #This is an instance method def putdata(self): print("Name: ", self.name) print("Age: ", self.age) print("Marks: ", self.marks) #Create an instance to the student class s = Student() #Call the method using an Object s.putdata()
  • 6. The Self Variable  ā€˜Self’ is the default variable that contains the memory address of the instance of the current class s1 = Student() ļ‚· s1 contains the memory address of the instance ļ‚· This memory address is internally and by default passed to ā€˜self’ variable Usage-1: def __init__(self): ļ‚· The ā€˜self’ variable is used as first parameter in the constructor Usage-2: def putdata(self):  The ā€˜self’ variable is used as first parameter in the instance methods
  • 8. Constructor Constructor with NO parameter  Constructors are used to create and initialize the 'Instance Variables'  Constructor will be called only once i.e at the time of creating the objects  s = Student() Example def __init__(self): self.name = "Ram" self.marks = 99
  • 9. Constructor Constructor with parameter Example def __init__(self, n = "", m = 0): self.name = n self.marks = m Instance-1 s = Student() Will initialize the instance variables with default parameters Instance-2 s = Student("Ram", 99) Will initialize the instance variables with parameters passed
  • 10. Constructor Program #To create Student class with a constructor having more than one parameter class Student: #Constructor definition def __init__(self, n = "", m = 0): self.name = n self.marks = m #Instance method def putdata(self): print("Name: ", self.name) print("Marks: ", self.marks) #Constructor called without any parameters s = Student() s.putdata() #Constructor called with parameters s = Student("Ram", 99) s.putdata()
  • 12. Types Of Variables  Instance variables  Class / Static variables
  • 13. Types Of Variables Instance Variables  Variables whose separate copy is created for every instance/object  These are defined and init using the constructor with 'self' parameter  Accessing the instance variables from outside the class,  instancename.variable class Sample: def __init__(self): self.x = 10 def modify(self): self.x += 1 #Create an objects s1 = Sample() s2 = Sample() print("s1.x: ", s1.x) print("s2.x: ", s2.x) s1.modify() print("s1.x: ", s1.x) print("s2.x: ", s2.x)
  • 14. Types Of Variables Class Variables  Single copy is created for all instances  Accessing class vars are possible only by 'class methods'  Accessing class vars from outside the class,  classname.variable class Sample: #Define class var here x = 10 @classmethod def modify(cls): cls.x += 1 #Create an objects s1 = Sample() s2 = Sample() print("s1.x: ", s1.x) print("s2.x: ", s2.x) s1.modify() print("s1.x: ", s1.x) print("s2.x: ", s2.x)
  • 16. Namespaces Introduction  Namespace represents the memory block where names are mapped/linked to objects  Types:  Class namespace  - The names are mapped to class variables  Instance namespace  - The names are mapped to instance variables
  • 17. Namespaces Class Namespace #To understand class namespace #Create the class class Student: #Create class var n = 10 #Access class var in class namespace print(Student.n) #Modify in class namespace Student.n += 1 #Access class var in class namespace print(Student.n) #Access class var in all instances s1 = Student() s2 = Student() #Access class var in instance namespace print("s1.n: ", s1.n) print("s2.n: ", s2.n) 10n Class Namespace 10n 10n Instance NamespaceInstance Namespace 11n Class Namespace 11n 11n Instance NamespaceInstance Namespace Before modifyng class variable ā€˜n’ After modifyng class variable ā€˜n’ If class vars are modified in class namespace, then it reflects to all instances
  • 18. Namespaces Instance Namespace #To understand class namespace #Create the class class Student: #Create class var n = 10 s1 = Student() s2 = Student() #Modify the class var in instance namespace s1.n += 1 #Access class var in instance namespace print("s1.n: ", s1.n) print("s2.n: ", s2.n) 10n Class Namespace 10n 10n Instance NamespaceInstance Namespace 10n Class Namespace 11n 10n Instance NamespaceInstance Namespace Before modifyng class variable ā€˜n’ After modifyng class variable ā€˜n’ If class vars are modified in instance namespace, then it reflects only to that instance
  • 20. Types of Methods  Types:  Instance Methods  - Accessor  - Mutator  Class Methods  Static Methods
  • 21. Types of Methods Instance Methods ā— Acts upon the instance variables of that class ā— Invoked by instance_name.method_name() #To understanf the instance methods class Student: #Constructor definition def __init__(self, n = "", m = 0): self.name = n self.marks = m #Instance method def putdata(self): print("Name: ", self.name) print("Marks: ", self.marks) #Constructor called without any parameters s = Student() s.putdata() #Constructor called with parameters s = Student("Ram", 99) s.putdata()
  • 22. Types of Methods Instance Methods: Accessor + Mutator #To understand accessor and mutator #Create the class class Student: #Define mutator def setName(self, name): self.name = name #Define accessor def getName(self): return self.name #Create an objects s = Student() #Set the name s.setName("Ram") #Print the name print("Name: ", s.getName()) Accessor Mutator ā— Methods just reads the instance variables, will not modify it ā— Generally written in the form: getXXXX() ā— Also called getter methods ā— Not only reads the data but also modifies it ā— Generally wriiten in the form: setXXXX() ā— Also called setter methods
  • 23. Types of Methods Class Methods #To understand the class methods class Bird: #Define the class var here wings = 2 #Define the class method @classmethod def fly(cls, name): print("{} flies with {} wings" . format(name, cls.wings)) #Call Bird.fly("Sparrow") Bird.fly("Pigeon") ā— This methods acts on class level ā— Acts on class variables only ā— Written using @classmethod decorator ā— First param is 'cls', followed by any params ā— Accessed by classname.method()
  • 24. Types of Methods Static Methods #To Understand static method class Sample: #Define class vars n = 0 #Define the constructor def __init__(self): Sample.n = Sample.n + 1 #Define the static method @staticmethod def putdata(): print("No. of instances created: ", Sample.n) #Create 3 objects s1 = Sample() s2 = Sample() s3 = Sample() #Class static method Sample.putdata() ā— Needed, when the processing is at the class level but we need not involve the class or instances ā— Examples: - Setting the environmental variables - Counting the number of instances of the class ā— Static methods are written using the decorator @staticmethod ā— Static methods are called in the form classname.method()
  • 26. Passing Members ā— It is possible to pass the members(attributes / methods) of one class to another ā— Example: e = Emp() ā— After creating the instance, pass this to another class 'Myclass' ā— Myclass.mymethod(e) - mymethod is static
  • 27. Passing Members Example #To understand how members of one class can be passed to another #Define the class class Emp: def __init__(self, name, salary): self.name = name self.salary = salary def putdata(self): print("Name: ", self.name) print("Salary: ", self.salary) #Create Object e = Emp("Ram", 20000) #Call static method of Myclass and pass e Myclass.mymethod(e) #Define another class class Myclass: @staticmethod def mymethod(e): e.salary += 1000 e.putdata()
  • 28. Passing Members Exercise 1. To calculate the power value of a number with the help of a static method
  • 30. Inner Class Introduction ā— Creating class B inside Class A is called nested class or Inner class ā— Example: Person's Data like, - Name: Single value - Age: Single Value - DoB: Multiple values, hence separate class is needed
  • 31. Inner Class Program: Version-1 #To understand inner class class Person: def __init__(self): self.name = "Ram" self.db = self.Dob() def display(self): print("Name: ", self.name) #Define an inner class class Dob: def __init__(self): self.dd = 10 self.mm = 2 self.yy = 2002 def display(self): print("DoB: {}/{}/{}" . format(self.dd, self.mm, self.yy)) #Creating Object p = Person() p.display() #Create inner class object i = p.db i.display()
  • 32. Inner Class Program: Version-2 #To understand inner class class Person: def __init__(self): self.name = "Ram" self.db = self.Dob() def display(self): print("Name: ", self.name) #Define an inner class class Dob: def __init__(self): self.dd = 10 self.mm = 2 self.yy = 2002 def display(self): print("DoB: {}/{}/{}" . format(self.dd, self.mm, self.yy)) #Creating Object p = Person() p.display() #Create inner class object i = Person().Dob() i.display()