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.
Complex calculations using variables
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.
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 an expression 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
.
Any string and String concatenation are expressions. When the interpreter sees them, it processes them and generates results — the values.
Here are some examples of an expression. We have written the total values in the comments to the right of each line:
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 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.
Programs consist of many combinations of expressions. Based on the above, consider whether this code would work:
who = "dragon's " + 'mother'
print(who)
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:
rubles_per_dollar = 60
dollars_count = 50 * 1.25 # 62.5
rubles_count = dollars_count * rubles_per_dollar # 3750.0
print(rubles_count)
Now we add text to the output using concatenation:
rubles_per_dollar = 60
dollars_count = 50 * 1.25 # 62.5
rubles_count = dollars_count * rubles_per_dollar # 3750.0
# The `str()`` function turns a number into a string
# There will be a lesson about these transformations
print('The price is ' + str(rubles_count) + ' rubles')
# => The price is 3750.0 rubles
Any variable can be part of any expression. At the moment of calculation, Python replaces the variable name with its value.
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.
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.
Variables and concatenation
Let us try using variables with concatenation. Nothing will change syntactically. We know how to concatenate two lines:
what = "Kings" + "road"
print(what) # => Kingsroad
So we can glue together a string and a variable in which we wrote the string:
first = "Kings"
what = first + "road"
print(what) # => Kingsroad
You can also concatenate two variables with strings in them:
first = "Kings"
last = 'road'
what = first + last
print(what) # => Kingsroad
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.