Thanks to visit codestin.com
Credit goes to www.tutorialsteacher.com

Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Python - Get Started
  • What is Python?
  • Where to use Python?
  • Python Version History
  • Install Python
  • Python - Shell/REPL
  • Python IDLE
  • Python Editors
  • Python Syntax
  • Python Keywords
  • Python Variables
  • Python Data Types
  • Number
  • String
  • List
  • Tuple
  • Set
  • Dictionary
  • Python Operators
  • Python Conditions - if, elif
  • Python While Loop
  • Python For Loop
  • User Defined Functions
  • Lambda Functions
  • Variable Scope
  • Python Modules
  • Module Attributes
  • Python Packages
  • Python PIP
  • __main__, __name__
  • Python Built-in Modules
  • OS Module
  • Sys Module
  • Math Module
  • Statistics Module
  • Collections Module
  • Random Module
  • Python Generator Function
  • Python List Comprehension
  • Python Recursion
  • Python Built-in Error Types
  • Python Exception Handling
  • Python Assert Statement
  • Define Class in Python
  • Inheritance in Python
  • Python Access Modifiers
  • Python Decorators
  • @property Decorator
  • @classmethod Decorator
  • @staticmethod Decorator
  • Python Dunder Methods
  • CRUD Operations in Python
  • Python Read, Write Files
  • Regex in Python
  • Create GUI using Tkinter
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Python Operators: Arithmetic, Assignment, Comparison, Logical, Identity, Membership, Bitwise

Operators are special symbols that perform some operation on operands and returns the result. For example, 5 + 6 is an expression where + is an operator that performs arithmetic add operation on numeric left operand 5 and the right side operand 6 and returns a sum of two operands as a result.

Python includes the operator module that includes underlying methods for each operator. For example, the + operator calls the operator.add(a,b) method.

Example: Operator Methods
import operator

n=5+5  
print(n)

n=operator.add(5, 10)
print(n)

n=operator.__add__(5, 20)
print(n)
Try it

Above, expression 5 + 6 is equivalent to the expression operator.add(5, 6) and operator.__add__(5, 6). Many function names are those used for special methods, without the double underscores (dunder methods). For backward compatibility, many of these have functions with the double underscores kept.

Python includes the following categories of operators:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Identity Operators
  • Membership Test Operators
  • Bitwise Operators

Arithmetic Operators

Arithmetic operators perform the common mathematical operation on the numeric operands.

The arithmetic operators return the type of result depends on the type of operands, as below.

  1. If either operand is a complex number, the result is converted to complex;
  2. If either operand is a floating point number, the result is converted to floating point;
  3. If both operands are integers, then the result is an integer and no conversion is needed.

The following table lists all the arithmetic operators in Python:

OperationOperatorFunctionExample in Python Shell
Addition: Sum of two operands+operator.add(a,b)
x,y= 5,6 print(x + y) #output: 11 import operator operator.add(5,6) #output: 11
Subtraction: Left operand minus right operand-operator.sub(a,b)
x,y =5,6 print(x - y) #output: -1 import operator operator.sub(10, 5) #output: 5
Multiplication*operator.mul(a,b)
x,y =5,6 print(x * y) #output: 30 import operator operator.mul(5,6) #output: 30
Exponentiation: Left operand raised to the power of right**operator.pow(a,b)
x = 2; y = 3 print(x ** y) #output: 8 import operator operator.pow(2, 3) #output: 8
Division/operator.truediv(a,b)
x = 6; y = 3 print(x / y) #output: 2 import operator operator.truediv(6, 3) #output: 2
Floor division: equivilant to math.floor(a/b)//operator.floordiv(a,b)
x = 6; y = 5 print(x // y)  #output: 1 import operator operator.floordiv(6,5) #output: 1
Modulus: Reminder of a/b%operator.mod(a, b)
x = 11; y = 3 print(x % y) #output: 12 import operator operator.mod(11, 3) #output: 2

Assignment Operators

The assignment operators are used to assign values to variables. The following table lists all the arithmetic operators in Python:

OperatorFunctionExample in Python Shell
=
x = 5; x 5
+=operator.iadd(a,b)
x = 5 print(x += 5) #output: 10 import operator x = operator.iadd(5, 5) #output: 10
-=operator.isub(a,b)
x = 5 print(x -= 2) #output: 3 import operator x = operator.isub(5,2)
*=operator.imul(a,b)
x = 2 print(x *= 3) #output: 6 import operator x = operator.imul(2, 3)
/=operator.itruediv(a,b)
x = 6 print(x /= 3)  #output: 2 import operator x = operator.itruediv(6, 3)
//=operator.ifloordiv(a,b)
x = 6 print(x //= 5) #output: 1 import operator operator.ifloordiv(6,5)
%=operator.imod(a, b)
x = 11 print(x %= 3) #output: 2 import operator operator.imod(11, 3) #output: 2
&=operator.iand(a, b)
x = 11 print(x &= 3) #output: 1 import operator operator.iand(11, 3) #output: 1
|=operator.ior(a, b)
x = 3 print(x |= 4) #output: 7 import operator operator.mod(3, 4) #output: 7
^=operator.ixor(a, b)
x = 5 print(x ^= 2)  #output: 7 import operator operator.ixor(5, 2) #output: 7
>>=operator.irshift(a, b)
x = 5 print(x >>= 2) #output: 1 import operator operator.irshift(5, 2)  #output: 1
<<=operator.ilshift(a, b)
x = 5 print(x <<= 2)  #output: 20 import operator operator.ilshift(5, 2)  #output: 20

Comparison Operators

The comparison operators compare two operands and return a boolean either True or False. The following table lists comparison operators in Python.

OperatorFunctionDescriptionExample in Python Shell
>operator.gt(a,b)True if the left operand is higher than the right one
x,y =5,6 print(x > y) #output: False import operator operator.gt(5,6) #output: False
<operator.lt(a,b)True if the left operand is lower than right one
x,y =5,6 print(x < y)  #output: True import operator operator.add(5,6) #output: True
==operator.eq(a,b)True if the operands are equal
x,y =5,6 print(x == y) #output: False import operator operator.eq(5,6) #output: False
!=operator.ne(a,b)True if the operands are not equal
x,y =5,6 print(x != y)  #output: True import operator operator.ne(5,6)  #output: True
>=operator.ge(a,b)True if the left operand is higher than or equal to the right one
x,y =5,6 print(x >= y)  #output: False import operator operator.ge(5,6)  #output: False
<=operator.le(a,b)True if the left operand is lower than or equal to the right one
x,y =5,6 print(x <= y)  #output: True import operator operator.le(5,6)  #output: True

Logical Operators

The logical operators are used to combine two boolean expressions. The logical operations are generally applicable to all objects, and support truth tests, identity tests, and boolean operations.

OperatorDescriptionExample
andTrue if both are true
x,y =5,6 print(x > 1 and y <10) #output: True
orTrue if at least one is true
x,y =5,6 print(x > 6 or y <10) #output: True
notReturns True if an expression evalutes to false and vice-versa
x = 5 print(not x > 1) #output: False

Identity Operators

The identity operators check whether the two objects have the same id value e.i. both the objects point to the same memory location.

OperatorFunctionDescriptionExample in Python Shell
isoperator.is_(a,b)True if both are true
x,y =5,6 print(x is y) #output: False import operator operator.is_(x,y) #output: False
is notoperator.is_not(a,b)True if at least one is true
x,y =5,6 print(x is not y)  #output: True import operator operator.is_not(x, y) #output: True

Membership Test Operators

The membership test operators in and not in test whether the sequence has a given item or not. For the string and bytes types, x in y is True if and only if x is a substring of y.

OperatorFunctionDescriptionExample in Python Shell
inoperator.contains(a,b)Returns True if the sequence contains the specified item else returns False.
nums = [1,2,3,4,5] print(1 in nums) #output: True print(10 in nums) #output: False print('str' in 'string') #output: True import operator operator.contains(nums, 2)  #output: True
not innot operator.contains(a,b)Returns True if the sequence does not contains the specified item, else returns False.
nums = [1,2,3,4,5] print(1 not in nums)  #output: False print(10 not in nums) #output: True print('str' not in 'string') #output: False import operator not operator.contains(nums, 2)  #output: False

Bitwise Operators

Bitwise operators perform operations on binary operands.

OperatorFunctionDescriptionExample in Python Shell
&operator.and_(a,b)Sets each bit to 1 if both bits are 1.
x=5; y=10 z=x & y print(z) #output: 0 import operator operator.and_(x, y)
|operator.or_(a,b)Sets each bit to 1 if one of two bits is 1.
x=5; y=10 z=x | y print(z) #output: 15 import operator operator.or_(x, y)
^operator.xor(a,b)Sets each bit to 1 if only one of two bits is 1.
x=5; y=10 z=x ^ y print(z) #output: 15 import operator operator.xor(x, y)
~operator.invert(a)Inverts all the bits.
x=5 print(~x) #output: -6 import operator operator.invert(x)
<<operator.lshift(a,b)Shift left by pushing zeros in from the right and let the leftmost bits fall off.
x=5 print(x<<2)  #output: 20 import operator operator.lshift(x,2)
>>operator.rshift(a,b)Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off.
x=5 print(x>>2) #output: 1 import operator operator.rshift(x,2)
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.