 Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Data Science and Machine Learning - 16 wks

Full-Stack Software Developer - 16w

Search from all Lessons

Social & live learning

The most efficient way to learn: Join a cohort with classmates just like you, live streams, impromptu coding sessions, live tutorials with real experts, and stay motivated.

← Back to How to's
Edit on Github

# What is an expression in Python?

Written by:

An expression in Python is a combination of values, variables, operators, and function calls that produce a result. It represents a computation or a calculation. In the following example, we use an expression to find the circumference of a circle with a radius of 4cm.

1circle_radius = 4 # a statement
2number_pi = 3.1415926535 # a statement
3
4circumference = (number_pi * 2) * circle_radius # an expression
5rounded_circumference = round(circumference, 2)
6
7print(rounded_circumference) # Output: 25.13

In this example, we use the Python expression (number_pi * 2) * circle_radius to find the circumference of a circle, then we use the round() function to take the first two digits of the result, in this expression the number pi 3.1415926535 is multiplied by 2, and then that result is multiplied by the radius of the circle 4, that gives us the circumference of the circle 25.13 cm.

## What is an expression in Python programming?

An expression in python is a combination of operators that produces a result or a value after the calculation, it is the building block of any Python program, enabling us to perform calculations, manipulate data, make decisions, and much more.

Expressions are often confused with statements in Python, the main difference between a statement and an expression is that an expression performs a calculation and returns a value while a statement performs an action but it doesn't return anything.

Here are some examples of statements and expressions:

StatementsExpressions
x = 15z = (2 + 3) * 4
print(x)z = z * len("Hello!")
if x > 10: print(x)n = "hello" + " " + "world!"

As mentioned before, an expression can be a combination of values, variables, operators, and function calls. Here are some examples of what you can use in a Python expression:

Values

1#literals
27
33.14159
4"Hello world!"
5
6#variables
7x
8z
9
10#function calls
11len("Hello")
12round(3.14159, 2)

Operators

1# arithmetic operators
32 - 1 # Subtraction
47 * 9 # Multiplication
56 / 2 # Division
69 % 2 # Module
7
8# Comparison operators
9x == z # Equal to
10x > z # Greater then
11x < z # Less then
12x != z # Not equal to
13
14# Logical operators
15x and z # Logical AND
16x or z # Logical OR
17not z # Logical NOT
18
19# Assignment operators
20x = z # Assignment
21x += # Adsition assignment

## Examples of expressions in Python

There are many use cases for expression in Python, you can use them to perform mathematical calculations, string concatenation, and many more. we're gonna see some examples of how to use them below.

### 1. Mathematical calculation

One of the most usual and important uses of an expression in Python is to make a mathematical calculation.

1circle_radius = 7
2number_pi = 3.14159
3
4total_area = round(number_pi * (circle_radius * 2), 2)
5print(total_area, "cm") # Output: 43.98 cm

Here we use the Python mathematical expression number_pi * (circle_radius * 2) to find the area of a circle with a radius of 7cm, then we use the Python round() method to display only the first two numbers of the result and store that number in the variable total_area.

### 2. String concatenation

Another useful use of expressions in Python is to concatenate two strings into one.

1message_one = "Hello"
2message_two = "world!!!"
3
4full_message = message_one + " " + message_two
5print(full_message) # Output: Hello world!!!

In this example, we use the Python expression message_one + " " + message_two to concatenate two strings into one, first, we add the value of the variable message_one with an empty space " " then we add to it the value of the variable message_two.

### 3. Conditional expression

Another way that you can use an expression is to create a conditional expression using Python conditionals.

1x = 15
2z = 20
3
4max_number = x if x > z else z
5print(max_number) # Output: 20

In this code, we want to assign the largest number between two variables to the variable max_number in order to do that, we use the Python expression x if x > z else z to assign it the largest of them. First, we create a condition, if the value of the variable x is greater than the value of the variable z we assign the value of x to the variable max_number x if x > z, but if the variable x is not greater than z then we assign the value of z to variable max_number else z, in this way we ensure that the variable max_number has the greater number between the two.

### 4. List Comprehension

You also can use an expression to create a list comprehension in Python.

1numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2squared_even_numbers = [num**2 for num in numbers if num % 2 == 0]
3
4print(squared_even_numbers) # Output: [4, 16, 36, 64, 100]

In this example, we want to create an array that has all the even numbers squared from the array numbers, to do this, we create a list comprehension with the condition that is going to store in the new array only the even numbers in the array numbers with the expression if num % 2 == 0 then we want to save those numbers squared, we do this with the expression num**2 for num in numbers, in this way, we store in the variable squared_even_numbers only the even numbers from the array numbers.

## Conclusion

We can use an expression in Python to perform mathematical calculations, manipulate data, and many more things. There are many use cases for Python expressions not only the ones mentioned in this article. You will probably use Python expressions many times as you work with the Python programming language you can even make use of Python libraries such as numpy (Numerical Python) and Python expressions to perform more advanced or complex mathematical equations. You can read more about this and other topics at 4Geek's Blog.