Skip to content
VLSI Mentor

Python Part I · Module 3 · Operators and Expressions

Expressions: Code That Produces a Value

Module 2 left you able to hold values under names and find out what they are. What you could not do was compute with them — every example stored something and displayed it, and nothing ever combined two values into a third. This module is about that, and it starts with the machinery rather than with a list of symbols. An expression is any piece of code Python can work out; evaluating it means working it out; and the result is always exactly one value, with a type. Once that model is solid, every operator family in the rest of the module is just a different thing you can ask for, and you will be able to predict what comes back rather than run it and hope.

Foundation9 min readPythonPart IFundamentalsExpressionsEvaluation

Part I · Module 3 · Chapter 3.1 · Expressions: Code That Produces a Value

1. What an Expression Is

An expression is any piece of code Python can work out to produce a value.

All of these are expressions:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print(4210)
print(10 + 5)
print(600 - 11)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
4210
15
589

The first one is interesting. 4210 on its own is already an expression — Python works it out, and the answer is 4210. An expression does not have to do arithmetic; it only has to produce a value.

A name is an expression too:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
seed = 4210
print(seed)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
4210

Python worked out what seed refers to, and that was the value.

2. Evaluation

Evaluating an expression means working it out. The result is always exactly one value.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   10 + 5     ← the expression
      ↓        ← Python evaluates it
     15       ← the resulting value

That is the whole model, and it is worth holding on to because it does not change for the rest of the module. Whatever operators appear, whatever types are involved, an expression reduces to one value.

3. The Result Has a Type

Module 2 gave you type(), and it is immediately useful here, because the type of what comes out is not always obvious from what went in.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print(type(10 + 5))
print(type(10 / 5))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
<class 'int'>
<class 'float'>

Two numbers went into each. One produced an int, the other a float. That is not an accident and Chapter 3.2 explains exactly why — the point here is only that the operation decides the result type, not just the inputs.

You will use type() several times in this module for exactly this reason.

4. Expression or Statement

A distinction worth having straight, because it explains something you already do.

An expression produces a value. A statement performs an action.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
total = 10 + 5

That line contains both. 10 + 5 is an expression. The whole line is an assignment statement.

And you already know the order, from Chapter 2.3 §4: Python evaluates the right-hand side first, then points the name at the result.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   total = 10 + 5
             ↓         Python evaluates the expression
   total =   15        then assigns the resulting value

So an assignment is a statement whose right-hand side is an expression. That is why count = count + 1 made sense in Chapter 2.3 — the expression was worked out before anything was stored.

5. Expressions Can Contain Expressions

An expression can be built from smaller ones, and Python works out the inner parts first.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print((10 + 5) * 2)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
30

Traced step by step:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   (10 + 5) * 2
        ↓           work out the bracketed part
      15   * 2
        ↓           now the multiplication
       30

The brackets here are doing two jobs: they group the addition, and they make the order visible to you as a reader. Chapter 3.8 covers what happens when you leave them out, and what order Python uses then. Until that chapter, the examples in this module will either use one operation at a time or bracket the order explicitly.

6. Names Are Evaluated Too

An expression can mix names and values freely. Python works out what each name refers to, then does the operation.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
total_tests = 120
failed_tests = 7

print(total_tests - failed_tests)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
113

Traced:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   total_tests - failed_tests
        ↓                          each name is worked out
       120      -      7
        ↓
       113

The names were not changed by this. total_tests still refers to 120. An expression produces a new value; it does not modify what it was built from.

7. Not Every Expression Is Arithmetic

Worth flagging now, because it shapes how you read the next few chapters.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print(3 > 7)
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
False

3 > 7 is an expression. Python evaluated it and the resulting value is False — the boolean from Chapter 2.8.

8. Predict, Run, Observe

For each, predict the value and its type, then run it.

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
print(100)
print(100 - 1)
print(100 / 4)
print(100 * 2)
print(2 < 9)
print(type(100 / 4))
print(type(2 < 9))
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
100
99
25.0
200
True
<class 'float'>
<class 'bool'>

Two to check yourself on: 100 / 4 produced 25.0 rather than 25, and 2 < 9 produced a bool rather than a number. Both are covered properly in the next few chapters — if you predicted them, you are already reading expressions rather than symbols.

9. Exercise

Exercise 1 — expression or not. For each, say whether it is an expression that produces a value: (a) 4210 (b) seed (c) 10 + 5 (d) seed = 10 (e) 3 > 7

Exercise 2 — trace it. Write out the evaluation of (120 - 7) * 2 the way §5 does, one step per line. Then run it and check.

Exercise 3 — predict the type. Without running, predict the type of each result, then verify with type(): (a) 50 + 50 (b) 50 / 50 (c) 50 > 50

Exercise 4 — the unchanged name. Predict all three lines of output, then run:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
total = 100
print(total - 1)
print(total)

Explain in one sentence why the last line prints what it does, using the phrase "produces a new value".

10. Summary

  • An expression is code that produces a value. A bare value is one; a name is one; arithmetic is one.
  • Evaluating means working it out, and the result is always exactly one value.
  • The result has a type, and the operation helps decide it — 10 + 5 gives an int, 10 / 5 gives a float.
  • A statement performs an action; an expression produces a value. An assignment is a statement whose right-hand side is an expression, evaluated first.
  • Expressions nest, and Python works out the inner parts first. Until Chapter 3.8, order is shown with brackets rather than assumed.
  • An expression does not modify the names it was built from. It produces something new.
  • A comparison is an expression producing a bool, not yet a condition. Conditions are Module 9.

The machine is now in place. The next chapter fills it with the operator family you will use most.

Where this fits

Part of the Python curriculum.