DEV Community

Chidi E. Egwu
Chidi E. Egwu

Posted on

A Quick Overview of Python Operators

Operators
The Python programming language is equipped with several operators, each with its own set of applications and in this tutorial, we will evaluate the Python operators in no order of
importance.

1. Arithmetic Operators: These are used in conjunction with numeric values to perform mathematical operations, and they include
a. Addition a + b
b. Subtraction a - b
c. Multiplication a * b
d. Division a / b
e. Modulus a % b
f. Exponentiation ab**
g. Floor division a // b

2. Comparison Operators: These are used to compare two values, with one value on each side of the operator.
a. Equal a == b
b. Not equal a != b
c. Greater than a > b
d. Greater than or equal to a >= b
e. Less than a < b
f. Less than or equal to a <= b

3. Logical Operators: These are used to combine conditional statements; they are made up of three operators as shown below.
a. and: returns True if both conditions are true and False if one or both condition is false.
Firstly, we create two variables x and z, then assigned them the values of 5 and 3 respectively.

>>> x = 5
>>> z = 3
Enter fullscreen mode Exit fullscreen mode

Then we check if x is equal to 5 and z is less than 3, while the evaluation of x is true the evaluation of z is false, so it returns False as earlier explained, the same applied to the other expressions below.

>>> x == 5 and z < 3
False
>>> x == 5 and z == 3
True
>>> x > 6 and z < 4
False
>>> x > 6 and z <3
False
Enter fullscreen mode Exit fullscreen mode

b. or: unlike the true operator, the or operator only requires one of the criteria to be true to return True.

>>> a = 5
>>> b = 3
>>> a > 5 or b < 5
True
Enter fullscreen mode Exit fullscreen mode

c. not: this returns the opposite of the result, that is it returns False if the result is true.

>>> a = 5
>>> b = 3
>>> not(a > 5 or b < 5)
False
Enter fullscreen mode Exit fullscreen mode

4. Membership Operators: This is used to determine whether a sequence exists in an object.
a. in: returns True, if the sequence being tested for is present in the object and the check is also case sensitive for strings.

>>> greeting = "Good morning Chidi, hope you had a good day"
>>> "chidi" in greeting
False
>>> "Chidi" in greeting
True
Enter fullscreen mode Exit fullscreen mode

b. not in: This operator is the inverse of the first, returning True if the sequence being tested for is not present in the object and False otherwise. It is also case sensitive.

>>> "Chidi" not in greeting
False
>>> "chidi" not in greeting
True
Enter fullscreen mode Exit fullscreen mode

5. Identity Operator: The identity operator compares objects, not based on equality but on being the same object existing as the same in memory location. It is used to compare values of the same type or class.
a. is: this will return True if both variables are the same object.

>>> a = "chidi"
>>> b = "1"
>>> c = "chidi"
>>> a is b
False
>>> a is c
True
Enter fullscreen mode Exit fullscreen mode

b. is not: this will return True if both variables are not the same object.

>>> a = "chidi"
>>> b = "1"
>>> a is not b
True
Enter fullscreen mode Exit fullscreen mode

6. Assignment Operator: The assignment operator, which is used to assign values to variables, is still one of the most used operators in any programming language. Its usage is as shown below.
a. = the equal assignment variable is used to assign values to a variable as shown in previous examples.
b. +=, -=, *=, /= same as writing z = z + 2 assuming z was originally assigned the value of 1 the result will be 3. This basically means applies to all other assignment operators.
Examples:

>>> j = 3
>>> j *= 2
>>> print (j)
6

Enter fullscreen mode Exit fullscreen mode

Note : += and =+ while appearing similar operate and function in a different manner. i += 1 is the same as i = i + 1, while i =+ 1 is the same as i = (+1).
c. %= modulus operator returns the remainder of a division.
d. //= the floor division divides two numbers and rounds the result down to the nearest integer.
e. **= the exponent operator is used to raise the number on the left to the power of the exponent on the right.
f. |= carries out the logical OR operation of two operands Y | Z and returns the result to the first operand Y.
g. &= carries out the logical AND operation of two operands Y | Z and returns the result to the first operand Y.

NOTE: Though the remaining three ^=, >>=, and <<= are part of the assignment operator, a view of the bitwise operator creates a clearer picture of their application.

7. Bitwise Operators: These are used for binary numbers comparison.
a. & referred to as AND, functions by setting each bit to 1 if both bits are 1 and 0 otherwise.
b. | referred to as OR, functions by settings each bit to 1 if one of two bits is 1.
c. ^ referred to as XOR, but unlike the | operator, it sets each bit to 1 if only one of two bits is 1.
d. ~ referred to as NOT, this operator returns an Invert of all the bits.
e. << Zero fill left shift, this is a fancy way of saying that the positions vacated by the left shift are filled or replaced with 0 for unsigned (positive) numbers, but with 1 for signed (negative) numbers. That is 0 is used to replace positive numbers while 1 is used to replace negative numbers.
f. >> Signed right shift, like the previous shift explained, it assigned 0 bit from the left while removing the right-most bits. Meaning if you right shift the binary value 0101 you will get a result of 0010.

Summary
This article looks at the operators used in the Python programming language; some are similar to regular mathematics operators you may be familiar with, while others are completely new to you. Regardless, the purpose of this article is to give you a good standing if you decide to use any of the listed operators or learn more about them. The later operators, namely the bitwise operators and the last three assignment operators, will require a separate writeup to provide a more detailed explanation of how they work, but most importantly, I hope I was able to bring you up to speed on the Python programming language operators.

Top comments (0)