Chapter 3 of 11
~14 minOperators
Operators are the building blocks of every Python expression - arithmetic, comparison, and logical operators combine values and conditions, and knowing their precedence keeps expressions predictable instead of surprising.
Arithmetic Operators
Python supports the usual arithmetic operators - addition, subtraction, multiplication - plus a few that surprise newcomers: / always produces a float even when dividing two integers, // performs floor division (rounding the result down to the nearest whole number), % returns the remainder, and ** raises a number to a power.
Try it yourself
Every arithmetic operator on the same two numbersa, b = 7, 2 print(a + b) print(a - b) print(a * b) print(a / b) print(a // b) print(a % b) print(a ** b)
Press Run to execute your Python. The first run loads Python in your browser (a few seconds).
= (Assignment) vs. == (Equality)
A single = assigns a value to a variable name; a double == compares two values and returns True or False. Confusing the two is a classic beginner mistake in other C-style languages, but Python actually prevents the most dangerous version of it: writing if x = y: is a SyntaxError in Python, since assignment is a statement, not an expression, so it cannot appear inside a condition at all.
Try it yourself
Assignment followed by comparisonx = 5 y = 5 print(x == y) x = 10 print(x == y)
Press Run to execute your Python. The first run loads Python in your browser (a few seconds).
Logical Operators
and, or, and not combine or invert boolean conditions. and is True only when both sides are True, or is True when at least one side is True, and not flips a single condition. These are commonly used to combine multiple checks into a single readable condition, for example validating both an age requirement and a permission flag together.
Try it yourself
Combining conditions with and, or, notage = 25 has_id = True print(age >= 18 and has_id) print(age < 18 or has_id) print(not has_id)
Press Run to execute your Python. The first run loads Python in your browser (a few seconds).
The Modulus Operator and Operator Precedence
The modulus operator % returns the remainder of a division and is the standard way to test whether a number is even or odd, or to wrap a counter around a fixed range. In Python, the sign of the result always matches the sign of the divisor - so -7 % 3 is 2, not -1, which differs from how some other languages compute it.
Operator precedence determines evaluation order when an expression mixes several operators: parentheses first, then exponentiation, then multiplication/division/floor-division/modulus, then addition/subtraction, and comparisons and logical operators last. When an expression could be read two different ways, adding explicit parentheses is clearer than relying on someone remembering the precedence table.
Try it yourself
Modulus sign behavior, then operator precedence in actionprint(7 % 3) print(-7 % 3) print(7 % -3) result = 2 + 3 * 4 ** 2 print(result) result2 = (2 + 3) * 4 ** 2 print(result2)
Press Run to execute your Python. The first run loads Python in your browser (a few seconds).
Coding challenge
Write a function evaluate(a, b, c) that computes total = a + b * c ** 2 and also computes a boolean is_positive that is True only when total is greater than 0 and c is not zero, then returns both values. Call it and print the results.
# Write your solution here
Press Run to execute your Python. The first run loads Python in your browser (a few seconds).
Answer every question below to mark this chapter as complete.
Quiz Yourself
Score: 0/51. What does the / operator always return in Python 3, regardless of whether the operands are integers?
2. Why does writing "if x = y:" fail in Python instead of silently doing an assignment?
3. What does "age >= 18 and has_id" evaluate to if age is 16 and has_id is True?
4. What is the result of -7 % 3 in Python?
5. Given "2 + 3 * 4 ** 2", what is evaluated first due to operator precedence?