In this article, we will show some basic aspects of arithmetic operators, assignment operators, comparison operators, bitwise operators, identity operators, membership operators, logical operators and more in Python.
Overview
Operator Type | Example Usage | Description |
---|---|---|
Arithmetic Operators | + (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus) |
Perform basic mathematical operations. |
Comparison Operators | == (Equal to), != (Not equal to), > (Greater than), < (Less than), >= (Greater than or equal to), <= (Less than or equal to) |
Compare two values and return a boolean result. |
Logical Operators | && (Logical AND), ` |
|
Bitwise Operators | & (Bitwise AND), ` |
(Bitwise OR), ^(Bitwise XOR), ~(Bitwise NOT), <<(Left shift), >>` (Right shift) |
Assignment Operators | = (Assign value to variable), += (Add and assign), -= (Subtract and assign), etc. |
Assign values to variables. |
Increment/Decrement Operators | ++ (Increment by 1), -- (Decrement by 1) |
Used to increment or decrement the value of a variable by 1. |
Special Operators | sizeof() , & (Address-of operator), * (Pointer/Dereference operator), [] (Array indexing), () (Function call), etc. |
Perform specific tasks not covered by other categories. |
Table of Contents
- 1. Arithmetic Operators
- 2. Comparison Operators
- 3. Logical Operators
- 4. Bitwise Operators
- 5. Assignment Operators
- 6. Identity Operators
- 7. Membership Operators
1. Arithmetic Operators
Arithmetic operators are used with numerical values to perform basic mathematical operations: addition (+), subtraction (-), multiplication (*), division (/), modulus (%) and exponentiation (**).
# Multiplication
print(10 * 3) # Outputs 30
# Division
print(9 / 3) # Outputs 3.0
# Addition
print(10 + 3) # Outputs 13
# Subtraction
print(5 - 3) # Outputs 2
# Modulus
print(5 % 3) # Outputs 2
# Exponentiation
print(2 ** 3) # Outputs 8
2. Comparison Operators
Comparison operators are used to compare two values: greater than (>), less than (<), equal (==), not equal (!=), greater than or equal to (>=), and less than or equal to (<=).
# Greater than
print(10 > 3) # Outputs True
# Less than
print(10 < 3) # Outputs False
# Equal
print(10 == 3) # Outputs False
# Not equal
print(10 != 3) # Outputs True
# Greater than or equal to
print(10 >= 10) # Outputs True
# Less than or equal to
print(3 <= 3) # Outputs True
3. Logical Operators
Logic operators can be used to combine conditional expressions: and (&&), or (||), not (! ). Logical operators are often used in python for if conditions.
# And
print(False and True) # Outputs False
print(True and True) # Outputs True
print(True and True and False) # Outputs False
# Or
print(True or False) # Outputs True
print(False or False) # Outputs False
print(False or False or True) # Outputs True
# Not
print(not True) # Outputs False
4. Bitwise Operators
Bitwise operators are used to compare binary numbers: OR (|), XOR (^), AND (&), NOT (~), shift left (<<), and shift right (>>).
# OR
print(4 | 6) # Outputs 6
# XOR
print(6 ^ 3) # Outputs 5
# AND
print(6 & 3) # Outputs 2
# NOT
print(~5) # Outputs -6
# Shift left
print(3 << 1) # Outputs 6
# Shift right
print(3 >> 1) # Outputs 1
5. Assignment Operators
Values are assigned to variables using assignment operators: equal (=), plus equals (+=), minus equals (-=), etc.
x = 12
x += 3 # Equivalent to x = x + 3, which is now 15
print(x)
y = 9
y -= 3 # Equivalent to y = y - 3, which is now 6
print(y)
6. Identity Operators
Identity operators are used to compare objects, not whether they are the same, but whether they are in fact the same object or identical: is (is), is not (is not).
x = ["strawberry", "raspberry"]
y = ["strawberry", "raspberry"]
z = x
print(x is z) # Outputs True - because z is the same object as x
print(x is y) # Outputs False - although x and y are equal, they are not the same object.
7. Membership Operators
Membership operators are used to test whether a sequence (string, list, tuple, etc.) contains a specified element: in, not in.
x = ["strawberry", "apple"]
print("apple" in x) # Outputs True
print("potato" not in x) # Outputs True
These are just some of the basic aspects of Python operators. As you continue to learn and use Python, you’ll discover more complex mathematical operations and libraries that can help with data analysis, machine learning, and more. Happy coding!