Operators in Python

 

Operators in Python

Data Science and A.I. Lecture Series

Author: Bindeshwar Singh Kushwaha | Institute: PostNetwork Academy

Introduction

Operators in Python are special symbols that perform computations on operands. Python provides various types of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Membership Operators
  • Identity Operators

Arithmetic Operators


a = 10
b = 5
print(a + b)  # Addition
print(a - b)  # Subtraction
print(a * b)  # Multiplication
print(a / b)  # Division
print(a % b)  # Modulus
print(a ** b) # Exponentiation
        

Relational Operators


x = 10
y = 5
print(x > y)  # Greater than
print(x < y) # Less than print(x == y) # Equal to print(x != y) # Not equal to print(x >= y) # Greater than or equal to
print(x <= y) # Less than or equal to
        

Logical Operators


x = True
y = False
print(x and y)  # Logical AND
print(x or y)   # Logical OR
print(not x)    # Logical NOT
        

Bitwise Operators


a = 5  # 0101 in binary
b = 3  # 0011 in binary
print(a & b)  # Bitwise AND
print(a | b)  # Bitwise OR
print(a ^ b)  # Bitwise XOR
print(a << 1) # Left shift print(a >> 1) # Right shift
        

Assignment Operators


x = 10
x += 5  # Equivalent to x = x + 5
x -= 3  # Equivalent to x = x - 3
x *= 2  # Equivalent to x = x * 2
x /= 2  # Equivalent to x = x / 2
print(x)
        

Membership Operators


my_list = [1, 2, 3, 4, 5]
print(3 in my_list)  # True
print(6 not in my_list)  # True
        

Identity Operators


a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b)  # True
print(a is c)  # False
print(a == c)  # True
        

Summary

  • Operators: Arithmetic, Relational, Logical, Bitwise, Assignment, Membership, Identity.
  • Lists: Ordered, mutable collections.
  • Tuples: Ordered, immutable collections.
  • Dictionaries: Key-value pairs.
  • Sets: Unordered, unique elements.
  • File Handling: Reading/Writing files.

 

 

0 views

©Postnetwork-All rights reserved.