Use arithmetic and assignment operators correctly.
โ Operators & Expressions
Learn how Python combines values, performs calculations, compares results and builds logical decisions while following precise precedence rules.
theory = 78
lab = 86
average = (theory + lab) / 2
eligible = average >= 60 and lab >= 70
print(f"Average: {average}")
print(f"Eligible: {eligible}")Eligible: True
By the End of This Level
You will be able to construct and evaluate Python expressions accurately.
Predict results using precedence and associativity.
Build comparison and logical expressions.
Recognize membership and identity operations.
Operators, Operands and Expressions
An operator tells Python which operation to perform. The values used by the operator are operands, and their complete combination is an expression.
In total = price * quantity, price and quantity are operands, * is the operator, and price * quantity is the expression whose result is assigned to total.
A valid combination of values, variables, operators and function calls that Python evaluates to produce one value.
12 + 8 โ 2075 >= 40 โ TrueTrue and False โ False"Py" in "Python" โ TrueArithmetic Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | True division | 10 / 3 | 3.333... |
// | Floor division | 10 // 3 | 3 |
% | Remainder | 10 % 3 | 1 |
** | Exponentiation | 10 ** 3 | 1000 |
Split Total Seconds
total_seconds = 3672
hours = total_seconds // 3600
remaining = total_seconds % 3600
minutes = remaining // 60
seconds = remaining % 60
print(hours, minutes, seconds)1 1 12
/ returns a floating-point result. // returns the mathematical floor, so negative values may move to the next smaller integer: -7 // 2 is -4.
Precedence and Associativity
Precedence decides which kind of operator is evaluated first. Associativity decides the direction when operators have the same precedence.
( )*** / // %+ -Trace the Expression
result = 10 + 2 * 3 ** 2
print(result)
grouped = (10 + 2) * 3 ** 2
print(grouped)28 108
10 + 2 * 3
# 10 + 6
# 16Multiplication runs before addition.
(10 + 2) * 3
# 12 * 3
# 36Parentheses make the intended order unmistakable.
Comparison Operators
A comparison expression evaluates to the Boolean value True or False.
| Operator | Question | Example | Result |
|---|---|---|---|
== | Are they equal? | 5 == 5 | True |
!= | Are they different? | 5 != 3 | True |
> | Greater than? | 8 > 10 | False |
< | Less than? | 8 < 10 | True |
>= | Greater than or equal? | 40 >= 40 | True |
<= | Less than or equal? | 12 <= 9 | False |
= assigns a value, while == compares two values for equality.
Logical Operators
and
True only when both operands are true.
or
True when at least one operand is true.
not
Reverses the truth value of its operand.
Short-circuiting
Python stops as soon as the final logical result is known.
Training Eligibility
attendance = 82
test_score = 68
fees_due = False
eligible = attendance >= 75 and test_score >= 60
blocked = fees_due or not eligible
print("Eligible:", eligible)
print("Blocked:", blocked)Eligible: True Blocked: False
Assignment Operators
Augmented assignment performs an operation and stores the result back in the same variable.
| Long form | Short form | Meaning |
|---|---|---|
score = score + 5 | score += 5 | Add and assign |
stock = stock - 1 | stock -= 1 | Subtract and assign |
price = price * 2 | price *= 2 | Multiply and assign |
value = value / 10 | value /= 10 | Divide and assign |
count = count % 5 | count %= 5 | Remainder and assign |
Update a Learning Score
score = 50
score += 20
score -= 5
score *= 2
print(score)130
Membership and Identity Operators
in and not in
Check whether a value occurs inside a sequence or collection.
"Py" in "Python" # True
"Java" not in "Python" # Trueis and is not
Check whether two references point to the same object.
result = None
print(result is None) # TrueEquality vs Identity
Use == for equal values. Use is mainly for singleton objects such as None.
score == 100
result is NoneCommon Operator Mistakes
score = 50Use ==, not =, when you intend to compare values.
total / count + 1Add parentheses when the required grouping is total / (count + 1).
age >= 18 or score >= 60Use and if both requirements must be satisfied.
๐ Operators & Expressions โ Quick Revision
Review the complete Level 3 lesson before attempting the quiz and programming problems.
An expression evaluates to a single value.
/ performs true division; // performs floor division.
% produces a remainder and ** performs exponentiation.
Parentheses provide the clearest control over evaluation order.
Exponentiation is evaluated before multiplication and addition.
Comparisons produce True or False.
and requires both conditions; or requires at least one.
not reverses a truth value.
Augmented assignment updates and stores a value concisely.
Use == for equality and is mainly with None.
Level 3 Quick Quiz
Select one answer for every question, then check your score.
๐ฏ 5 Programming Problems โ Operators & Expressions
Use the displayed sample inputs. Write expressions carefully, run them, and check the exact expected output before viewing the official program.
A problem counts as Solved when its output passes before the official solution is opened.
Read the price and quantity, then display the total bill.
120, 3Expected output: Total = 360price by quantity.Python Code Editor
Program Output
Run your program to see the output.
Test Case
price = int(input())
quantity = int(input())
total = price * quantity
print("Total =", total)Read three marks and display their arithmetic average.
78, 85, 92Expected output: Average = 85Python Code Editor
Program Output
Run your program to see the output.
Test Case
mark1 = int(input())
mark2 = int(input())
mark3 = int(input())
average = (mark1 + mark2 + mark3) / 3
print("Average =", average)Read principal, annual rate and time, then calculate simple interest.
10000, 5, 2Expected output: Simple interest = 1000principal * rate * time / 100.Python Code Editor
Program Output
Run your program to see the output.
Test Case
principal = float(input())
rate = float(input())
time = float(input())
interest = principal * rate * time / 100
print("Simple interest =", interest)Convert total seconds into hours, minutes and remaining seconds.
3672Expected output: Three labelled lines: 1, 1, 12// for completed units and % for the remainder.Python Code Editor
Program Output
Run your program to see the output.
Test Case
total = int(input())
hours = total // 3600
remaining = total % 3600
minutes = remaining // 60
seconds = remaining % 60
print("Hours =", hours)
print("Minutes =", minutes)
print("Seconds =", seconds)A student is eligible when age is at least 18 and score is at least 60. Display the Boolean result.
20, 75Expected output: Eligible = Trueand.Python Code Editor
Program Output
Run your program to see the output.
Test Case
age = int(input())
score = int(input())
eligible = age >= 18 and score >= 60
print("Eligible =", eligible)๐ฏ Make Every Expression Intentional
Know each operand and its data type.
Use parentheses to express the intended order.
Follow precedence one operation at a time.
Check both the result and its type.
๐ฌ Expression Evaluation โ Visual Flow
Watch Python reduce 10 + 2 * 3 ** 2 one precedence level at a time.
๐งฎ How Python Evaluates an Expression
1. Read the Complete Expression
Python receives 10 + 2 * 3 ** 2 and identifies the available operators.
๐ Program Tracing โ Operator Results
Trace arithmetic, remainder and comparison results as variables and output change.
Click Next to begin tracing.
โ
๐ค Operators & Expressions โ Interview Questions
Answer aloud before opening each explanation.
What is the difference between / and //?
/ performs true division and returns a floating-point result. // performs floor division and returns the floor of the quotient.
What is operator precedence?
Precedence is the rule that determines which operators are evaluated before others when an expression contains several operator types.
What is associativity?
Associativity determines the evaluation direction when operators have the same precedence. Most arithmetic operators associate left to right, while exponentiation associates right to left.
How do and and or differ?
and requires both conditions to be truthy. or requires at least one condition to be truthy.
What is short-circuit evaluation?
Python stops evaluating a logical expression when the remaining operands cannot change its resultโfor example, after a false operand in an and expression.
When should is be used instead of ==?
Use == to compare values. Use is to compare object identity, most commonly when checking a singleton such as None.
๐ก Operators & Expressions โ Extra Tips
- 01
Use parentheses when they make the intended formula easier to read, even if precedence already gives the same result.
- 02
Trace one intermediate value at a time when an expression produces an unexpected answer.
- 03
Remember that
/returns a float even when the division is exact. - 04
Use
//and%together when splitting a total into units and remainders. - 05
Write complete comparisons on both sides of logical operators.
- 06
Prefer clear Boolean variable names such as
is_eligibleorhas_access.
โ๏ธ Operators & Expressions โ Extra Practice Questions
- Predict
5 + 2 * 3 ** 2without running it, then verify your trace. - Read two numbers and display all seven arithmetic-operation results.
- Read a three-digit number and display the sum of its digits using
//and%. - Convert a total number of minutes into hours and remaining minutes.
- Write an expression that checks whether a number lies from 10 through 50, inclusive.
- Show how parentheses change the result of
20 - 6 / 2 + 3. - Use augmented assignment to apply a bonus, penalty and multiplier to a score.
- Check whether the text
"Python"contains"tho". - Build a Boolean expression for age at least 18, attendance at least 75 and no pending fee.
- Explain why
2 ** 3 ** 2is different from(2 ** 3) ** 2.
Operators & Expressions Complete
When you can predict precedence, choose suitable operators and solve all five programs without help, mark this level complete and continue.
