Sitemap
Seetharam Bethala

Seetharam Bethala

Python Software Developer | Recent College Graduate Seeking Entry-Level Programming Position | Certified Machine learning engineer

Operators in Python

5 min readAug 19, 2024

Operators play an essential role because they form the building blocks of any programming language; important operations can be carried out upon variables and values due to these operators. Python, like other programming languages, has a full set of operators used to do the routine tasks including arithmetic calculations, simple comparisons, and logical operations. We discuss in this section all kinds of operators available in Python and their usage.

1. Arithmetic Operators
And here are some arithmetic operators that perform basic mathematical operations on numeric values.

Addition (+): Adds two numbers.
a = 5
b = 3
result = a + b # Output: 8
Subtraction (-): Subtracts one number from another.
result = a — b # Output: 2
Multiplication (*): Multiplies two numbers.
result = a * b # Output: 15
Division (/): One number is divided by another, returning a floating-point result.
result = a / b # Output: 1.6666666666666667
Floor Division (//): Divides one number by another and returns the largest integer less than or equal to the quotient.
result = a // b # Output: 1
Modulus (%): Returns the remainder of the division.
result = a % b # Output: 2
Exponentiation (**): Raises one number to the power of another.
result = a ** b # Output: 125
2. Comparison Operators
Comparison operators operate on two values and provide a Boolean value (True or False).

Equal to (==): Compares if two values are the same.
result = (a == b) # Output: False
Not equal to (!=): Compares if two values are not the same.
result = (a != b) # Output: True
Greater than (>): Checks whether the value on the left is greater than that on the right.
result = (a > b) # Output: True
Less than (<): Checks whether the value on the left is less than the value on the right.
result = (a < b) # Output: False
Greater than or equals to (>=): It checks whether the value on the left is greater than or equals to the value on the right.
result = (a >= b) # Output: True
Less than or equals to (<=): It checks whether the value on the left is less than or equals to that on the right.
result = (a <= b) # Output: False
3. Logical Operators
Logical operators serve to connect conditional statements or, moreover, to evaluate logical conditions.

and: This gives a True return when both conditions are true.
result = (a > 1 and b < 5) # Output: True
or: This will give a Positive result if, at least, any one condition is True.
result = (a > 1 or b > 5) # Output: True
not: This will give true results if the condition is False.
result = not (a > 1) # Output: False
4. Assignment Operators
Assignment operators are to assign values to variables. They can also perform operations and then assign the result in the same variable.

Get Seetharam Bethala’s stories in your inbox

Join Medium for free to get updates from this writer.

=: Assigns the value on right to the variable on the left.
a = 10
+: =: Adds the value on right to the variable and then assigns the resultant in the same variable.
a += 5 # Equivalent to a = a + 5 -=: Decreases the value on the right from the variable and assigns it to the variable.

code a -= 3 # Equivalent to a = a — 3 *=: Multiplies the variable by the value on the right and assigns it to the variable.

code a *= 2 # Equivalent to a = a * 2
/: Divide the variable by the value to the right and store the result in the variable.
a /= 4 # Same as a = a / 4
%=: Return the modulus of the variable in regards to the value on the right, then assign the result into the variable.
a %= 3 # Same as a = a % 3
Exponentiation and assignment. Raises the variable to the power of the value on the right and associates this value to the variable.
a **= 2 # a becomes the square of a. Same as a = a ** 2
Floor division and assignment. Divides the variable by the value on the right, returns the int result of that operation, and binds the variable to the result.
a //= 3 # Produces an int result, a = a // 3
5. Bitwise Operators
Bitwise operators: operate on the binary representations of integers.

& (AND): Sets each bit to 1 if both bits are 1.
result = a & b # Output depends on binary values of a and b
| (OR): Sets each bit to 1 if one of the bits is 1.
result = a | b
^ (XOR): Sets each bit to 1 if only one of the bits is 1.
a ^ b
~ (NOT): Flips all the bits.
result = ~a
<< (Left Shift): Moves the bits on the left to the left by n places.
result = a << 2 # moves the bits of a left by 2 places
>> (Right Shift): Moves the bits on the right to the right by n places.
result = a >> 2 # Shifts the bits of a 2 positions to the right
6. Membership Operators
Membership operators test for membership in a sequence, such as strings, lists, or tuples.

in: Returns True if a specified value is found in the sequence.
fruits = [“apple”, “banana”, “cherry” ]
result = “apple” in fruits # Output: True
not in: Returns True if a specified value is not found in the sequence.
result = “orange” not in fruits # Output: True
7. Identity Operators
Use identity operators to compare the memory locations of two objects.

is: Returns True if both variables reference the same object.
a = [1, 2, 3]
b = a
result = (a is b) # Output: True
is not: Returns True if the variables do not reference the same object.
c = [1, 2, 3]
result = (a is not c) # Output: True
Summary
An understanding of operators in Python is at the very heart of programming effectively. Each type of operator serves either a specific function, and you can carry out a myriad of operations using them ranging from fundamental arithmetic to intricate logic expressions. Mastery of these operators will enable you to manipulate your data, compare values, and simply guide the flow of your program succinctly and easily.

--

--

Seetharam Bethala
Seetharam Bethala

Written by Seetharam Bethala

Python Software Developer | Recent College Graduate Seeking Entry-Level Programming Position | Certified Machine learning engineer