1.6 Types of Operators in Python

 Python offers a variety of operators to manipulate data and control program flow. Here's a comprehensive overview:

Arithmetic Operators

Used for performing basic mathematical operations.

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • //: Floor division (returns integer quotient)
  • %: Modulus (returns remainder)
  • **: Exponentiation

Comparison (Relational) Operators

Used to compare values and return Boolean results (True or False).

  • ==: Equal to
  • !=: Not equal to
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

Assignment Operators

Used to assign values to variables.

  • =: Simple assignment
  • +=: Add and assign
  • -=: Subtract and assign
  • *=: Multiply and assign
  • /=: Divide and assign
  • //=: Floor divide and assign
  • %=: Modulus and assign
  • **=: Exponentiate and assign   
  • &=: Bitwise AND and assign
  • |=: Bitwise OR and assign
  • ^=: Bitwise XOR and assign
  • >>: Bitwise right shift and assign
  • <<: Bitwise left shift and assign   

Logical Operators

Used to combine conditional statements.

  • and: Returns True if both operands are True
  • or: Returns True if at least one operand is True
  • not: Reverses the logical state of its operand

Bitwise Operators

Operate on individual bits of their operands.

  • &: Bitwise AND
  • |: Bitwise OR
  • ^: Bitwise XOR
  • ~: Bitwise NOT
  • <<: Left shift
  • >>: Right shift

Membership Operators

Used to check for membership in sequences (lists, tuples, strings, sets, dictionaries).

  • in: Returns True if a value is present in a sequence
  • not in: Returns True if a value is not present in a sequence

Identity Operators

Used to compare object identities (memory locations).

  • is: Returns True if two variables refer to the same object
  • is not: Returns True if two variables refer to different objects

Ternary Operator (Conditional Expression)

A concise way to write conditional statements.

Python
value_if_true if condition else value_if_false
Previous Post Next Post