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
.
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:
Statements | Expressions |
---|---|
x = 15 | z = (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 25 + 4 # Addition 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
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.
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
.
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
.
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.
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
.
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.