Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Types of Operators in Python


Operators in Python are special symbols or keywords used to perform operations on variables and values. Python provides a variety of operators that can be categorized into different types. This article covers the main types of operators with examples.

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.

          
            # Examples of Arithmetic Operators
            a = 10
            b = 3
            print(a + b)  # Addition: 13
            print(a - b)  # Subtraction: 7
            print(a * b)  # Multiplication: 30
            print(a / b)  # Division: 3.333...
            print(a % b)  # Modulus: 1
            print(a ** b) # Exponentiation: 1000
            print(a // b) # Floor Division: 3
          
        

2. Assignment Operators

Assignment operators are used to assign values to variables.

          
            # Examples of Assignment Operators
            x = 5
            x += 3  # Equivalent to x = x + 3
            print(x)  # 8
            x *= 2  # Equivalent to x = x * 2
            print(x)  # 16
          
        

3. Comparison Operators

Comparison operators are used to compare two values and return a boolean result.

          
            # Examples of Comparison Operators
            a = 10
            b = 5
            print(a == b)  # False
            print(a != b)  # True
            print(a > b)   # True
            print(a <= b)  # False
          
        

4. Logical Operators

Logical operators are used to combine conditional statements.

          
            # Examples of Logical Operators
            a = True
            b = False
            print(a and b)  # False
            print(a or b)   # True
            print(not a)    # False
          
        

5. Bitwise Operators

Bitwise operators are used to perform operations at the binary level.

          
            # Examples of Bitwise Operators
            a = 5  # 0101 in binary
            b = 3  # 0011 in binary
            print(a & b)  # 1 (0001 in binary)
            print(a | b)  # 7 (0111 in binary)
            print(a ^ b)  # 6 (0110 in binary)
            print(~a)     # -6 (inverts bits)
            print(a << 1) # 10 (shifts bits left)
            print(a >> 1) # 2 (shifts bits right)
          
        

6. Membership Operators

Membership operators are used to check whether a value is a member of a sequence (like a list, string, or tuple).

          
            # Examples of Membership Operators
            my_list = [1, 2, 3, 4]
            print(2 in my_list)      # True
            print(5 not in my_list)  # True
          
        

7. Identity Operators

Identity operators are used to compare the memory addresses of two objects.

          
            # Examples of Identity Operators
            a = 10
            b = 10
            print(a is b)       # True
            print(a is not b)   # False
          
        

Conclusion

Python provides a rich set of operators to perform various operations on data. Understanding these operators is fundamental to writing efficient and effective Python programs. By mastering these operators, you can handle calculations, comparisons, logical operations, and more with ease.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java