Sub topic 1
Expressions, operands and operators
Prerequisites
This chapter assumes you can run a Python script or use the REPL (the interactive prompt). You should know the core built-in types: int, float, str, bool, list and dict. You should also be comfortable with simple variable assignment such as x = 5.
What an expression is
An expression is a piece of code that Python can evaluate to produce a value. A literal like 42, a variable name like total, and a combination like total + 1 are all expressions. A statement, by contrast, performs an action, such as x = 5 or for .... Statements may contain expressions, but an expression cannot contain a statement.
You can check whether something is an expression by asking whether it can appear on the right side of = or inside a print() call.
Operands and operators
An operator is a symbol or keyword that tells Python to compute something. An operand is a value the operator works on. In 3 + 4, the operator is + and the operands are 3 and 4.
Operators are grouped by the number of operands they take:
- Unary operators take one operand, such as
-xornot flag. - Binary operators take two operands, such as
a * bora and b. - The conditional expression
a if cond else btakes three operands.
Some operators are symbols (+, ==, <<) and some are words (and, or, not, in, is).
price = 20
quantity = 3
total = price * quantity # binary operator
negative = -total # unary operator
label = "big" if total > 50 else "small" # three operands
print(total)
print(negative)
print(label)How evaluation works
Python evaluates an expression by reducing it step by step. Each sub-expression becomes a value, and that value becomes an operand for the next operator. The result of any expression is itself an object, so it can be stored, passed to a function, or used inside a larger expression.
The order of these steps is set by precedence and associativity. A later chapter covers those rules in detail. For now, use parentheses to make the order explicit.
Operators are tied to types
The same operator can mean different things for different operand types. Python picks the behavior based on the operands.
print(3 + 4) # addition of ints
print("3" + "4") # concatenation of strings
print([1] + [2]) # concatenation of lists
try:
print(3 + "4") # mixing int and str
except TypeError as error:
print("TypeError:", error)The last line shows a common error. Python does not guess how to combine an int with a str, so it raises TypeError. The fix is to convert one operand explicitly, for example str(3) + "4" or 3 + int("4").
Operator families
The rest of this tutorial covers each family in turn.
| Family | Examples | Result type | Column |
|---|---|---|---|
| Arithmetic | `+`, `-`, `*`, `/`, `//`, `%`, `**` | number | |
| Comparison | `==`, `!=`, `<`, `>=` | `bool` | |
| Logical | `and`, `or`, `not` | one of the operands | |
| Membership | `in`, `not in` | `bool` | |
| Identity | `is`, `is not` | `bool` | |
| Bitwise | `&`, `\ | `, `^`, `~`, `<<`, `>>` | `int` |
Exercise
In the expression -x + y * 2, name the operators and say which are unary and which are binary.
Show answer
The operators are -, + and *. The - in front of x is unary because it has one operand. The + and * are binary because each has two operands.
Exercise
Why does 3 + "4" raise an error, and how can you change it to produce the integer 7?
Show answer
Python does not convert between int and str automatically, so + raises TypeError. Convert the string first: 3 + int("4") gives 7.