Operators in programming are symbolic constructs used to perform different operations on variables and values.
These operations can be arithmetic, comparison, assignment, logical, etc.
x = 1 y = 4 z = x + y
In the example above,
y are variables assigned 1 and 4 respectively using the
= operator. Afterward, we use the
+ operator to add together
y and assign the outcome to
In Python programming language, operators are grouped into different categories based on their functions.
Arithmetic operators are used for basic mathematical operations on numeric values or variables bearing numeric values.
|+||Addition||x + y||Adds x and y.|
|-||Subtraction||x - y||Subtracts y from x.|
|*||Multiplication||x * y||Multiply x and y.|
|**||Exponent||x ** y||Raises x to the power of y.|
|/||Division||x / y||Divide x by y and returns the result.|
|%||Modulus||x % y||Divide x by y and returns the remainder.|
|//||Floor Division||x // y||Divides x by y and rounds the result down to the nearest whole number. If one of the operands (x or y) is negative, the result is rounded towards negative infinity.|
These operators are used on numbers for comparison.
During the comparison, they treat numbers as strings of binary digits (bits), written in twos-complement binary, rather than a single numeric value.
To solve the examples in the table below, let x = 7 (0000 0111 in binary form) and y = 2 (0000 0010 in binary form).
|&||Bitwise AND||x & y||Performs a "bitwise and". Sets each bit to 1 if the bit of both |
||||Bitwise OR||x | y||Performs a "bitwise or". Sets each bit to 1 if either or both bit of |
|^||Bitwise XOR||x ^ y||Performs a "bitwise exclusive or". Sets each bit to 1 if |
|~||Bitwise NOT||~x||Performs a "bitwise not". Inverts the bit of |
|>>||Bitwise Right Shift||x >> y||Performs a "bitwise right shift". Shifts the bits of |
|<<||Bitwise Left Shift||x << y||Performs a "bitwise left shift". Shifts the bits of |
To assign values to variables in python programming, you make use of assignment operators.
|=||Assign||x = 7||Assigns the value, 7, to the variable |
|+=||Add and Assign||x += 2||Same as x = x + 2. Adds 2 to the variable |
|-=||Subtract and Assign||x -= 2||Same as x = x - 2. Subtracts 2 from the variable |
|*=||Multiply and Assign||x *= 2||Same as x = x * 2. Multiplies the variable |
|**=||Exponent and Assign||x **= 2||Same as x = x ** 2. Raises the variable |
|/=||Divide and Assign||x /= 2||Same as x = x / 2. Divides the variable |
|%=||Modulus and Assign||x %= 2||Same as x = x % 2. Divides the variable |
|//=||Floor Division and Assign||x //= 2||Same as x = x // 2. Performs floor division on the variable |
|&=||Bitwise AND and Assign||x &= 2||Same as x = x & 2. Performs Bitwise AND to the variable |
||=||Bitwise OR and Assign||x |= 2||Same as x = x | 2. Performs Bitwise OR to the variable |
|^=||Bitwise Exclusive OR and Assign||x ^= 2||Same as x = x ^ 2. Performs Bitwise Exclusive OR to the variable |
|<<=||Binary Left Shift and Assign||x <<= 2||Same as x = x << 2. Performs Bitwise Binary Left Shift to the variable |
|>>=||Binary Right Shift and Assign||x >>= 2||Same as x = x >> 2. Performs Bitwise Binary Right Shift to the variable |
Comparison operators are used to compare two or more values, statements, variables etc.
|==||Equal||x == y||Checks if value of x equals y.|
|!=||Not Equal||x != y||Checks if value of x is not equal to y.|
|>||Greater Than||x > y||Checks if value of x is greater than y.|
|<||Less Than||x < y||Checks if value of x is less than y.|
|>=||Greater Than||x >= y||Checks if value of x is greater than or equal to y.|
|<=||Less Than||x <= y||Checks if value of x is less than or equal to y.|
These operators are used to combine two or more statements. They can be used to compare statements to make a decision based on an outcome.
Logical operators are the
|and||Logical AND||x == a and x == b||Returns |
|or||Logical OR||x == a or x == b||Returns |
|not||Logical NOT||not(x == a)||Checks if |
There some special operators used particularly in Python programming language that is worth mentioning.
These operators are the identity operator and the membership operator./p>
Identity operators are used to compare two values or variables by checking if they are located on the same part of the memory.
Two variables or values that are similar does not mean that they are identical.
Unlike the comparison operators which check if two values are similar using their form, the identity operator checks if they are identical using their memory location.
Identity operators are the
is not operators.
|is||Is same object||x is y||Returns |
|is not||Is Not same object||x is not y||Returns |
Membership operators are used to check if a value or variable is found in a sequence such as
In the case of a
dictionary, Python allows us to only test the key and not the value.
Identity operators are the
not in operators.
|in||In object||x in y||Returns |
|not in||Not In object||x not in y||Returns |
Precedence of operators is the sequence in which operators will be processed and evaluated when used in a mathematical expression.
It is similar to the "BODMAS" and "PEMDAS" used in mathematics.
The operators are evaluated from the highest to the lowest. The operator(s) with the highest precedence evaluated first followed by those with the lowest.
For example, x = 2 + 3 * 2; here,
x is assigned 8, not 10 because the operator
* has higher precedence than
+, so it first multiplies 3 * 2 and then is added to 2.
The table below shows the precedence of operators from highest to lowest. In python programming, some operators are ranked the same.
|2||~ + -||Complement, unary plus and minus (method names for the last two are [email protected] and [email protected])|
|3||* / % //||Multiply, divide, modulo and floor division|
|4||+ -||Addition and Subtraction|
|5||>> <<||Right and left bitwise shift|
|7||^|||Bitwise exclusive 'OR' and regular 'OR'|
|8||<= < > >=||Comparison operators|
|9||<> == !=||Equality operators|
|10||= %= /= //= -= += *= **=||Assignment operators|
|11||is is not||Comparison operators|
|12||in not in||Membership operators|
|13||not or and||Logical operators|
Operators in programming are used to perform different operations on variables and values. These operations can be arithmetic, comparison, assignment, logical, etc.
In Python programming language, operators are grouped into different categories: Arithmetic operators, Bitwise operators, Assignment operators, Comparison operators, Logical operators, and Special operators like Identity operators and Membership operators.
If you run into errors or unable to complete this tutorial, feel free to contact us anytime, and we will instantly resolve it. You can also request clarification, download this tutorial as a pdf, or report bugs using the buttons below.