Skip to main content

Expressions and Statements

What You'll Learn

The difference between expressions and statements, all the assignment operators, and how Python evaluates complex expressions.

Expressions vs Statements

An expression produces a value:

2 + 3 # value: 5
len("hello") # value: 5
x > 10 # value: True or False

A statement performs an action:

x = 5 # assignment statement
print(x) # function call statement
if x > 0: # conditional statement
...

Expressions can appear inside statements:

result = len("hello") * 2 # expression on the right, statement overall

Assignment Operators

Standard assignment:

x = 10
name = "Alice"
items = [1, 2, 3]

Augmented assignment (shorthand for x = x op y):

x = 10

x += 5 # x = x + 5 → 15
x -= 3 # x = x - 3 → 12
x *= 2 # x = x * 2 → 24
x //= 5 # x = x // 5 → 4
x **= 3 # x = x ** 3 → 64
x %= 10 # x = x % 10 → 4

Works with strings and lists too:

message = "Hello"
message += ", World!" # "Hello, World!"

items = [1, 2]
items += [3, 4] # [1, 2, 3, 4]

Multiple assignment:

a, b, c = 1, 2, 3
x = y = z = 0

# Swap values without a temp variable
a, b = b, a

Walrus operator := (Python 3.8+) — assign inside an expression:

# Without walrus — read file line by line
while True:
line = f.readline()
if not line:
break
process(line)

# With walrus
while line := f.readline():
process(line)

Arithmetic Operators

print(10 + 3) # 13 addition
print(10 - 3) # 7 subtraction
print(10 * 3) # 30 multiplication
print(10 / 3) # 3.333... (true division — always float)
print(10 // 3) # 3 floor division — rounds toward negative infinity
print(10 % 3) # 1 modulo (remainder)
print(10 ** 3) # 1000 exponentiation

Comparison Operators

print(5 == 5) # True equal
print(5 != 4) # True not equal
print(5 > 3) # True greater than
print(5 < 3) # False less than
print(5 >= 5) # True greater or equal
print(5 <= 4) # False less or equal

# Chained comparisons — unique to Python
x = 5
print(1 < x < 10) # True
print(0 <= x <= 5) # True

Logical Operators

# and — both must be True
print(True and True) # True
print(True and False) # False

# or — at least one must be True
print(True or False) # True
print(False or False) # False

# not — inverts the value
print(not True) # False
print(not False) # True

Short-circuit evaluation — Python stops as soon as the result is known:

# If `a` is False, `b` is never evaluated
result = a and b

# If `a` is True, `b` is never evaluated
result = a or b

This is useful for safe defaults:

name = user_input or "anonymous" # if user_input is empty/""/None, use "anonymous"

Operator Precedence

Python evaluates in this order (highest to lowest):

PriorityOperatorsExample
1 (highest)() parentheses(2 + 3) * 4 = 20
2** power2 ** 3 = 8
3+x, -x, ~x unary-5
4*, /, //, %10 * 2 // 3
5+, -10 + 5 - 2
6<, >, ==, etc.x > 0
7notnot x
8anda and b
9 (lowest)ora or b

When unsure, use parentheses:

# Unclear
result = x + y * z > 10 and not flag

# Clear
result = (x + (y * z) > 10) and (not flag)

Conditional Expression (Ternary)

One-line if/else:

age = 20
status = "adult" if age >= 18 else "minor"
print(status) # adult

Boolean Truthiness

In Python, many values are considered True or False:

# Falsy values (evaluate to False in boolean context)
bool(0) # False
bool(0.0) # False
bool("") # False
bool([]) # False
bool({}) # False
bool(None) # False

# Everything else is Truthy
bool(1) # True
bool(-1) # True
bool("hello") # True
bool([0]) # True (non-empty list)

Practical use:

items = []

if items:
print("Has items")
else:
print("Empty") # prints this

# Instead of: if len(items) > 0:

Quick Reference

# Augmented assignment
x += n x -= n x *= n
x /= n x //= n x **= n x %= n

# Multiple assignment
a, b = b, a # swap
a, b, c = 1, 2, 3

# Arithmetic
+ - * / // % **

# Comparison
== != > < >= <=

# Logical
and or not

# Ternary
value = x if condition else y

# Walrus
while chunk := f.read(1024):
...

What's Next

Lesson 5: Naming Style and Readability