Free Python course. Sign Up for tracking progress →

Python: Expressions in definitions

We've already figured out how to work with variables to store and reuse information. But they also help simplify complex calculations. For example, currency conversion or making up a new word. Let's look at how to do it in practice.

Let us imagine that we need to convert euros into yuans via dollars. Banks often do this kind of conversion via an intermediate currency when shopping abroad.

First, convert 50 euros into dollars. Suppose that one euro is $1.25:

dollars_count = 50 * 1.25
print(dollars_count)  # => 62.5

Here we write expression into the variable dollars_count = 50 * 1.25 to the right of the equals sign. The interpreter will calculate the result (62.5) and write it to a variable. The interpreter doesn't care what form the data is written in: 62.5 or 50 * 1.25. From its perspective, both are expressions to be calculated. It does the calculations and comes up with the same value, 62.5.

Any string is an expression. String concatenation (concatenation of variable values) is also an expression. When the interpreter sees an expression, it processes it and generates a result, the value of the expression.

Here are some examples of an expression. We've written the total values in the comments to the right of each expression:

62.5             # 62.5
50 * 1.25        # 62.5
120 / 10 * 2     # 24.0
int('100')       # 100

'hello'          # hello
'Good' + 'will'  # Goodwill

In the places where an expression is expected, you can put any calculation. It can be not only mathematical, but also string-like concatenation. The program will remain functional.

Programs consist of many combinations of expressions. Based on the above, consider whether this code would work:

who = "dragon's " + 'mother'
print(who)

This code will display the string dragon's mother. If you want to check it yourself, run the code on repl.it and experiment with it.

Variables can be used to write even more complex calculations. Now, back to our currency program. Let's write the dollar value in yuans as a separate variable. Let's calculate the value of 50 euros in dollars by multiplying it by 1.25. Suppose that 1 dollar is 6.91 yuans:

yuans_per_dollar = 6.91
dollars_count = 50 * 1.25  # 62.5
yuans_count = dollars_count * yuans_per_dollar  # 431.875.0

print(yuans_count)

Now let's add text to the output using concatenation:

yuans_per_dollar = 6.91
dollars_count = 50 * 1.25  # 62.5
yuans_count = dollars_count * yuans_per_dollar  # 431.875.0

# The str() function turns a number into a string.
# There will be a separate lesson about these transformations.
print('The price is ' + str(yuans_count) + ' yuans')
# => The price is 431.875.0 yuans

Any variable can be part of any expression. At the moment of calculation, the name of the variable will be replaced by its value.

The interpreter calculates the value of dollars_count before this variable is used in other expressions. When it comes time to use a variable, Python already knows the value because it's calculated it.

Variables can be used to perform complex calculations and to provide a detailed output with the resulting value. But you can also get new expressions by combining two or more variable values. Concatenation is responsible for this.

Instructions

Write a program that takes the original number of euros written in the variable euros_count, converts euros to dollars, and prints it to the screen. Then, it should convert the result to yuans and print it on a new line.

Example output for 100 euros:

125.0
863.75

For the purposes of the exercise, we'll say that:
- 1 euro = $1.25
- 1 dollar = 4,50 zl

The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.


If you got stuck and don't know what to do, you can ask a question in our community