Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. Python Operators

Python Operators

Author image

Harsh Pandey

Software Developer

Published on Tue Mar 19 2024

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<br>print(result) # Output: 5
  • Subtraction (-)
    Subtracts the right operand from the left operand.
    result = 5 - 3<br>print(result) # Output: 2
  • Multiplication (*)
    Multiplies two operands.
    result = 4 * 3<br>print(result) # Output: 12
  • Division (/)
    Divides the left operand by the right operand. The result is a floating-point number.
    result = 8 / 2<br>print(result) # Output: 4.0
  • Modulus (%)
    Returns the remainder of the division.
    result = 5 % 2<br>print(result) # Output: 1
  • Exponentiation (**)
    Raises the first operand to the power of the second operand.
    result = 3 ** 2<br>print(result) # Output: 9
  • Floor Division (//)
    Performs division but discards the fractional part, returning an integer result.
    result = 7 // 2<br>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<br>print(5 == 3) # Output: False
  • Not Equal to (!=)
    Checks if the values of two operands are not equal.
    print(5 != 3) # Output: True<br>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<br>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<br>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<br>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<br>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<br>print((5 > 3) and (5 < 4)) # Output: 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<br>print((5 < 3) or (5 < 4)) # Output: 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<br>print(not(5 < 3)) # Output: True

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.
    print(5 & 3) # Output: 1
  • 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.
    print(5 | 3) # Output: 7
  • 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.
    print(5 ^ 3) # Output: 6
  • NOT Operator (~)
    The ~ operator performs a bitwise NOT, inverting each bit of the number.
    print(~5) # Output: -6
  • 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
    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

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.
    x = 10<br>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<br>x += 3 # Same as x = x + 3<br>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<br>x -= 3 # Same as x = x - 3<br>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<br>x *= 3 # Same as x = x * 3<br>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<br>x /= 2 # Same as x = x / 2<br>print(x) # Output: 5.0

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.
    x = ["apple", "banana"]<br>y = x<br>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.
    x = ["apple", "banana"]<br>y = ["apple", "banana"]<br>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.
    x = ["apple", "banana"]<br>print("banana" in x) # Output: True
  • not in: Returns True if a sequence with the specified value is not present in the object.
    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.

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.