- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python Standard operators as functions
In programming, operator is generally a symbol (key) predefined to perform a certain operation such as addition, subtraction, comparison etc. Python has a large set of built-in operations divided in different categories such as arithmetic, comparison, bit-wise, membership etc.
The operator module in python library consists of functions corresponding to built-in operators. Names of the functions are analogous to type of corresponding operator. For example, add() function in operator module corresponds to + operator.
Python’s Object class has dunder (double underscore before and after name) methods corresponding to operator symbols. These dunder methods can be suitably overloaded in user defined classes to implement operator overloading. The operator module also consists of corresponding dunder functions. For example, add() as well as __add__() both implement operation of + symbol.
To start with let us import functions from operator module
>>> from operator import *
To learn about functions corresponding to arithmetic operators, two variables are initialized as
>>> a = 10 >>> b = 20
add(), sub() and mul() correspond to +, - and * operators. The / symbol of division is implemented by truediv() function.
>>> add(a,b) 30 >>> a + b 30 >>> sub(a,b) -10 >>> a - b -10 >>> mul(a,b) 200 >>> a * b 200 >>> truediv(a,b) 0.5 >>> a / b 0.5
Other arithmetic operators %, ** and // are implemented by mod(), pow() and floordiv() functions from operator module.
>>> a = 5 >>> b = 3 >>> a % b 2 >>> mod(a,b) 2 >>> a ** b 125 >>> pow(a,b) 125 >>> a // 2 2 >>> floordiv(a,b)
Logical operators <, <=, >, >=, == and != are implemented by lt(), le(), gt(), ge(), eq() and ne() function respectively.
>>> a = 5 >>> b = 7 >>> a < b True >>> lt(a,b) True >>> a <= b True >>> le(a,b) True >>> a > b False >>> gt(a,b) False >>> a >= b False >>> ge(a,b) False >>> a == b False >>> eq(a,b) False >>> a != b True >>> ne(a,b) True
Functions working with sequences
Python’s built-in operators in, + (concatenation), and del are respectively implemented by contains(), concat(), delitem() functions. For indexed assignment operator seq[x] = y, setitem() function is used. For obtaining value of seq[x], getitem() function is used.
>>> a = [1,2,3] >>> b = ['a','b','c'] >>> a + b #sequence concatenation [1, 2, 3, 'a', 'b', 'c'] >>> concat(a,b) [1, 2, 3, 'a', 'b', 'c'] >>> contains(a,'2') #implements in operator False >>> contains(a,2) True >>> 2 in a True >>> b = 'x' #index assignment implemented by setitem() >>> b ['a', 'x', 'c'] >>> setitem(b,1,'b') >>> b ['a', 'b', 'c'] >>> a #fetching value at index – implemented by getitem() 2 >>> getitem(a,1) 2 >>> del b #deleting element at index. Corresponding function is delitem() >>> b ['a', 'b'] >>> delitem(a,2) >>> a [1, 2]
Operators that perform assignment and computation in a single step are called in-place operator. Python’s in-place operators are implemented by corresponding functions in operator module. The += operator which performs add and assign operation has corresponding iadd() function. Likewise each operator function prefixed by ‘i’ forms in-place equivalent.
>>> a = 10 >>> b = 20 >>> a = iadd(a,b) #equivalent to a += b >>> a 30
>>> a = 10 >>> b = 20 >>> a = isub(a,b) #equivalent to a -= b >>> a -10
>>> a= 10 >>> b = 20 >>> a = imul(a,b) #equivalent to a *= b >>> a 200
>>> a = 10 >>> b = 4 >>> a = itruediv(a,b) >>> a 2.5
The operator module also consists of functions implementing standard bit-wise operators
>>> and_(10,2) #equivalent to 10 & 2 2 >>> or_(10,2) #equivalent to 10 | 2 10 >>> xor(10,2) #equivalent to 10 ^ 2 8 >>> lshift(10,2) #equivalent to 10 << 2 40 >>> rshift(10,2) #equivalent to 10 >> 2 2
- Related Articles
- Inplace vs Standard Operators in Python
- Python Arithmetic Operators
- Python Comparison Operators
- Python Assignment Operators
- Python Bitwise Operators
- Python Logical Operators
- Python Membership Operators
- Python Operators Precedence
- IS vs AS Operators in C#
- Division Operators in Python?
- Basic Operators in Python
- Overloading Operators in Python
- Standard Data Types in Python