Python Basics
Theory: Function calls and expressions
Let's keep looking at functions and their components. Today, we'll be looking at expressions. Let's discuss what it is and what is the difference between functions and expressions.
What are expressions
An expression in programming returns a result that we can use. You probably already know a lot about them and the principles of their construction.
For example, addition, subtraction, and concatenation are all expressions:
The peculiarity of expressions is that they return a result that we can use again: for example, they can be assigned to a variable or printed. It is what it looks like in the code:
But not everything in programming is an expression. The definition of a variable is an instruction, which means it can't be part of an expression. In other words, this code will lead to an error:
Let us see whether we can take a function call as an expression.
Functions as expressions
We know that functions return results, so they are expressions. It leads to a lot of possibilities. For example, we can use a function call directly in mathematical operations. It is how we can get the last character index in a word:
This code has no new syntax. We have merely connected parts we already know. We could go even further:
All of this applies to any function, e.g., string functions:
As you'll see later, we can combine expressions to produce increasingly complex behavior in different places. The more you learn and practice Python, the better you understand the topic. Over time, you'll learn how to connect code fragments to get the needed results.
Completed
0 / 43