A practical example of operator overloading

Lesson Details:
June 29, 2020

I: Introduction

A: Learn basic python programming for beginners

In this article, we will learn about the basics of python programming language. I will give a brief introduction to the python programming language. We will also learn how to install python for windows or linux operating system. In addition, we will learn the installation of pip and virtualenv for python.

Python is a high level programming language. It was designed by Guido van Rossum in the late 1980s. It is a general purpose interpreted programming language with a simple syntax. It is a dynamic typed programming language. The word “python” refers to a tribe of African and Asian non-venomous snakes, some of which are not poisonous. It is the most popular programming language for machine learning as well as data science.

Python is a hybrid object-oriented and structured programming language. Python has many built-in modules that support the development of GUI applications as well as web application development. It has a huge standard library which includes modules to support data science, big data analytics, machine learning, artificial intelligence, and deep learning. Python can be used to develop applications for a variety of domains such as web development, software development, network programming, systems administration, etc.

A Practical Example of Operator Overloading

Let us take an example to understand the concept of operator overloading in python. Suppose you have two variables named var1 and var2 both are integers. Further, suppose you want to multiply these two variables together. To do so, you need to use the arithmetic multiplication operator (i.e., *). You can simply write the following statement to achieve this result: var1 = var2 * 2 . However, you can also write this statement by using the multiplication operator (*) directly instead of writing var1 = var2 * 2 .

Let us see how to code this statement in python:

def multiply ( x , y ): return x * y multiply ( 2 , 3 ) # multiplies 2 with 3 using the multiplication operator (*) print ( multiply ( 5 , 6 )) # multiplies 5 with 6 using the multiplication operator (*) print ( multiply ( 10 , 11 )) # multiplies 10 with 11 using the multiplication operator (*) print ( multiply ( "hello" , "world" )) # multiplies "hello" with "world" using the multiplication operator (*) def main (): x = 10 y = 13 print ( multiply ( x , y )) if __name__ == '__main__' : main ()


30 42 110 hello world

The above program is an example of operator overloading in python. Let us see what happened here. First, we defined a function named multiply . This function takes two parameters named x and y both are integers. Inside the function, we simply used the multiplication operator (*) twice to perform the multiplication operation on the value of x and y . Then, we printed the values of x , y , and x * y . Next, we defined another function named main . Inside the main function, we first declared two variables named x and y both are integers. Then, we called the function multiply twice to perform mathematical operations on x and y . Finally, we printed the value of x , y , and x * y .

Whenever you overload operators in python, you must use parentheses after them if they are not alone inside parentheses or square brackets. However, if they are alone inside parentheses or square brackets then it is OK not to use parentheses after them. For example, multiply(x + 1) is correct whereas multiply(x + 1) without parentheses is incorrect. Please note that operators should not be overloaded if they are alone inside parentheses or square brackets. I hope in the next tutorial we will learn more in detail about operators in python in my next article in future.

Course content