What Is Expression

This section provides a quick introduction of expressions. An expression is a sequence of lexical tokens in Python source code that expresses a single object, a single operation, or multiple operations executed sequentially.

What Is Expression? An expression is a sequence of lexical tokens in Python source code that expresses a single object, a single operation, or multiple operations executed sequentially.

An expression will always produce an object at the end.

When an expression with multiple operations is executed, the order of execution follow 3 generic rules:

The table below lists some commonly used operations on numeric and Boolean objects. They are sorted by their precedences in descending order.

Symbols                Operations
--------------------   --------------
(...)                  Parentheses
**                     Exponentiation
*, /, //, %            Multiplication and divisions
+, -                   Addition and subtraction
<, <=, >, >=, ==, !=   Comparisons
not                    Boolean NOT
and                    Boolean AND
or                     Boolean OR

Here are some examples of expressions:

# expression with a single "int" object
>>> 9
9

# expression with a single "tulip" object
>>> (9, 1, 1)
(9, 1, 1)

# expression with a single operation
>>> 9 + 3
12

# expression with two operations
# multiplication has higher precedence and gets executed first
>>> 9 + 3 * 2
15

# expression with a single operation
# addition within parentheses gets executed first
>>> (9 + 3) * 2
24

# expression with multiple operations in different data types
>>> (9 + 3) * 2 < 10 or (9 + 3) * 2 > 20
True

# expression with objects referred by variables
>>> x = (9 + 3) * 2
>>> x < 10 or x > 20
True

Precedences of other operations will be discussed for each specific group of data types.

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

Variables, Operations and Expressions

 What Is Variable

 What Is Operation

What Is Expression

 Conditional Expression - Ternary Operation

 Assignment Expression - Walrus Operation

 Statements - Execution Units

 Function Statement and Function Call

 Iterators and Generators

 List, Set and Dictionary Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB