Expressions, Operands & Operators
You have already typed print(2 + 3) and seen Python answer 5. That 2 + 3 has a name. It is called an expression. Every expression is made of just two kinds of parts.
1Three words to learn
Look at 2 + 3. It has two values — 2 and 3 — and one symbol between them, the +. Each of these has a name in Python.
The values on which the work is done. In 2 + 3, the operands are 2 and 3.
The symbols that tell Python what to do with the operands. In 2 + 3, the operator is +.
Operands and operators together. Python solves an expression and gets one single value from it.
2A longer example: 2 + 3 - 4
An expression can have more than one operator and more than two values. Look at 2 + 3 - 4:
2, 3 and 4 are the operands · + and - are the operators
Three operands, two operators, one expression. However long an expression is, Python does the same thing with it. It solves the expression and puts the one value it gets in its place.
4 + 10 * 5, Python does the * before the +. This is the same rule you follow in maths — multiply first, then add. The rule that decides which operator is done first is called precedence. It has its own lesson at the end of this chapter.3Take one apart
Click each expression below. The operands are shown in purple and the operators in black. Below them you can see the one value that Python gets from the whole expression.
Two operands and one operator. This is the smallest expression you can write.
12 > 3 asks a question, and its value is True. 'py' + 'thon' joins two strings, and its value is 'python'. The idea is the same. Only the operators are different.4An expression becomes one value
Remember this. Python never prints the expression itself. It solves the expression first, and only the answer is given to print().
The answer of an expression is a value, so it has a type too. 2 + 3 gives an int. 10 / 4 gives a float. 12 > 3 gives a bool. You will learn about each of them in the next three lessons.
5There is no × on your keyboard
In your maths notebook you write 2 × 3. Python does not understand ×. For multiplication it uses the asterisk, *:
Every Python operator can be typed using the keys on your keyboard. Now try some expressions of your own:
6Recap
In the expression 2 + 3 - 4, which are the operands?
In the expression 4 + 10 * 5, which are the operators?
How many values does Python get from one expression?