Using Variables in Expressions
Introduction
Welcome back to Storing Data in Python Variables. You are now stepping into Unit 2 of 4, and you are making steady progress through the course.
In the previous lesson, we created variables and printed the values they held. That is a solid first step, but variables really begin to shine when we do something with them. In this lesson, we will use variables inside arithmetic expressions to compute brand-new results. We will focus on three core ideas:
- combining variables with arithmetic operators,
- mixing variables with plain (literal) numbers,
- saving a computed answer into a new variable, all while leaving the originals untouched.
By the end, expressions will feel like a natural extension of everything you already know about variables.
From Bare Numbers to Named Values
In the previous course, we performed arithmetic directly on numbers, such as print(12 * 3). That works, but the moment the values become meaningful (a price, a quantity, or a shipping cost), we want names attached to them so the code reads like a description of the problem.
The good news is that Python treats a variable name as a stand-in for its stored value. Anywhere you could write a number, you can write a variable that holds a number instead. When Python runs the line, it looks up each name, replaces it with the value, and then performs the calculation. This tiny idea, substitution, is what unlocks everything in this lesson.
Computing with Variables and Arithmetic Operators
Let's start with a small shopping example. We will store a price and a quantity in variables, then multiply them together to get a total.
Here is what Python does on the total = price * quantity line:
- It looks up
priceand finds12. - It looks up
quantityand finds3. - It evaluates
12 * 3, which gives36. - It stores that result under the new name
total.
The print() call then displays the label and the freshly computed value:
Notice how the arithmetic operators from the previous course still work exactly the same; they are simply powered by variables now.
Combining a Variable with a Literal Value
An expression does not have to use only variables. We can freely mix a variable with a literal number, meaning a plain value typed right into the code. This is handy for quick "what if" style questions.
Python evaluates price + 10 before printing: it substitutes 12 for price, adds 10, and hands the result 22 to print(). There was no need to create a separate variable to hold the answer, since it flows straight into the output:
Think of the expression as a one-shot calculation that lives just long enough to be displayed.
Expressions Do Not Change the Original Variable
Here is a subtle but important point: reading a variable inside an expression never changes it. When we wrote price + 10, Python looked at what price held (12) and used that value in the calculation. It did not overwrite price with 22.
Let's prove it by printing price right after:
The output confirms that price is untouched:
Keep this in mind: to actually change a variable, we need a new assignment with =, which is exactly the topic of the next unit. For now, every variable in this lesson stays read-only.
Combining Multiple Variables in One Expression
Now, let's bring everything together. Real formulas often involve several inputs, and Python is perfectly happy to reference many variables inside a single expression.
On the grand_total line, Python follows the usual math order of operations: multiplication happens before addition. So it computes price * quantity first (12 * 3 = 36), then adds shipping (36 + 5 = 41), and finally stores 41 under the name grand_total:
Notice the pattern we just used: gather inputs in clearly named variables, combine them in one readable expression, and store the answer in a new, descriptive variable.
Tracing the Complete Output
Running the full program from top to bottom produces the four lines below, one for each print() we wrote:
Take a moment to trace each line back to the expression that produced it. Total came from multiplying two variables; Ten more would be mixed a variable with a literal; Price is still proved the original variable was untouched; and Grand total combined three variables in a single expression. Same variables, different questions asked of them.
Conclusion and Next Steps
In this lesson, we saw how variables become truly useful once we start computing with them. We combined variables with arithmetic operators, mixed variables with literal numbers for quick calculations, and captured fresh results in new, well-named variables, all without ever modifying the originals. That last point is the key mental model to carry forward: an expression reads values; it does not change them.
Now, it is your turn to put these ideas into action in the CodeSignal IDE, where a set of hands-on exercises is waiting for you to build your own expressions from scratch. Have fun experimenting, and see you in the practice section!
