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.

price = 12
quantity = 3

# Use variables in an expression to compute a new value
total = price * quantity
print("Total:", total)

Here is what Python does on the total = price * quantity line:

  1. It looks up price and finds 12.
  2. It looks up quantity and finds 3.
  3. It evaluates 12 * 3, which gives 36.
  4. It stores that result under the new name total.

The print() call then displays the label and the freshly computed value:

Total: 36

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.

# Combine a variable with a literal; the variable itself is unchanged
print("Ten more would be:", price + 10)

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:

Ten more would be: 22

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:

print("Price is still:", price)

The output confirms that price is untouched:

Price is still: 12

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.

# Combine several variables in one expression
shipping = 5
grand_total = price * quantity + shipping
print("Grand total:", grand_total)

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:

Grand total: 41

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:

Total: 36
Ten more would be: 22
Price is still: 12
Grand total: 41

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!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal