Operators and Expressions
Explore different operators in Python including arithmetic, comparison, and logical operators, and how to combine them into expressions.
Python Operators and Expressions
Understanding Operators and Expressions
In Python, operators are special symbols or keywords that perform operations on operands (values or variables). An expression is a combination of operators, operands, and sometimes function calls that evaluates to a value. Think of it like a mathematical equation, but it can also involve strings, booleans, and other data types. Python uses these constructs to perform calculations, make comparisons, and manipulate data.
Expressions are evaluated according to operator precedence (the order in which operators are applied). Parentheses can be used to override this precedence and control the order of evaluation.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations:
+
(Addition): Adds two operands.-
(Subtraction): Subtracts the second operand from the first.*
(Multiplication): Multiplies two operands./
(Division): Divides the first operand by the second (returns a float).//
(Floor Division): Divides the first operand by the second (returns the integer part of the quotient).%
(Modulo): Returns the remainder of the division.**
(Exponentiation): Raises the first operand to the power of the second.
x = 10
y = 3
print(x + y) # Output: 13
print(x - y) # Output: 7
print(x * y) # Output: 30
print(x / y) # Output: 3.3333333333333335
print(x // y) # Output: 3
print(x % y) # Output: 1
print(x ** y) # Output: 1000
Comparison Operators
Comparison operators compare two operands and return a boolean value (True
or False
):
==
(Equal to): ReturnsTrue
if the operands are equal.!=
(Not equal to): ReturnsTrue
if the operands are not equal.>
(Greater than): ReturnsTrue
if the first operand is greater than the second.<
(Less than): ReturnsTrue
if the first operand is less than the second.>=
(Greater than or equal to): ReturnsTrue
if the first operand is greater than or equal to the second.<=
(Less than or equal to): ReturnsTrue
if the first operand is less than or equal to the second.
x = 5
y = 10
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: False
print(x < y) # Output: True
print(x >= 5) # Output: True
print(y <= 10) # Output: True
Logical Operators
Logical operators are used to combine or modify boolean expressions:
and
: ReturnsTrue
if both operands areTrue
.or
: ReturnsTrue
if at least one operand isTrue
.not
: Returns the opposite boolean value of the operand.
a = True
b = False
print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False
print(not b) # Output: True
# Example combining comparison and logical operators
age = 25
is_student = True
if age > 18 and is_student:
print("Eligible for student discount")
else:
print("Not eligible for student discount")
Combining Operators and Expressions
You can combine different types of operators to create complex expressions. Python follows a specific order of operations (PEMDAS/BODMAS - Parentheses, Exponents, Multiplication and Division, Addition and Subtraction) to evaluate these expressions. Using parentheses explicitly makes your code clearer and less prone to errors.
result = (10 + 5) * 2 - (20 / 4)
print(result) # Output: 25.0
# Another Example
x = 5
y = 10
z = 2
is_valid = (x > 0 and y < 20) or (z == 2)
print(is_valid) # Output: True