Python Tutorial

Operators & Expressions

Arithmetic, comparison, logical, walrus operator (:=)

Arithmetic Operators

Python supports the standard math operations you're familiar with:
OperatorNameExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 33.33 (float)
//Floor Division10 // 33 (int)
%Modulo (Remainder)10 % 31
**Exponentiation10 ** 2100
python
# Modulo for even/odd
if 10 % 2 == 0:
    print("Even")

# Exponentiation
print(2 ** 10)  # 1024

Comparison Operators

Use these to compare values and get a boolean (True or False).
OperatorDescriptionExample
==Equal to10 == 10 (True)
!=Not equal10 != 5 (True)
>Greater than10 > 5 (True)
<Less than10 < 5 (False)
>=Greater or equal10 >= 10 (True)
<=Less or equal10 <= 5 (False)
python
name = "Alpha"
if name == "Alpha":
    print("Welcome back")

Logical Operators

Used for boolean logic. Note use of English keywords!
OperatorDescriptionExample
andBoth true(5 > 3) and (2 < 4) (True)
orAt least one true(5 > 3) or (2 > 10) (True)
notInverts booleannot (5 > 3) (False)
python
is_logged_in = True
has_auth = False

if is_logged_in and has_auth:
    print("Open Dashboard")

The Walrus Operator (Assignment Expression)

Introduced in Python 3.8, the walrus operator (:=) allows you to assign a value to a variable inside an expression.
python
# Without Walrus
data = get_data()
if data:
    process_data(data)

# With Walrus
if (data := get_data()):
    process_data(data)
Common Use Case (Loops): Read lines from a file until an empty line appears.
python
while (line := file.readline()) != "":
    print(line)

Assignment and Membership

python
# Assignment
x = 10
x += 5  # Same as x = x + 5

# Membership (in)
nums = [1, 2, 3]
if 2 in nums:
    print("Found 2")

# Identity (is) - used for None and True/False
if x is None:
    print("x is null")
Important: Use == for comparing values and is for comparing identity (memory address), specifically with None.

Summary

CategoryExamplePurpose
Arithmeticx + yMath operations
Comparisonx == yLogic checks
Logicala and bCombined logic
Assignmentx = 10Store data
Walrus(x := 5)Inline assignment