HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>We have already figured out how to work with variables to store and reuse information. But they also help simplify complex calculations. For example, we can observe currency conversion or making up a new word. Let us look at how to do it in practice.</p>
1 <p>We have already figured out how to work with variables to store and reuse information. But they also help simplify complex calculations. For example, we can observe currency conversion or making up a new word. Let us look at how to do it in practice.</p>
2 <h2>Complex calculations using variables</h2>
2 <h2>Complex calculations using variables</h2>
3 <p>Let us imagine that we need to convert euros into rubles via dollars. Banks often do this kind of conversion via an intermediate currency when shopping abroad.</p>
3 <p>Let us imagine that we need to convert euros into rubles via dollars. Banks often do this kind of conversion via an intermediate currency when shopping abroad.</p>
4 <p>First, convert 50 euros into dollars. Suppose that one euro is $1.25:</p>
4 <p>First, convert 50 euros into dollars. Suppose that one euro is $1.25:</p>
5 <p>Here we write an<strong>expression</strong>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 does not care what form the data is written in - it can be 62.5 or 50 * 1.25. From this perspective, Python sees them both as expressions to calculate. It does the calculations and comes up with the same value, 62.5.</p>
5 <p>Here we write an<strong>expression</strong>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 does not care what form the data is written in - it can be 62.5 or 50 * 1.25. From this perspective, Python sees them both as expressions to calculate. It does the calculations and comes up with the same value, 62.5.</p>
6 <p>Any string and String concatenation are expressions. When the interpreter sees them, it processes them and generates results - the<strong>values</strong>.</p>
6 <p>Any string and String concatenation are expressions. When the interpreter sees them, it processes them and generates results - the<strong>values</strong>.</p>
7 <p>Here are some examples of an expression. We have written the total values in the comments to the right of each line:</p>
7 <p>Here are some examples of an expression. We have written the total values in the comments to the right of each line:</p>
8 <p>In the places where Python expects an expression, we can put any calculation. It can be not only mathematical but also string-like concatenation. The program will remain functional.</p>
8 <p>In the places where Python expects an expression, we can put any calculation. It can be not only mathematical but also string-like concatenation. The program will remain functional.</p>
9 <p>Programs consist of many combinations of expressions. Based on the above, consider whether this code would work:</p>
9 <p>Programs consist of many combinations of expressions. Based on the above, consider whether this code would work:</p>
10 <p>We can use variables to write even more complex calculations. Now, back to our currency program. Let us write the dollar value in rubles as a separate variable. We will calculate the price of 50 euros in dollars by multiplying it by 1.25. Suppose that 1 dollar is 60 rubles:</p>
10 <p>We can use variables to write even more complex calculations. Now, back to our currency program. Let us write the dollar value in rubles as a separate variable. We will calculate the price of 50 euros in dollars by multiplying it by 1.25. Suppose that 1 dollar is 60 rubles:</p>
11 <p>Now we add text to the output using concatenation:</p>
11 <p>Now we add text to the output using concatenation:</p>
12 <p>Any variable can be part of any expression. At the moment of calculation, Python replaces the variable name with its value.</p>
12 <p>Any variable can be part of any expression. At the moment of calculation, Python replaces the variable name with its value.</p>
13 <p>The interpreter calculates the value of dollars_count before we use this variable in other expressions. When it comes time to use a variable, Python has already calculated its value.</p>
13 <p>The interpreter calculates the value of dollars_count before we use this variable in other expressions. When it comes time to use a variable, Python has already calculated its value.</p>
14 <p>We can use variables to perform complex calculations and 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.</p>
14 <p>We can use variables to perform complex calculations and 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.</p>
15 <h2>Variables and concatenation</h2>
15 <h2>Variables and concatenation</h2>
16 <p>Let us try using variables with concatenation. Nothing will change syntactically. We know how to concatenate two lines:</p>
16 <p>Let us try using variables with concatenation. Nothing will change syntactically. We know how to concatenate two lines:</p>
17 <p>So we can glue together a string and a variable in which we wrote the string:</p>
17 <p>So we can glue together a string and a variable in which we wrote the string:</p>
18 <p>You can also concatenate two variables with strings in them:</p>
18 <p>You can also concatenate two variables with strings in them:</p>
19 <p>Variables are an essential tool in programming. They simplify complex calculations and thus make development easier. But to work successfully with variables, you must not only use them but also call them correctly. We will talk about this in the next lesson.</p>
19 <p>Variables are an essential tool in programming. They simplify complex calculations and thus make development easier. But to work successfully with variables, you must not only use them but also call them correctly. We will talk about this in the next lesson.</p>