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:
-
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...
-
Comparison Operators:
- Used to compare values. It either returns
True
orFalse
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
- Used to compare values. It either returns
-
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
-
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
-
Membership Operators:
- Used to test whether a value or variable is found in a sequence (
string
,list
,tuple
,set
, anddictionary
). - Example:
# in operator print('a' in 'apple') # Output: True # not in operator print('b' not in 'apple') # Output: True
- Used to test whether a value or variable is found in a sequence (
-
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! 🐍✨