Python Operators (Tutorial)

Python Operators

This tutorial of Python Operators illustrates the Arithmetic, Assignment, Comparision, Logical, Identity, Membership, and Bitwise operators with examples.

Python Operators

In any programming language, operators are used for performing different kinds of operations on the variables and the values. Python allows us to use an extensive set of operators. Python Operators can be divided into seven different categories that are listed below.

  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparison Operators
  4. Logical Operators
  5. Identity Operators
  6. Membership Operators
  7. Bitwise Operators

We’ll learn about the variables of each of these categories one by one.

Arithmetic Operators

The operators that are used to perform mathematical calculations on different values and variables are known as Arithmetic operators. As they are arithmetic operators, so they’re usually used for numerical values and the variables storing those numerical values. The different arithmetic operators available in python for simple mathematical calculations are listed below in tabular form and are well described with examples.

Arithmetic Operator Name Example
+ Addition
* Multiplication
/ Division
% Modulus
** Exponentiation
// Floor Division

Example. The following code illustrates the use of all of the above-defined arithmetic operators.

Python Arithmetic Operators Example

Assignment Operators

As the name suggested, the assignment operators are used to assign values to the variables. But assignment operators are also used to implicitly perform operations first on the assigned values and then storing the value into the variable. = is the only assignment operator that assigns the value as it is in the variable, all of the other assignment operators are used to first perform some specific operation on the value and then storing the value into the variable. Given below is the tabular representation of the different assignment operators with examples.

Assignment Operator Example Similar To
= a = 5
+= a += 3
-= a -= 3
*= a *= 3
/= a /= 3
%= a %= 3
//= a //= 3
**= a **= 3
&= a &= 3
|= a |= 3
^= a ^= 3
>>= a >>= 3
<<= a <<= 3

Example. The following snippet of code illustrates the use of all of the above listed assignment operators.

Python Assignment Operators Example

Comparison Operators

Comparison Operators are used to comparing two values at a time. The values need not just be numerical, these operators can also be applied to other python objects and even can be used for things like Python Strings Comparison. The following table lists the different comparison operators.

Comparison Operator Name Example
== Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Note. All of the comparison operators return Boolean value i.e. either True or False.

Example. Although these operators can be used on different python objects yet, for example, we’ve illustrated the use of comparison operators only on the numerical values type variables.

Python Comparison Operators Example

Logical Operators

Logical Operators also return a Boolean value i.e. either True or False. They are used to check the result of two different boolean resultant expressions. In other words, they’re used to check if two or more conditions that are going to return boolean values are mutually True or False. There are only three logical operators in python that are listed below in the table.

Logical Operator Description Example
and It returns True if both the statements are True.
or It returns True if any one of the statements are True.
not It returns True if a statement is not true.

Example. The following example illustrates the use of these logical operators.

Python Logical Operators Example

Identity Operators

Identity operators are used to checking if two different Python objects are exactly the same. Here the same does not mean equal, rather the same here means if two objects are exactly the same with the same memory location. Do not confuse it with equal and not equal operators. There are only two identity operators that are listed in the following table. Both of these operators return only boolean values.

Identity Operator Description Example
is It will only return True if two python objects are exactly the same objects.
is not It will return True if two python objects are not exactly the same objects.

Example. Observe the following example carefully to understand the use of identity operators.

Python Identity Operators Example

Membership Operators

Membership Operators are used to check if a value is a member of a sequence or not. These are one of the most helpful operators in python and similar checks in other programming languages required extra coding efforts. The two membership operators available in python are defined below in the tabular form. Both of these variables will return boolean values.

Membership Operator Description Example
in returns True if a value is in the sequence
not in returns True if a value is not in the sequence

Example. To illustrate the usage of the membership operators, we’ve provided a code snippet below that checks if the value is present in the list or not.

Python Membership Operators Example

Bitwise Operators

The operators that are used to perform operators bit by bit on the binary value of the actual value are known as Bitwise operators. For example, if the value is 5, then its binary form is 101, which includes three bits, two ones and one zero. Each individual bit will be involved in bitwise operations. There are six different bitwise operators that are listed below.

Bitwise Operator Name Description
& AND Bitwise AND operation i.e. resultant bit will be 1 if bits from both the values are 1.
| OR Bitwise OR Operation i.e. resultant bit ill be 1 if anyone bit from both of the values is 1.
^ XOR Bitwise XOR operation. It will set the resultant bit to 1 if only one of the bits from both of the values will be 1.
~ NOT Bitwise NOT operation. It will simply invert all of the bits.
<< ZERO FILL LEFT SHIFT The resultant bits will be shifted to left by pushing zeros in from the right and the leftmost bit will be fall off.
>> ZERO FILL RIGHT SHIFT The resultant bits will be shifted to the right by pushing leftmost bit copies to the left and rightmost bit will fall off.

Example. The following example illustrates the use of bitwise operators in python.

Python Bit Wise Operators Example

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *