Introduction to Python Operators
When it comes to programming, variables and constants are nouns, while operators are the verbs. Operators are the special keywords or symbols used to instruct the interpreter to perform a series of mathematical or logical operations.
Whether it is calculating the trajectory of a rocket or checking whether a username is already taken, operators are the tools of choice when it comes to programming in Python. Learn what is operators in Python with our Python tutorial for beginners.
1. Arithmetic Operators in Python
Arithmetic operators are used to perform mathematical operations on numeric values. These are the most basic operators used by any programming language and are the ones most people are accustomed to since they were first introduced in elementary math.
| Operator | Name | Description | Example (a=10,b=3) |
| + | Addition | Adds two operands | a+b=13 |
| – | Subtraction | Subtracts the right operand from the left | a−b=7 |
| * | Multiplication | Multiplies two operands | a∗b=30 |
| / | Division | Divides left operand by right (results in float) | a/b=3.33 |
| % | Modulus | Returns the remainder of the division | a%b=1 |
| ** | Exponentiation | Left operand raised to the power of right operand | a∗∗b=1000 |
| // | Floor Division | Division that results in a whole number (downward) | a//b=3 |
Code Example:
x = 15
y = 4
print(f”Addition: {x + y}”) # 19
print(f”Modulus: {x % y}”) # 3 (15 divided by 4 leaves 3)
print(f”Floor Division: {x // y}”) # 3 (15 / 4 is 3.75, floor is 3)
2. Comparison / Relational Operators in Python
Comparison operators are used to perform comparisons between two values. The result of a comparison operation is always a Boolean value.
| Operator | Name | Example |
| == | Equal | x == y |
| != | Not equal | x != y |
| > | Greater than | x > y |
| < | Less than | x < y |
| >= | Greater than or equal to | x >= y |
| <= | Less than or equal to | x <= y |
3. Logical Operators in Python
Logical operators in Python are required to link two or more condition statements. They are extremely important for decision-making in your code.
- and: Returns True if both statements are True.
- or: Returns True if at least one of the statements is True.
- not: Returns the opposite of the statement.
Code Example:
age = 25
has_license = True
# Logical AND
if age >= 18 and has_license:
print(“You can drive.”)
# Logical NOT
is_weekend = False
if not is_weekend:
print(“Time to go to work!”)
Practice with more real-time examples through our Python project ideas.
4. Assignment Operators in Python
Assignment operators are required to assign a value to a variable. The basic assignment operator is the equals sign (=), but Python has “augmented” assignment operators.
| Operator | Equivalent to |
| += | x = x + 3 |
| -= | x = x – 3 |
| *= | x = x * 3 |
| /= | x = x / 3 |
| %= | x = x % 3 |
| //= | x = x // 3 |
5. Bitwise Operators in Python
These are operators that perform a specific action on the “bits” (the 1s and 0s) of the number.
| Operator | Name | Description |
| & | AND | Sets each bit to 1 if both bits are 1 |
| | | OR | Sets each bit to 1 if one of two bits is 1 |
| ^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
| ~ | NOT | Inverts all the bits |
| << | Zero fill left shift | Shift left by pushing zeros in from the right |
| >> | Signed right shift | Shift right by pushing copies of the leftmost bit |
These operators are less commonly used in high-level web dev but are extremely important for things like cryptography and graphics.
6. Identity and Membership Operators
These operators are unique to Python and make Python a very readable language.
Identity Operators
These are used to compare two variables, not for equality but for sameness.
- is: This returns True if the two variables are the same.
- is not: This returns True if the two variables are not the same.
Membership Operators
These are used to compare a sequence (like a string, list, etc.) and whether it is present in an object.
- in: This returns True if a sequence containing the specified value is present.
- not in: This returns True if the value is not present.
Code Example:
fruits = [“apple”, “banana”, “cherry”]
print(“banana” in fruits) # True
print(“orange” not in fruits) # True
# Identity Example
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a is c) # True (c points to the same list as a)
print(a is b) # False (they have the same content, but are different objects)
Enroll in our Python Training in Chennai for comprehensive learning.
Operator Precedence
Just as we have a specific order for mathematical calculations (PEMDAS), Python has a specific order for calculations as well. In other words, multiplication comes before addition.
- Parentheses ()
- Exponentiation **
- Multiplication, Division, Floor Division, Modulus *, /, //, %
- Addition, Subtraction +, –
- Bitwise shifts <<, >>
- Comparisons >, <, ==, etc.
- Logical NOT not
- Logical AND and
- Logical OR or
Conclusion
As long as you can understand what are operators in Python, you can perform a wide variety of complex logic. From basic addition to determining whether a certain member is present in a list, you can use the tools provided by the Python basic operators to transform raw information into a more meaningful form.
With the help of the basic operators in Python with examples, you can control the flow of your programs and perform complex mathematical calculations without writing a lot of code. Explore more courses by visiting our software training institute in Chennai.
