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
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions, and more.
Guides on different topics related to the technologies that we teach in our courses
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.
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.
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
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:
|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:
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.
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
Another useful use of expressions in Python is to concatenate two strings into one.
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
Another way that you can use an expression is to create a conditional expression using Python conditionals.
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
x if x > z, but if the variable
x is not greater than
z then we assign the value of
z to variable
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.
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
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.