Click for All Topics
Operators are symbols or special characters that are used to perform operations on several values. These values on which the operation is performed are called operands. Python provides us with a pretty decent range of operators like arithmetic, logical, assignment and bitwise, etc. All of these operators have their own individual functions and roles. In total, there are seven operators in Python that are used to carry out particular operations. Let’s take a look at each one of them carefully.
Arithmetic operators are responsible for performing mathematical operations in Python. Different arithmetic operators are in charge of different operations. We have several operations like addition, subtraction, multiplication, division, and much more. Here is a list of arithmetic operators:
Name of Operator | Symbol of Operator | Example |
Addition operator | ‘+’ | x + y |
Subtraction operator | ‘-’ | x – y |
Multiplication operator | ‘*’ | x * y |
Division operator | ‘/’ | x / y |
Floor operator | ‘//’ | x // y |
Modulus operator | ‘%’ | x % y |
Exponentiation operator | ‘**’ | x ** y |
The assignment operators are used to assign values to variables in Python. These assignment operators will sometimes associate themselves with the arithmetic operator to perform various functions. Here are the major assignment operators:
Name of Operator | Symbol of operator | Example |
Assignment operator | ‘=’ | x = 10 |
Addition assignment | ‘+=’ | x += 3 |
subtraction assignment | ‘-=’ | x -= 3 |
Multiplication assignment | ‘*=’ | x *= 3 |
Division assignment | ‘/=’ | x /= 3 |
Modulus assignment | ‘%=’ | x %= 3 |
Exponentiation assignment | ‘**=’ | x **= 3 |
When two values are compared in Python, the comparison operator returns a Boolean value (true or false) based on the comparison outcome. There are several comparison operators in Python and the important ones are listed below for you.
Name of operator | Symbol for operator | Example |
Equal to | ‘==’ | x ==y |
Not equal to | ‘! =’ | x! = y |
Greater than | ‘>’ | x>y |
Less than | ‘<’ | x<y |
Greater than equal to | ‘> =’ | x> = y |
Less than equal to | ‘< =’ | x< =y |
Note:- Always keep in mind that ‘=’ is for assigning a value to the variable while ‘==’ is to check or compare two values.
Logical operators are Python symbols or keywords that allow you to combine or work with Boolean values (true or false) to create more complex conditions or expressions. There are three types of logical operators, which are listed in the table below.
Name of operator | Symbol of operator | Examples |
Logical AND | ‘and’ | x <2 and x<10 |
Logical OR | ‘or’ | x<4 or x<9 |
Logical NOT | ‘not’ | not x |
Bitwise operators in Python are used to carry out operations on a single bit of an integer value. They manipulate numbers at a binary level and treat them as collections of bits. The following bitwise operators are available in Python:
Name of operator | Symbol of operator | Example |
Bitwise AND | ‘&’ | x & y |
Bitwise OR | ‘ | ’ | x | y |
Bitwise XOR | ‘^’ | x ^ y |
Bitwise NOT | ‘~’ | x~y |
To determine whether a value is a member of a sequence or collection in Python, use the membership operators. Depending on whether the value is present or absent in the given sequence, they return a Boolean value (True or False). Python offers two operators for membership.
Name of Operator | About | Example |
in | It returns True if a value is found in the specified sequence, and False otherwise. | x in y |
not in | If a value is present in the sequence, it returns False; otherwise, it returns True if the value is not present. | x not in y |
When comparing the identities of two objects in Python, identity operators are used regardless of whether they point to the same memory space. Depending on the comparison result, these operators evaluate True or False.
Name of operator | About | Example |
is | If both operands refer to the same object, it returns True; if not, it returns False. | x is y |
is not | It returns True if the operands on both sides do not refer to the same object and False if they do. | x is not y |
Article written by Aakarsh Pandey, Team edSlash
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India