python operators

Python Operators

Operators in computer languages are used to perform various operations on variables and values. Python operators perform the same task in the python language. Below is a very simple program in python to understand the basic function of the operator.

print(10 + 10);


Output

20

This statement in python is making use of the ‘+’ operator. This operator is helping the user directly perform the addition of two numbers.

Python Operators Types

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators
  • Python Modulo operator

Airthematic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations like addition, subtraction, multiplication, division, etc. Some of them are described below:

OperatorNameExample
+Additiona+b
Subtractiona-b
*Multiplicationa*b
/Divisiona/b
**Exponentiationa**b
Airthematic Operators

Assignment Operators

Assignment operators are used to assign values to variables. Some of them are described below:

OperatorNameExample
=a=3a=5
+=a+=6a=a+6
-=a-=6a=a-6
*=a*=6a=a*6
Assignment Operators

Comparison Operators

Comparison operators in python are used to perform comparisons between two values. Some of them are described below:

OperatorNameExample
==Equala==b
!=Not equala!=b
>Greater thana>b
<Less thana<b
Comparison Operators

Logical Operators

Logical operators are used to combine conditional statements. Some of them are described below:

Operator NameDescriptionExample
andReturns True if both statements are truea < 5 AND  b < 10
orReturns False if both statements are falsea < 5 OR  b < 10
notReverse the result, returns False if the result is trueNOT(a < 5 and b < 10)
Logical operators

Identity Operators

Identity operators are used for comparing the objects in python, not if they are equal, but if they are actually the same object, with the same memory location. Some of them are described below:

Operator DescriptionExample
isReturns True if both variables are the same objecta is y
is notReturns True if both variables are not the same objecta is not y
Identity Operators

Membership Operators

Membership operators are used for testing if a sequence is presented in an object. Some of them are described below:

Operator DescriptionExample
inReturns True if a sequence with the specified value is present in the objecta in y
not inReturns True if a sequence with the specified value is not present in the objecta not in y
Membership Operators

Bitwise Operators

Bitwise operators are used for comparing binary numbers in python. Some of them are described below:

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if one of the 2 bits is 1
^XORSets each bit to 1 if only one of two bits is 1
~NOTInverts all the bits
Bitwise operators

Python Modulo Operator

We denote the Python Modulo operator as %. We can also call it a Modulus Operator. Python Modulo is a mathematical operation and we can use it for calculating remainder. Basically, this module is used to get the remainder of a division problem. This modulo operator is a type of arithmetic operator. 
To read more about Modulo Operator

Operator Precedence and Associativity

The precedence and associativity of operators in python determine the priorities of the operator. Mainly, precedence is used in an expression with more than one operator with different precedence to determine which operation to perform first. However, if an expression contains two or more operators with the same precedence then Operator Associativity is used to determine. It can either be Left to Right or from Right to Left.

Operator Overloading

Operator Overloading in python is a term for giving extended meaning beyond their pre-defined meaning. For example operator, ‘+’ is used to perform addition between two integers as well as join two strings and merge two lists. It is achievable because the β€˜+’ operator is overloaded by int class and str class in python. The learner might have noticed that the same built-in operator or function shows different behavior for objects of different classes, this is called Operator Overloading.

Below is a python code to understand operator overloading better:

print(10 + 10)
 
print("Developer"+"Helps")
 
print(10 * 10)
 
print("Developer"*2)


Output

20
DeveloperHelps
100
DeveloperDeveloper
>

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!