SlideShare a Scribd company logo
3
Most read
5
Most read
6
Most read
Operator Overloading In Python
What’s in it for you?
How to overload the operators in
Python?
What is Operator Overloading?
What is Operator Overloading?
Python operators work for built-in classes in a predefined
operational way. But the same operator behaves
differently for objects of different classes
For example, the + operator will perform arithmetic
addition on two numbers, merge two lists, or
concatenate two strings
This feature in Python that allows the same operator to
have different meanings according to the context is
called operator overloading
8 + 7
Operands
Operator
How to overload the operators in Python?
Object 1
B1=batsman(66, 78)
Class Batsman
Object 2
B2=batsman(23, 59)
Consider that we have two objects B1 and B2 of a class Batsman. We
must add both objects with a binary ‘+’ operator
It will throw an error
because the compiler
doesn’t know how to
add two objects. Here,
we will define a method
for an operator and that
process is called
operator overloading
How to overload the operators in Python?
We can overload all defined
operators. Python provides some
special or magic function that
is automatically invoked when
associated with that operator to
perform operator overloading
• __add__()
• __sub__()
• __mul__()
• __div__()
• __gt__()
• __lt__()
These are some of the
magic methods
How to overload the operators in Python?
For example, when we use
the + operator, the magic
method __add__ is
automatically invoked in
which the operation for the
+ operator is defined. Here
we will overload the +
operator to add the scores
of batsman
Output
INDIA
Simplilearn Solutions Pvt. Limited
#53/1C, 24th Main, 2nd Sector
HSR Layout, Bangalore 560102
Phone: +91 8069999471
UNITED STATES
Simplilearn Solutions Pvt. Limited
801 Corporate Center Drive, Suite 138
Raleigh, NC 27607
Phone: (919) 205-5565
UNITED STATES
Simplilearn Solutions Pvt. Limited
201 Spear Street, Suite 1100
San Francisco, CA 94105
Phone: (415) 741-3319
Join us to learn more!

More Related Content

PPTX
Chapter 07 inheritance
Praveen M Jigajinni
 
PPTX
Packages In Python Tutorial
Simplilearn
 
PPTX
Chapter 05 classes and objects
Praveen M Jigajinni
 
ODP
Python Modules
Nitin Reddy Katkam
 
PPTX
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Introduction to Python
Mohammed Sikander
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PPTX
Functions in Python
Kamal Acharya
 
Chapter 07 inheritance
Praveen M Jigajinni
 
Packages In Python Tutorial
Simplilearn
 
Chapter 05 classes and objects
Praveen M Jigajinni
 
Python Modules
Nitin Reddy Katkam
 
Object oriented programming in python
baabtra.com - No. 1 supplier of quality freshers
 
Introduction to Python
Mohammed Sikander
 
Modules and packages in python
TMARAGATHAM
 
Functions in Python
Kamal Acharya
 

What's hot (20)

PPTX
Modules in Python Programming
sambitmandal
 
PPTX
Variables in python
Jaya Kumari
 
PPTX
Operator overloading
Ramish Suleman
 
PDF
Numeric Data types in Python
jyostna bodapati
 
PDF
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
PPTX
Object Oriented Programming in Python
Sujith Kumar
 
PPTX
Python-Encapsulation.pptx
Karudaiyar Ganapathy
 
PPTX
Looping Statements and Control Statements in Python
PriyankaC44
 
PDF
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
PPTX
Member Function in C++
NikitaKaur10
 
PPT
Python Dictionaries and Sets
Nicole Ryan
 
PPTX
Method overloading
Lovely Professional University
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPT
Class and object in C++
rprajat007
 
PPTX
Values and Data types in python
Jothi Thilaga P
 
PPTX
Python OOPs
Binay Kumar Ray
 
PPTX
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
PPTX
Polymorphism
Nochiketa Chakraborty
 
Modules in Python Programming
sambitmandal
 
Variables in python
Jaya Kumari
 
Operator overloading
Ramish Suleman
 
Numeric Data types in Python
jyostna bodapati
 
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Python: Modules and Packages
Damian T. Gordon
 
Object Oriented Programming in Python
Sujith Kumar
 
Python-Encapsulation.pptx
Karudaiyar Ganapathy
 
Looping Statements and Control Statements in Python
PriyankaC44
 
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
Member Function in C++
NikitaKaur10
 
