Learning Guides
Operators in Python: Complete Guide with Examples
Quick answer: Learn Python operators including arithmetic, comparison, logical, assignment, identity and membership operators, with examples and operator precedence.
Arithmetic Operators
a, b = 10, 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333... (true division, always returns a float)
print(a // b) # 3 (floor division, discards the remainder)
print(a % b) # 1 (modulus, the remainder)
print(a ** b) # 1000 (exponentiation)
Comparison Operators
print(5 == 5) # True
print(5 != 3) # True
print(5 > 3) # True
print(5 <= 5) # True
Logical Operators
age = 25
has_id = True
print(age >= 18 and has_id) # True, both must be true
print(age < 18 or has_id) # True, at least one is true
print(not has_id) # False
Assignment Operators
x = 10
x += 5 # same as x = x + 5, x is now 15
x -= 3 # x is now 12
x *= 2 # x is now 24
x //= 5 # x is now 4
Identity Operators: is vs ==
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == c) # True, same values
print(a is c) # True, same object in memory
print(a is b) # False, different objects, even though values are equal
Use == to compare values. Use is to check whether two names refer to the exact same object in memory, which matters most when checking for None: use "if x is None" rather than "if x == None".
Membership Operators: in and not in
fruits = ['apple', 'banana', 'cherry']
print('banana' in fruits) # True
print('mango' not in fruits) # True
Operator Precedence
Python evaluates expressions in a defined order: parentheses first, then exponentiation, then multiplication and division, then addition and subtraction, then comparisons, then logical operators. When in doubt, use parentheses to make the intended order explicit.
result = 2 + 3 * 4 # 14, not 20, since multiplication happens first
result2 = (2 + 3) * 4 # 20, parentheses override default precedence
Common Interview Questions
What is the difference between == and is?
== compares values for equality. is compares object identity, checking whether two names point to the exact same object in memory.
What is the difference between / and // in Python?
/ always performs true division and returns a float. // performs floor division, discarding any remainder and returning a whole number result.
FAQ
Frequently Asked Questions
What is the difference between == and is in Python?
== compares whether two values are equal. is compares whether two variables refer to the exact same object in memory, which is why 'is None' is the recommended way to check for None.
What is the difference between / and // in Python?
/ performs true division and always returns a float. // performs floor division, discarding the remainder and returning a whole number.
What does the % operator do in Python?
It returns the remainder of a division, commonly used to check divisibility or find even and odd numbers.
What is Python's operator precedence order?
Parentheses first, then exponentiation, then multiplication and division, then addition and subtraction, then comparisons, then logical operators.
Keep Reading
Related Articles
Learning Guides
INNER JOIN in SQL with Examples: Complete Beginner's Guide
Learn INNER JOIN in SQL with practical examples, syntax, interview questions, and real-world use cases. A complete beginner-friendly SQL JOI
Learning Guides
OUTER JOIN in SQL: Complete Guide with Examples for Beginners
Master OUTER JOIN in SQL with practical examples and real-world scenarios. Learn LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, syntax, use cases,
Learning Guides
Multi-Row Functions in SQL: Complete Guide with Examples
Learn multi-row (aggregate) functions in SQL including SUM, AVG, COUNT, MIN, MAX, GROUP BY and HAVING, with practical examples, NULL handlin