Python operators are symbols that tell Python what to do with data. They can be used to perform arithmetic operations, compare data, and perform other tasks.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations on data. The most common arithmetic operators are:
+:
Addition-:
Subtraction*: Multiplication
/:
Division%:
Modulus (remainder)'
Code Sample
# Addition
print(2 + 3)
# Subtraction
print(5 - 2)
# Multiplication
print(3 * 4)
# Division
print(10 / 2)
# Modulus
print(10 % 3)
Output:
5
3
12
5.0
1
Comparison Operators
Comparison operators are used to compare two values. The most common comparison operators are:
==
: Equal to!=
: Not equal to<
: Less than>
: Greater than<=
: Less than or equal to>=
: Greater than or equal to
Code sample
# Equal to
print(2 == 2)
# Not equal to
print(2 != 3)
# Less than
print(1 < 2)
# Greater than
print(3 > 2)
# Less than or equal to
print(2 <= 2)
# Greater than or equal to
print(3 >= 2)
Output:
True
True
True
True
True
True
Logical Operators
Logical operators are used to combine two or more comparison operators to create a more complex comparison. The most common logical operators are:
and
: Both conditions must be true for the overall condition to be true.or
: Either condition can be true for the overall condition to be true.not
: Reverses the truth value of the condition.
Logical operators using real-world examples:
and
: You need to have both a driver's license and a car to drive.or
: You can buy a book online or at a bookstore.not
: You cannot go swimming if it is raining.
Code Sample
and
:
# If it is raining and the temperature is below 50 degrees Fahrenheit,
# then wear a jacket.
if raining and temperature < 50:
print("Wear a jacket!")
or
:
# If you are over 18 years old or you have a parent's permission,
# then you can watch this movie.
if age > 18 or parent_permission:
print("You can watch the movie!")
not
:
# If you do not have a fever, then you may go to school.
if not fever:
print("You may go to school.")
Logical operators can be used to create complex conditions that can be used in a variety of ways, such as in decision-making, data filtering, and validation.
Membership Operators
Membership operators are used to check if a value is a member of a collection. The most common membership operators are:
in
: Checks if a value is in a collection.not in
: Checks if a value is not in a collection.
Example
# in
print(2 in [1, 2, 3])
# not in
print(4 not in [1, 2, 3])
Output:
True
True
Identity Operators
Identity operators are used to check if two objects are the same object. The most common identity operators are:
is
: Checks if two objects are the same object.is not
: Checks if two objects are not the same object.
Example:
# is
a = 2
b = 2
print(a is b)
# is not
a = 2
b = 3
print(a is not b)
Output:
True
False
Operator Precedence
Operator precedence determines the order in which operators are evaluated. For example, multiplication and division have higher precedence than addition and subtraction. This means that multiplication and division will be evaluated before addition and subtraction.
Example:
# Multiplication and division have higher precedence than addition
# and subtraction.
print(2 + 3 * 4)
# Parentheses can be used to change the order of evaluation.
print((2 + 3) * 4)
Output:
14
20
A Final Word
Python operators are a powerful tool that can be used to perform a variety of tasks. By understanding how to use operators, you can write Python programs to do all sorts of amazing things.