Skip to content

Latest commit

 

History

History
83 lines (66 loc) · 2.06 KB

operators.md

File metadata and controls

83 lines (66 loc) · 2.06 KB

In Python, operators are special symbols that carry out arithmetic or logical computation. Here's a brief overview of the main types of operators in Python with examples:

  1. Arithmetic Operators:

    • Used to perform mathematical operations like addition, subtraction, multiplication, etc.
    • Example:
      # Addition
      print(5 + 3)  # Output: 8
      
      # Subtraction
      print(5 - 3)  # Output: 2
      
      # Multiplication
      print(5 * 3)  # Output: 15
      
      # Division
      print(5 / 3)  # Output: 1.666...
  2. Comparison Operators:

    • Used to compare values. It either returns True or False according to the condition.
    • Example:
      # Equal to
      print(5 == 3)  # Output: False
      
      # Not equal to
      print(5 != 3)  # Output: True
      
      # Greater than
      print(5 > 3)  # Output: True
  3. Logical Operators:

    • Used to combine conditional statements.
    • Example:
      # and operator
      print((5 > 3) and (5 < 10))  # Output: True
      
      # or operator
      print((5 > 3) or (5 > 10))  # Output: True
      
      # not operator
      print(not(5 > 3))  # Output: False
  4. Assignment Operators:

    • Used to assign values to variables.
    • Example:
      # Simple assignment
      x = 5
      print(x)  # Output: 5
      
      # Add and assign
      x += 3
      print(x)  # Output: 8
  5. Membership Operators:

    • Used to test whether a value or variable is found in a sequence (string, list, tuple, set, and dictionary).
    • Example:
      # in operator
      print('a' in 'apple')  # Output: True
      
      # not in operator
      print('b' not in 'apple')  # Output: True
  6. Bitwise Operators:

    • Used to perform bitwise calculations on integers.
    • Example:
      # Bitwise AND
      print(5 & 3)  # Output: 1
      
      # Bitwise OR
      print(5 | 3)  # Output: 7

These examples should give you a good understanding of how different operators work in Python. Feel free to experiment with them to see how they operate! 🐍✨