Operators
Operators are used to perform various operations on variables and values. The standard arithmetic and assignment operators are the most familiar.
Syntax
The following code snippet uses the assignment operator, =
, to set my_variable
to the value of num1
and num2
with an arithmetic operator acting on them. For example, if operator
represented *
, my_variable
would be assigned a value of num1 * num2
.
my_variable = num1 operator num2
Python operators can be organized into the following groups:
- Arithmetic operators for performing traditional math evaluations.
- Assignment operators for assigning values to variables.
- Comparison operators for comparing two values.
- Logical operators for combining boolean values.
- Ternary operators for shorthand conditional experessions.
- Membership operators for testing membership in iterables.
Arithmetic Operators
Python has the following arithmetic operators:
- Addition,
+
, which returns the sum of two numbers. - Subtraction,
-
, which returns the difference of two numbers. - Multiplication,
*
, which returns the product of two numbers. - Division,
/
, which returns the quotient of two numbers. - Exponentiation,
**
, which returns the value of one number raised to the power of another. - Modulus,
%
, which returns the remainder of one number divided by another. - Floor division,
//
, which returns the integer quotient of two numbers.
The following code demonstrates how to use arithmetic operators in Python:
3 + 2 # Addition | Output: 53 - 2 # Subtraction | Output: 13 * 2 # Multiplication | Output: 63 / 2 # Division | Output: 1.53 ** 2 # Exponentation | Output: 93 % 2 # Modulus | Output: 13 // 2 # Floor Division | Output: 1
Assignment Operators
Python includes the following assignment operators:
- The
=
operator assigns the value on the right to the variable on the left. - The
+=
operator updates a variable by incrementing its value and reassigning it. - The
-=
operator updates a variable by decrementing its value and reassigning it. - The
*=
operator updates a variable by multiplying its value and reassigning it. - The
/=
operator updates a variable by dividing its value and reassigning it. - The
%=
operator updates a variable by calculating its modulus against another value and reassigning it.
The following code demonstrates how to use assignment operators in Python:
x = 5 # Assign | Output: x = 5x += 2 # Increment and Assign | Output: x = 7x -= 2 # Decrement and Assign | Output: x = 5x *= 2 # Multiply and Assign | Output: x = 10x /= 2 # Divide and Assign | Output: x = 5x %= 2 # Modulus and Assign | Output: x = 1
Comparison Operators
Python has the following comparison operators:
- Equal,
==
, for returningTrue
if two values are equal. - Not equal,
!=
, for returningTrue
if two values are not equal. - Less than,
<
, for returningTrue
if left value less than right value. - Less than or equal to,
<=
, for returningTrue
if left value is less than or equal to right value. - Greater than,
>
, for returningTrue
if left value greater than right value. - Greater than or equal to,
>=
, for returningTrue
if left value greater than or equal to right value.
The following code demonstrates how to use comparison operators in Python:
3 == 3 # Output: True3 != 2 # Output: True3 < 5 # Output: True3 <= 5 # Output: True3 > 5 # Output: False3 >= 5 # Output: False
Logical Operators
Python has the following logical operators:
- The
and
operator returnsTrue
if both statements areTrue
. - The
or
operator returnsTrue
if either statement isTrue
. - The
not
operator returnsTrue
if its associated statement isFalse
.
The following code demonstrates how to use logical operators in Python:
x = 10y = 5z = 15# Logical ANDand_result = (x > y) and (z > x) # True and True -> Output: True# Logical ORor_result = (x < y) or (z > y) # False or True -> Output: True# Logical NOTnot_result = not (x < y) # not False -> Output: True
Ternary Operators
Ternary operators in Python provide a shorthand way of writing simple if-else
statements. They are often used for conditional expressions or evaluations.
Syntax
value_if_true if condition else value_if_false
The expression evaluates the condition
, and if it is True
, it returns value_if_true
. Otherwise, it returns value_if_false
.
age = 20status = "Adult" if age >= 18 else "Minor"print(status) # Output: Adult
Membership Operators
Membership operators test whether a value exists within an iterable object like a list, string, or dictionary.
- The
in
operator returnsTrue
if the element on the left is found in the iterable object on the right. - The
not in
operator returnsTrue
if the element on the left is not found in the iterable object on the right.
Below is an example that shows how to use the membership operators in Python:
x = "a" in "apple" # Output: Truey = "z" not in "apple" # Output: True
Order of Operations
Python evaluates an expression in order of precedence as follows:
- Items in parentheses, (
(
…)
), have the highest level of precedence, expressions within them are evaluated first. - Exponentiation (
**
) - Multiplication and division operators (
*
,/
,//
&%
) - Addition and subtraction (
+
&-
) - Comparison (
<
,<=
,>
&>=
) - Equality (
==
&!=
) not
and
or
Below is an example that shows the order of operations in an expression in Python:
result = 3 + 2 * (4 ** 2) / 2print(result) # Output: 19.0
Note: Items at the same precedence are evaluated left to right. The exception to this is exponentiation, which evaluates right to left.
Codebyte Example
The following code demonstrates the use of various operators in Python, including arithmetic, comparison, logical, ternary, and membership operators:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours