Variables and operations
Variables are simply values that are allocated somewhere in the memory of our computer. They are similar to variables in mathematics. They can be anything: text, integers, or floats (a number with precision after the decimal point, such as 2.33).
To create a new variable, you only need to write this:
x = 2
In this case, we have named a variable x
and set its value to 2
.
As in mathematics, you can perform some operations on these variables. The most common operations are addition, subtraction, multiplication, and division. The way to write them in Python is like this:
x = x + 5 #x += 5
x = x - 3 #x -= 3
x = x * 2.5 #x *= 2.5
x = x / 3 #x /= 3
If you look at it for the first time, it doesn't make much sense—how can we write that x = x + 5
?
In Python, and in most code, the "=" notation doesn't mean the two terms are equal. It means that we associate the new x
value with the value of the old...