Operators and Expressions in Python

A quick reminder that in these early lessons you’ll find some references to topics you haven’t yet covered in this course. Don’t worry when you read about something you haven’t come across yet. You’ll learn about these topics soon.

This text is part of a Real Python tutorial by Leodanis Pozo Ramos.


Python operators enable you to perform computations by combining objects and operators into expressions. Understanding Python operators is essential for manipulating data effectively.

This lesson covers the main Python operators. By the end of this lesson, you’ll understand that:

  • Arithmetic operators perform mathematical calculations on numeric values.
  • Comparison operators evaluate relationships between values, returning Boolean results.
  • Boolean operators create compound logical expressions.
  • Augmented assignment operators simplify expressions involving the same variable.

Getting Started With Operators and Expressions

In programming, an operator is usually a symbol or combination of symbols that allows you to perform a specific operation. This operation can act on one or more operands. If the operation involves a single operand, then the operator is unary. If the operator involves two operands, then the operator is binary.

For example, in Python, you can use the minus sign (-) as a unary operator to declare a negative number. You can also use it to subtract two numbers:

Language: Python
>>> -273.15
-273.15

>>> 5 - 2
3

In this code snippet, the minus sign (-) in the first example is a unary operator, and the number 273.15 is the operand. In the second example, the same symbol is a binary operator, and the numbers 5 and 2 are its left and right operands.

Programming languages typically have operators built in as part of their syntax. In many languages, including Python, you can also create your own operator or modify the behavior of existing ones, which is a powerful and advanced feature to have.

In practice, operators provide a quick shortcut for you to manipulate data, perform mathematical calculations, compare values, run Boolean tests, assign values to variables, and more. In Python, an operator may be a symbol, a combination of symbols, or a keyword, depending on the type of operator that you’re dealing with.

For example, you’ve already seen the subtraction operator, which is represented with a single minus sign (-). The equality operator is a double equal sign (==). So, it’s a combination of symbols:

Language: Python
>>> 42 == 42
True

In this example, you use the Python equality operator (==) to compare two numbers. As a result, you get True, which is one of Python’s Boolean values.

Speaking of Boolean values, the Boolean or logical operators in Python are keywords rather than signs, as you’ll learn in the section about Boolean operators and expressions. So, instead of the odd signs like ||, &&, and ! that many other programming languages use, Python uses or, and, and not.

Using keywords instead of odd signs is a really cool design decision that’s consistent with the fact that Python loves and encourages code’s readability.

Before jumping into discussions about different types of operators, you need to know that the most elementary goal of an operator is to be part of an expression. Operators by themselves don’t do much:

Language: Python
>>> -
  File "<input>", line 1
    -
    ^
SyntaxError: incomplete input

>>> ==
  File "<input>", line 1
    ==
    ^^
SyntaxError: incomplete input

>>> or
  File "<input>", line 1
    or
    ^^
SyntaxError: incomplete input

As you can see in this code snippet, if you use an operator without the required operands, then you’ll get a syntax error. So, operators must be part of expressions, which you can build using Python objects as operands.

So, what is an expression anyway? An expression is a simple statement that produces and returns a value.

You’ll find operators in many expressions. Here are a few examples:

Language: Python
>>> 7 + 5
12

>>> 42 / 2
21.0

>>> 5 == 5
True

In the first two examples, you use the addition and division operators to construct two arithmetic expressions whose operands are integer numbers. In the last example, you use the equality operator to create a comparison expression. In all cases, you get a specific value after executing the expression.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

You must own this product to join the conversation.