Python Dictionaries and Sets
Nicole Ryan
 
Method overloading
Lovely Professional University
 
Constructor in java
Pavith Gunasekara
 
Class and object in C++
rprajat007
 
Values and Data types in python
Jothi Thilaga P
 
Python OOPs
Binay Kumar Ray
 
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
Polymorphism
Nochiketa Chakraborty
 
Ad

Similar to Operator Overloading In Python (20)

DOCX
Operator oveerloading
yatinnarula
 
PPT
08 c++ Operator Overloading.ppt
Tareq Hasan
 
PPT
08 c-operator-overloadingppt2563
Youth For Peace
 
PPT
Unary operator overloading
BalajiGovindan5
 
PPTX
Operator overloading
Burhan Ahmed
 
PPT
Binary operator overloading
BalajiGovindan5
 
PPT
Operator overloading in C++
BalajiGovindan5
 
PPTX
B.sc CSIT 2nd semester C++ Unit4
Tekendra Nath Yogi
 
PPTX
Presentation on overloading
Charndeep Sekhon
 
DOCX
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
festockton
 
PPTX
Operator overloading
Garima Singh Makhija
 
PPTX
Presentation1.pptx
WaqasJaved76
 
PPTX
Oops
ankush_kumar
 
PPT
Lec 26.27-operator overloading
Princess Sam
 
PPTX
Operator overloaing
zindadili
 
PPT
Lec 28 - operator overloading
Princess Sam
 
PPTX
Operator Overloading
Juginder Pal Singh
 
PPT
Java Operators.ppt
Pragna15
 
PDF
Operator overloading C++
Lahiru Dilshan
 
PDF
Oop05 6
schwaa
 
Operator oveerloading
yatinnarula
 
08 c++ Operator Overloading.ppt
Tareq Hasan
 
08 c-operator-overloadingppt2563
Youth For Peace
 
Unary operator overloading
BalajiGovindan5
 
Operator overloading
Burhan Ahmed
 
Binary operator overloading
BalajiGovindan5
 
Operator overloading in C++
BalajiGovindan5
 
B.sc CSIT 2nd semester C++ Unit4
Tekendra Nath Yogi
 
Presentation on overloading
Charndeep Sekhon
 
Assg 07 Templates and Operator OverloadingCOSC 2336 Sprin.docx
festockton
 
Operator overloading
Garima Singh Makhija
 
Presentation1.pptx
WaqasJaved76
 
Lec 26.27-operator overloading
Princess Sam
 
Operator overloaing
zindadili
 
Lec 28 - operator overloading
Princess Sam
 
Operator Overloading
Juginder Pal Singh
 
Java Operators.ppt
Pragna15
 
Operator overloading C++
Lahiru Dilshan
 
Oop05 6
schwaa
 
Ad

More from Simplilearn (20)

PPTX
Top 50 Scrum Master Interview Questions | Scrum Master Interview Questions & ...
Simplilearn
 
PPTX
Bagging Vs Boosting In Machine Learning | Ensemble Learning In Machine Learni...
Simplilearn
 
PPTX
Future Of Social Media | Social Media Trends and Strategies 2025 | Instagram ...
Simplilearn
 
PPTX
SQL Query Optimization | SQL Query Optimization Techniques | SQL Basics | SQL...
Simplilearn
 
PPTX
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
Simplilearn
 
PPTX
How To Start Influencer Marketing Business | Influencer Marketing For Beginne...
Simplilearn
 
PPTX
Cyber Security Roadmap 2025 | How To Become Cyber Security Engineer In 2025 |...
Simplilearn
 
PPTX
How To Become An AI And ML Engineer In 2025 | AI Engineer Roadmap | AI ML Car...
Simplilearn
 
PPTX
What Is GitHub Copilot? | How To Use GitHub Copilot? | How does GitHub Copilo...
Simplilearn
 
PPTX
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Simplilearn
 
PPTX
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Simplilearn
 
PPTX
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Simplilearn
 
PPTX
Data Cleaning In Data Mining | Step by Step Data Cleaning Process | Data Clea...
Simplilearn
 
PPTX
Top 10 Data Analyst Projects For 2025 | Data Analyst Projects | Data Analysis...
Simplilearn
 
PPTX
AI Engineer Roadmap 2025 | AI Engineer Roadmap For Beginners | AI Engineer Ca...
Simplilearn
 
PPTX
Machine Learning Roadmap 2025 | Machine Learning Engineer Roadmap For Beginne...
Simplilearn
 
PPTX
Kotter's 8-Step Change Model Explained | Kotter's Change Management Model | S...
Simplilearn
 
PPTX
Gen AI Engineer Roadmap For 2025 | How To Become Gen AI Engineer In 2025 | Si...
Simplilearn
 
PPTX
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Simplilearn
 
PPTX
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Simplilearn
 
Top 50 Scrum Master Interview Questions | Scrum Master Interview Questions & ...
Simplilearn
 
Bagging Vs Boosting In Machine Learning | Ensemble Learning In Machine Learni...
Simplilearn
 
Future Of Social Media | Social Media Trends and Strategies 2025 | Instagram ...
Simplilearn
 
SQL Query Optimization | SQL Query Optimization Techniques | SQL Basics | SQL...
Simplilearn
 
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
Simplilearn
 
How To Start Influencer Marketing Business | Influencer Marketing For Beginne...
Simplilearn
 
Cyber Security Roadmap 2025 | How To Become Cyber Security Engineer In 2025 |...
Simplilearn
 
How To Become An AI And ML Engineer In 2025 | AI Engineer Roadmap | AI ML Car...
Simplilearn
 
What Is GitHub Copilot? | How To Use GitHub Copilot? | How does GitHub Copilo...
Simplilearn
 
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Simplilearn
 
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Simplilearn
 
Top 7 High Paying AI Certifications Courses For 2025 | Best AI Certifications...
Simplilearn
 
Data Cleaning In Data Mining | Step by Step Data Cleaning Process | Data Clea...
Simplilearn
 
Top 10 Data Analyst Projects For 2025 | Data Analyst Projects | Data Analysis...
Simplilearn
 
AI Engineer Roadmap 2025 | AI Engineer Roadmap For Beginners | AI Engineer Ca...
Simplilearn
 
Machine Learning Roadmap 2025 | Machine Learning Engineer Roadmap For Beginne...
Simplilearn
 
Kotter's 8-Step Change Model Explained | Kotter's Change Management Model | S...
Simplilearn
 
Gen AI Engineer Roadmap For 2025 | How To Become Gen AI Engineer In 2025 | Si...
Simplilearn
 
Top 10 Data Analyst Certification For 2025 | Best Data Analyst Certification ...
Simplilearn
 
Complete Data Science Roadmap For 2025 | Data Scientist Roadmap For Beginners...
Simplilearn
 

Recently uploaded (20)

PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PDF
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 

Operator Overloading In Python

  • 2. What’s in it for you? How to overload the operators in Python? What is Operator Overloading?
  • 3. What is Operator Overloading? Python operators work for built-in classes in a predefined operational way. But the same operator behaves differently for objects of different classes For example, the + operator will perform arithmetic addition on two numbers, merge two lists, or concatenate two strings This feature in Python that allows the same operator to have different meanings according to the context is called operator overloading 8 + 7 Operands Operator
  • 4. How to overload the operators in Python? Object 1 B1=batsman(66, 78) Class Batsman Object 2 B2=batsman(23, 59) Consider that we have two objects B1 and B2 of a class Batsman. We must add both objects with a binary ‘+’ operator It will throw an error because the compiler doesn’t know how to add two objects. Here, we will define a method for an operator and that process is called operator overloading
  • 5. How to overload the operators in Python? We can overload all defined operators. Python provides some special or magic function that is automatically invoked when associated with that operator to perform operator overloading • __add__() • __sub__() • __mul__() • __div__() • __gt__() • __lt__() These are some of the magic methods
  • 6. How to overload the operators in Python? For example, when we use the + operator, the magic method __add__ is automatically invoked in which the operation for the + operator is defined. Here we will overload the + operator to add the scores of batsman Output
  • 7. INDIA Simplilearn Solutions Pvt. Limited #53/1C, 24th Main, 2nd Sector HSR Layout, Bangalore 560102 Phone: +91 8069999471 UNITED STATES Simplilearn Solutions Pvt. Limited 801 Corporate Center Drive, Suite 138 Raleigh, NC 27607 Phone: (919) 205-5565 UNITED STATES Simplilearn Solutions Pvt. Limited 201 Spear Street, Suite 1100 San Francisco, CA 94105 Phone: (415) 741-3319 Join us to learn more!