2.1. Simple Expressions#

When learning any new language, whether it’s English, Spanish, or Chinese, one of the first things you need to grasp is the grammar. In spoken and written languages, grammar dictates how words and phrases are combined to form sentences. In the same way, programming languages have their own set of rules that govern how you can combine symbols, variables, and operators to create meaningful expressions. These rules are called syntax.

In Python, expressions are the fundamental building blocks. Just as sentences are the basic units of communication in human languages, expressions are the smallest units of computation that can be evaluated to produce a value in Python.

2.1.1. What is an Expression?#

In Python, an expression is a combination of variables, values, operators, and function calls that produce a result. You can think of it like a small equation or statement that Python evaluates to a value. Consider the example below:

5 + 3
8

In this expression, 5 + 3 is evaluated by Python, and the result is 8.

2.1.2. Syntax in Python: The Grammar of Programming#

Consider the sentence: “a nice the want forty-two.” Although you can combine strings of words together to form any sentence, there’s no inherent meaning to this sentence. Likewise, in Python, you can’t just throw together symbols, numbers, and variables at random. You must follow Python’s syntax rules—the grammar of the language.

Consider the example below:

5 + = 3  # This will throw a syntax error
  Cell In[6], line 1
    5 + = 3  # This will throw a syntax error
        ^
SyntaxError: invalid syntax

2.1.3. Types of Python Expressions#

Python expressions come in many forms. Let’s break down the most common types you’ll encounter:

2.1.3.1. Arithmetic Expressions#

Arithmetic expressions are mathematical computations made up of numbers and arithmetic operators.

Common Operators. Expressions can be used to express any sort of arithmetic. The operators below are the most essential for data scientists

Expression Type

Operator

Example

Value

Addition

+

2 + 3

5

Subtraction

-

2 - 3

-1

Multiplication

*

2 * 3

6

Division

/

7 / 3

2.66667

Remainder

%

7 % 3

1

Exponentiation

**

2 ** 0.5

1.41421

Consider some examples below:

x = 10 + 5   # Simple addition (evaluates to 15)
y = 10 - 3   # Subtraction (evaluates to 7)
z = 10 * 2   # Multiplication (evaluates to 20)
w = 10 / 2   # Division (evaluates to 5.0)
a = 10 ** 2  #Exponentiation (evaluates to 100)
b = 10 // 3  # Floor Division (evaluates to)

Python evaluates these expressions following the rules of precedence (which operator gets evaluated first, like PEMDAS in math).

5 + 3 * 2  # Evaluates to 11, because multiplication is done first
11
1 + 2 * (3 * 4 * 5 / 6) ** 3 + 6 + 5 - 8 + (4 * 5)
2024.0

Just as mastering grammar allows you to communicate clearly in a spoken language, mastering Python’s syntax will allow you to write clear and powerful code. Let’s now consider a couple more types of expressions