Most Python operators will look familiar from PHP — a few of Python's own choices are worth flagging specifically.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 5 + 2 → 7 |
| - | Subtraction | 5 - 2 → 3 |
| * | Multiplication | 5 * 2 → 10 |
| / | Division (always returns a float) | 5 / 2 → 2.5 |
| // | Floor division (rounds down to an int) | 5 // 2 → 2 |
| % | Modulus (remainder) | 5 % 2 → 1 |
| ** | Exponent | 5 ** 2 → 25 |
// is worth noting specifically — Python has no direct equivalent to PHP's automatic "int if it divides evenly" division behavior; / always gives a float, and // is how you deliberately get a whole number back.
Python uses + for both arithmetic and joining strings — there's no separate dot operator like PHP's .. Python decides which one you mean from the types involved, which is exactly why mixing a string and a number with + raises an error rather than guessing (from the Data Types lesson):
greeting = "Hello, " + "world!"
print(greeting)
# name = "Age: " + 21 # TypeError — use an f-string instead (next lesson)This is Python's equivalent of PHP's == vs. === distinction, but it works differently. == compares value; is compares identity — whether two names point at the literal same object in memory:
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True — same values
print(a is b) # False — two different list objects, even though equal
print(a is c) # True — c points at the exact same object as aDefault to ==, use is for None checks
Use == for comparing values, which is what you want almost all of the time. is is specifically for checking identity, and its one genuinely common use is comparing against None: if value is None: is the idiomatic Python way to do it, not if value == None:.
Python spells these out as words rather than symbols — there's no &&, ||, or !:
age = 20
has_id = True
if age >= 18 and has_id:
print("Entry allowed.")
if not has_id:
print("ID required.")