Python Operators

Explore the essentials of Python Operators, including arithmetic, logical, comparison, bitwise, assignment, identity, and membership operators, with examples.

Python, one of the most popular programming languages today, is known for its simplicity and readability. One fundamental aspect of Python that contributes to its ease of use is its operators. Operators are the constructs which can manipulate the value of operands. In this blog, we'll explore the different types of operators available in Python and their applications.

What Are Python Operators?

Operators in Python are special symbols or keywords that are used to perform operations on one or more operands. An operand can be a value, a variable, or an expression. Operators are the building blocks of Python programs and are used to perform tasks like arithmetic calculations, logical comparisons, and memory operations.

Types Of Python Operators

The various types of operators in python are:

  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Identity Operators and Membership Operators

Arithmetic Operators In Python

Arithmetic operators in Python are used to perform basic mathematical operations and are fundamental to any kind of numerical manipulation in Python programming. These operators include addition (+), subtraction (-), multiplication (*), division (/), modulus (%), exponentiation (**), and floor division (//).

Addition (+)

Adds two operands. For example.

result = 3 + 2
print(result)  # Output: 5

Subtraction (-)

Subtracts the right operand from the left operand.

result = 5 - 3
print(result)  # Output: 2

Multiplication (*)

Multiplies two operands.

result = 4 * 3
print(result)  # Output: 12

Division (/)

Divides the left operand by the right operand. The result is a floating-point number.

result = 8 / 2
print(result)  # Output: 4.0

Modulus (%)

Returns the remainder of the division.

result = 5 % 2
print(result)  # Output: 1

Exponentiation (**)

Raises the first operand to the power of the second operand.

result = 3 ** 2
print(result)  # Output: 9

Floor Division (//)

Performs division but discards the fractional part, returning an integer result.

result = 7 // 2
print(result)  # Output: 3

Understanding these arithmetic operators is essential for anyone beginning with Python, as they are used widely in various types of numerical calculations and data manipulation tasks.

Comparison Operators In Python

Comparison operators in Python are used to compare values. They evaluate to either True or False based on the condition. These operators are essential in decision-making processes in Python scripts.

Equal to (==)

Checks if the values of two operands are equal.

print(5 == 5)  # Output: True
print(5 == 3)  # Output: False

Not Equal to (!=)

Checks if the values of two operands are not equal.

print(5 != 3)  # Output: True
print(5 != 5)  # Output: False

Greater than (>)

Checks if the value of the left operand is greater than the right operand.

print(5 > 3)  # Output: True
print(3 > 5)  # Output: False

Less than (<)

Checks if the value of the left operand is less than the right operand.

print(3 < 5)  # Output: True
print(5 < 3)  # Output: False

Greater than or Equal to (>=)

Checks if the left operand is greater than or equal to the right operand.

print(5 >= 5)  # Output: True
print(4 >= 5)  # Output: False

Less than or Equal to (<=)

Checks if the left operand is less than or equal to the right operand.

print(3 <= 5)  # Output: True
print(5 <= 3)  # Output: False

Understanding these comparison operators is crucial for controlling the flow of a Python program, as they are commonly used in conditional statements and loops to evaluate conditions and make decisions.

Logical Operators In Python

Logical operators in Python are used to combine conditional statements, and they are fundamental in controlling the flow of logic in Python code. These operators include and, or, and not, each serving a specific purpose in evaluating conditions.

and Operator

The and operator returns True if both the operands (conditions) are true.

print((5 > 3) and (5 > 4))  # Output: True
print((5 > 3) and (5 < 4))  # Output: False

In this example, the first statement is true because both conditions are true, whereas the second statement is false. After all, one condition is false.

or Operator

The or operator returns True if at least one of the operands is true.

print((5 > 3) or (5 < 4))  # Output: True
print((5 < 3) or (5 < 4))  # Output: False

Here, the first statement is true because at least one condition (5 > 3) is true. The second statement is false as both conditions are false.

not Operator

The not operator reverses the result of the condition. It returns True if the condition is false and vice versa.

print(not(5 > 3))  # Output: False
print(not(5 < 3))  # Output: True

In these examples, not(5 > 3) returns False because 5 > 3 is true, and not(5 < 3) returns True because 5 < 3 is false.

Logical operators are integral in Python for building complex logical expressions, making them essential in decision-making structures like if-else statements and loops.

Bitwise Operators In Python

Bitwise operators in Python are used to perform operations on binary numbers at the bit level. These operators are essential for manipulating individual bits and performing bit-by-bit operations.

AND Operator (&)

The & operator performs a bitwise AND, where each bit of the output is 1 if both corresponding bits of the operand are 1.

Example.

print(5 & 3)  # Output: 1

In binary, 5 is 101 and 3 is 011. The bitwise AND of these numbers is 001, which is 1 in decimal.

OR Operator (|)

The | operator performs a bitwise OR, where each bit of the output is 1 if at least one of the corresponding bits of the operands is 1.

Example.

print(5 | 3)  # Output: 7

In binary, 5 is 101 and 3 is 011. The bitwise OR of these numbers is 111, which is 7 in decimal.

XOR Operator (^)

The ^ operator performs a bitwise XOR, where each bit of the output is 1 if the corresponding bits of the operands are different.

Example.

print(5 ^ 3)  # Output: 6

In binary, 5 is 101 and 3 is 011. The bitwise XOR of these numbers is 110, which is 6 in decimal.

NOT Operator (~)

The ~ operator performs a bitwise NOT, inverting each bit of the number.

print(~5)  # Output: -6

The bitwise NOT of 5 (101 in binary) is 010 in binary, which is -6 in decimal (considering the two's complement representation).

Shift Operators

Left Shift (<<): Shifts the bits of the number to the left and fills 0 on voids left as a result. The left operand specifies the value to be shifted, and the right operand specifies the number of positions to shift.

print(5 << 1)  # Output: 10

5 in binary is 101. Shifting it one position to the left results in 1010, which is 10 in decimal.

Right Shift (>>): Shifts the bits of the number to the right. The left operand specifies the value to be shifted, and the right operand specifies the number of positions to shift.

print(5 >> 1)  # Output: 2

5 in binary is 101. Shifting it one position to the right results in 010, which is 2 in decimal.

Bitwise operators are particularly useful in low-level programming, such as device driver writing or protocol development, where manipulation of data at the bit level is crucial.

Assignment Operator In Python

Assignment operators in Python are used to assign values to variables. These operators make the code more concise and improve its readability by combining arithmetic operations with assignment.

The Basic Assignment Operator (=)

The = operator assigns the value on the right to the variable on the left.

Example.

x = 10
print(x)  # Output: 10

Compound Assignment Operators

These operators combine assignment with other operations.

Add and Assign (+=): Adds the right operand to the left operand and assigns the result to the left operand.

x = 5
x += 3  # Same as x = x + 3
print(x)  # Output: 8

Subtract and Assign (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.

x = 5
x -= 3  # Same as x = x - 3
print(x)  # Output: 2

Multiply and Assign (*=): Multiplies the right operand with the left operand and assigns the result to the left operand.

x = 5
x *= 3  # Same as x = x * 3
print(x)  # Output: 15

Divide and Assign (/=): Divides the left operand by the right operand and assigns the result to the left operand.

x = 10
x /= 2  # Same as x = x / 2
print(x)  # Output: 5.0

Modulus and Assign (%=), Exponent and Assign (=)**, and Floor Division and Assign (//=) work similarly, performing their respective operations and then assigning the result to the left operand.

Assignment operators are essential in Python for variable manipulation, simplifying expressions, and making code more efficient and easier to understand.

Identity Operators And Membership Operators In Python

Identity and Membership operators in Python are used to compare the identity of objects and check for membership within data structures, respectively.

Identity Operators

Identity operators compare the memory locations of two objects. There are two identity operators in Python: is and is not.

is: Evaluates to true if the variables on either side of the operator point to the same object.

Example.

x = ["apple", "banana"]
y = x
print(x is y)  # Output: True

is not: Evaluates to true if the variables on either side of the operator do not point to the same object.

Example.

x = ["apple", "banana"]
y = ["apple", "banana"]
print(x is not y)  # Output: True

Membership Operators

Membership operators test if a sequence is presented in an object. The two membership operators in Python are in and not in.

in: Returns True if a sequence with the specified value is present in the object.

Example:

x = ["apple", "banana"]
print("banana" in x)  # Output: True

not in: Returns True if a sequence with the specified value is not present in the object.

Example.

print("cherry" not in x)  # Output: True

Identity and Membership operators are crucial in Python for validating the identity of objects and checking for the presence of elements within various data structures like lists, tuples, and strings. They are widely used in conditional statements and loops to enhance the logic and functionality of Python scripts.

Operators in Python are essential for performing various operations on data. Understanding these operators and their applications is crucial for anyone looking to master Python programming. Whether it's for data manipulation, arithmetic calculations, or logical comparisons, Python operators provide a simple yet powerful tool for developers.

You can also check these blogs:

  1. Ternary Operator In Python