site logo

Learn to Python. Build the Future.


Category: (All)
❮  Go Back

11. Understanding Python Operators: A Comprehensive Guide

Introduction

In Python, operators are special symbols or keywords used to perform operations on values and variables. They are essential for manipulating data and controlling the flow of a program. Python supports a variety of operators, which can be categorized into several groups.


1. Arithmetic Operators

Arithmetic operators are used with numeric values to perform common mathematical operations:

  1. + : Addition
  2. - : Subtraction
  3. * : Multiplication
  4. / : Division
  5. % : Modulus (remainder)
  6. ** : Exponentiation
  7. // : Floor division (quotient without remainder)

Example:


x = 10
y = 5
print(x + y) # Output: 15
print(x - y) # Output: 5
print(x * y) # Output: 50
print(x / y) # Output: 2.0
print(x % y) # Output: 0
print(x ** y) # Output: 100000
print(x // y) # Output: 2


2. Assignment Operators

Assignment operators are used to assign values to variables:

  1. = : Assign
  2. += : Add and assign
  3. -= : Subtract and assign
  4. *= : Multiply and assign
  5. /= : Divide and assign
  6. %= : Modulus and assign
  7. //= : Floor divide and assign
  8. **= : Exponentiate and assign
  9. &= : Bitwise AND and assign
  10. |= : Bitwise OR and assign
  11. ^= : Bitwise XOR and assign
  12. >>= : Bitwise right shift and assign
  13. <<= : Bitwise left shift and assign
  14. := : Assignment expression (Python 3.8+)

Example:


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


3. Comparison Operators

Comparison operators are used to compare two values:

  1. == : Equal
  2. != : Not equal
  3. > : Greater than
  4. < : Less than
  5. >= : Greater than or equal to
  6. <= : Less than or equal to

Example:


x = 10
y = 5
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: True
print(x < y) # Output: False
print(x >= y) # Output: True
print(x <= y) # Output: False


4. Logical Operators

Logical operators are used to combine conditional statements:

  1. and : Returns True if both statements are true
  2. or : Returns True if one of the statements is true
  3. not : Reverses the result, returns False if the result is true

Example:


x = 10
y = 5
print(x > 5 and y < 10) # Output: True
print(x > 5 or y > 10) # Output: True
print(not(x > 5 and y < 10)) # Output: False


5. Identity Operators

Identity operators are used to compare objects, not if they are equal, but if they are actually the same object, with the same memory location:

  1. is : Returns True if both variables are the same object
  2. is not : Returns True if both variables are not the same object

Example:


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


6. Membership Operators

Membership operators are used to test if a sequence is presented in an object:

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

Example:


x = ['apple', 'banana']
print('apple' in x) # Output: True
print('cherry' not in x) # Output: True


7. Bitwise Operators

Bitwise operators are used to compare (binary) numbers:

  1. & : AND
  2. | : OR
  3. ^ : XOR
  4. ~ : NOT
  5. << : Zero fill left shift
  6. >> : Signed right shift

Example:


x = 10 # 1010 in binary
y = 4 # 0100 in binary
print(x & y) # Output: 0
print(x | y) # Output: 14
print(x ^ y) # Output: 14
print(~x) # Output: -11
print(x << 2) # Output: 40
print(x >> 2) # Output: 2


Conclusion

Understanding Python operators is essential for performing operations on variables and values. By mastering these operators, you can write more efficient and effective Python code.

11. Understanding Python Operators A Comprehensive Guide

coldshadow44 on 2025-10-08



(0)





Showing comments related to this post.




Member's Sites: