Skip to main content
Translate content

To read content in another language, use your browser’s Translate page option (right-click the page or open the browser menu). Choose your preferred language; if offered, enable automatic translation. The site language menu changes navigation, not the original article. Interactive activities may remain in their original language.

ProgrammingFundamentalsEstimated time: 45 minUpdated November 26, 2026

Python Operators Explained: Arithmetic, Comparison, and Logical Operators

How does Python perform calculations, compare values, and make decisions? Operators are essential building blocks that allow programs to process information and evaluate conditions. In this beginner-friendly lesson, you will explore arithmetic, comparison, and logical operators through clear explanations, practical Python examples, interactive experiments, and step-by-step exercises. You will also learn how operator precedence works and how to avoid common programming mistakes.

Visual guide to Python arithmetic, comparison, and logical operators with simple programming examples for beginners.

Learning objectives

  • Understand the role of operators and operands in Python.
  • Perform calculations using arithmetic operators.
  • Distinguish true division from floor division.
  • Calculate remainders using the modulo operator.
  • Calculate powers using the exponentiation operator.
  • Compare numerical values using comparison operators.
  • Understand Boolean values and comparison results.
  • Distinguish assignment from equality comparison.
  • Combine conditions using and, or, and not.
  • Interpret Boolean truth tables.
  • Understand operator precedence and parentheses.
  • Use chained comparisons to check numerical ranges.
  • Recognize common operator-related programming errors.
  • Apply Python operators to practical programming problems.

Introduction

How does Python calculate a total, check an answer, or decide whether two conditions are satisfied? Operators are symbols and keywords that tell Python to perform operations on values. In this lesson, you will explore arithmetic, comparison, and logical operators using small programs and interactive experiments. You only need to know variables, numbers, Boolean values, and the print() function.

Learning objectives

  • Calculate with seven arithmetic operators.
  • Compare values using six comparison operators.
  • Combine Boolean conditions using and, or, and not.
  • Apply operator precedence and recognize common errors.

1. Arithmetic operators

An operand is a value on which an operator acts. In 8 + 3, 8 and 3 are operands and + is the operator. Python evaluates the expression to 11.

OperatorMeaningExpressionResult
+Addition8 + 311
-Subtraction8 - 35
*Multiplication8 * 324
/True division8 / 32.6666666666666665
//Floor division8 // 32
%Modulo (remainder)8 % 32
**Exponentiation8 ** 3512

Worked example: sharing pencils

There are 17 pencils and 5 students. Give each student the same number of whole pencils.

pencils = 17
students = 5
print(pencils // students)
print(pencils % students)

Output: 3, then 2. Floor division gives the number of whole pencils per student. Modulo gives the two pencils left over. Check: 5 * 3 + 2 = 17.

True division of integers produces a float, even when the quotient is exact: 8 / 2 gives 4.0. Floor division rounds toward negative infinity, not toward zero: -7 // 3 gives -3 and -7 % 3 gives 2. Division, floor division and modulo by zero raise ZeroDivisionError.

Worked example: powers and totals

side = 4
area = side ** 2
cost = 3 * 5 + 2
print(area)
print(cost)

The area is 16 square units because 4 squared is 16. The cost is 17 because multiplication happens before addition.

Interactive experiment: arithmetic calculator

Change the integer operands and choose an operator. Predict the result before checking it.

First integer Operator Second integer

17 // 5 = 3

This browser demonstration models Python integer arithmetic for small values. Division results may be rounded for display.

2. Comparison operators

A comparison asks whether a relationship is true. Comparing ordinary numbers returns a Boolean: True or False. These are Python keywords and must be capitalized exactly.

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than5 > 3True
<Less than5 < 3False
>=Greater than or equal to5 >= 5True
<=Less than or equal to5 <= 3False

Worked example: checking a quiz score

score = 70
print(score >= 70)
print(score == 100)
print(score != 0)

The outputs are True, False, True. A score of 70 meets the inclusive threshold, is not 100, and is different from zero. The single equals sign assigns a value; two equals signs compare values.

Python can compare compatible values. For example, 5 == 5.0 is True, but 5 == "5" is False because the latter value is text. Ordering an integer and a string with < raises TypeError.

Interactive experiment: compare numbers

First integer Comparison Second integer

5 == 3: False

3. Logical operators

Logical operators combine or reverse conditions. With Boolean operands, and is true only when both are true, or is true when at least one is true, and not reverses a Boolean value.

ABA and BA or B
FalseFalseFalseFalse
FalseTrueFalseTrue
TrueFalseFalseTrue
TrueTrueTrueTrue

not True gives False; not False gives True.

Worked example: completing a learning activity

score = 85
submitted = True
completed = score >= 70 and submitted
print(completed)

First compare 85 with 70: True. Then combine True and True: True. Both requirements are satisfied.

has_book = False
has_notes = True
print(has_book or has_notes)
print(not has_book)

Both outputs are True. The first condition requires at least one resource; the second reverses False.

Python short-circuits logical expressions: it skips the right operand of and when the left operand is falsy, and skips the right operand of or when the left operand is truthy. For non-Boolean operands, and and or can return an operand rather than a Boolean. For example, 0 or 5 returns 5. In this lesson, use Boolean conditions to keep results clear.

Interactive experiment: Boolean logic

Condition A Condition B

A and B: False; A or B: True; not A: False

4. Operator precedence

Python evaluates operators in a defined order. For the operators in this lesson, parentheses control grouping; exponentiation comes before multiplication, division, floor division and modulo; these come before addition and subtraction; comparisons come before not, then and, then or. Operators at the same arithmetic level generally evaluate left to right, except exponentiation, which groups right to left. Unary minus has a special relationship with powers: -2 ** 2 is -4, whereas (-2) ** 2 is 4.

Worked example

print(2 + 3 * 4)
print((2 + 3) * 4)
print(2 ** 3 ** 2)
print(3 + 2 > 4 and not False)

The outputs are 14, 20, 512, and True. In the last expression, addition produces 5, comparison produces True, not False produces True, and the two Boolean values combine to True. Parentheses can make complex expressions easier to read.

Checking a range

score = 85
print(0 <= score <= 100)

Python supports chained comparisons. This expression checks both bounds and returns True. It is equivalent here to 0 <= score and score <= 100.

5. Practice exercises

Predict each result before running the code. Open the solution only after attempting the question.

  1. Evaluate 12 + 4 * 3.
    Solution

    Multiply 4 by 3 to get 12, then add 12. Result: 24.

  2. Find 23 // 5 and 23 % 5.
    Solution

    23 = 5 * 4 + 3. Results: 4 and 3.

  3. Evaluate 3 ** 4.
    Solution

    3 * 3 * 3 * 3 = 81.

  4. Evaluate 7 <= 7 and 7 != 7.
    Solution

    Equality is allowed by <=, so True. The values are equal, so != gives False.

  5. Evaluate True and False, True or False, and not True.
    Solution

    False, True, False, respectively.

  6. Evaluate (6 + 2) * 3.
    Solution

    Parentheses give 8, then multiplication gives 24.

  7. Check whether a score of 105 is between 0 and 100 inclusive.
    Solution

    0 <= 105 <= 100 returns False because 105 exceeds 100.

  8. Explain why 10 / 0 fails.
    Solution

    Division by zero is undefined. Python raises ZeroDivisionError.

  9. Evaluate -7 // 3 and -7 % 3.
    Solution

    Floor division rounds down to -3. The remainder is 2 because -7 = 3 * (-3) + 2.

  10. Given score = 75 and submitted = False, evaluate score >= 70 and submitted.
    Solution

    The score condition is True, but submitted is False. Both are required, so the result is False.

6. Common mistakes and key takeaways

  • Use ** for powers, not ^ (bitwise XOR).
  • Use == to compare and = to assign.
  • Use and, or, not rather than &&, ||, or !.
  • Use parentheses to make grouping clear.
  • Remember that / and // are different and that zero cannot be a divisor.
  • Use True and False with the correct capitalization.

Arithmetic operators calculate values, comparison operators test relationships, and logical operators combine conditions. Together, they form the foundation for Python conditional statements and more complex programs.

Open the original interactive version

Use the lesson text above to open links to other pages.

Definitions

Operator

A symbol or keyword that performs an operation on one or more values.

Operand

A value or expression on which an operator acts.

Expression

A combination of values, variables, and operations that Python evaluates to produce a value.

Arithmetic Operator

An operator used to perform mathematical calculations.

True Division

Division using the / operator. Dividing two integers with this operator produces a floating-point result.

Floor Division

Division using the // operator, which rounds the quotient toward negative infinity.

Modulo

The % operator, which calculates the remainder associated with floor division.

Exponentiation

The operation of raising a number to a power using the ** operator.

Comparison Operator

An operator that compares values, normally producing True or False for built-in scalar values.

Boolean

A data type representing the logical values True and False.

Logical Operator

An operator used to combine or reverse conditions, such as and, or, and not.

Assignment

The process of binding a value to a variable using the = symbol.

Operator Precedence

The rules that determine how operators are grouped within an expression.

Truth Table

A table showing the results of logical operations for all possible Boolean input combinations.

Short-Circuit Evaluation

The behavior of and and or in which Python skips evaluating the second operand when the first operand determines the result.

Chained Comparison

A Python expression containing consecutive comparisons, such as 0 <= score <= 100.

ZeroDivisionError

An exception raised when Python attempts division or modulo with a zero divisor.

Worked examples

Performing Basic Arithmetic

Evaluate the Python expression 8 + 3 * 2.

Multiplication has higher precedence than addition. First calculate 3 * 2 = 6. Then calculate 8 + 6 = 14. The result is 14.

Sharing Pencils

There are 17 pencils and 5 students. Use floor division and modulo to calculate how many whole pencils each student receives and how many remain.

17 // 5 returns 3. Each student receives 3 pencils. 17 % 5 returns 2, so 2 pencils remain. Check: 5 * 3 + 2 = 17.

True Division and Floor Division

Compare the results of 7 / 2 and 7 // 2.

7 / 2 returns 3.5 because true division preserves the fractional part. 7 // 2 returns 3 because floor division rounds the quotient toward negative infinity.

Floor Division with Negative Numbers

Evaluate -7 // 3 and -7 % 3.

The mathematical quotient is approximately -2.333. Rounding toward negative infinity gives -3. Therefore, -7 // 3 returns -3. The remainder is -7 - 3 * (-3) = 2, so -7 % 3 returns 2.

Calculating the Area of a Square

A square has a side length of 4 units. Calculate its area using exponentiation.

Use side = 4 and area = side ** 2. Python calculates 4 * 4 = 16. The area is 16 square units.

Checking a Quiz Score

A learner receives a score of 70. Evaluate score >= 70.

The >= operator means greater than or equal to. Since the score equals 70, the comparison returns True.

Combining Two Conditions

Given score = 85 and submitted = True, evaluate score >= 70 and submitted.

The comparison 85 >= 70 returns True. The submitted variable is also True. True and True returns True because both conditions are satisfied.

Using the OR Operator

Given has_book = False and has_notes = True, evaluate has_book or has_notes.

The or operator returns True for Boolean operands when at least one operand is True. Since has_notes is True, the result is True.

Reversing a Boolean Value

Given completed = False, evaluate not completed.

The not operator reverses the truth value of its operand. Since completed is False, not completed returns True.

Using Parentheses

Compare 2 + 3 * 4 with (2 + 3) * 4.

In the first expression, multiplication is performed before addition, giving 2 + 12 = 14. In the second expression, parentheses group the addition first, giving 5 * 4 = 20.

Checking a Numerical Range

Given score = 85, evaluate 0 <= score <= 100.

Python checks both 0 <= 85 and 85 <= 100. Both comparisons are True, so the chained comparison returns True.

Common mistakes to avoid

  • Using = instead of == when comparing values.
  • Using ^ instead of ** to calculate powers.
  • Confusing true division / with floor division //.
  • Assuming floor division always truncates toward zero.
  • Confusing the modulo operator % with percentage calculations.
  • Attempting to divide by zero.
  • Writing true or false instead of True or False.
  • Using && instead of and.
  • Using || instead of or.
  • Using ! instead of not.
  • Assuming and and or always return Boolean values for non-Boolean operands.
  • Ignoring operator precedence.
  • Forgetting parentheses when a different grouping is required.
  • Assuming that the or operator requires exactly one true condition.
  • Confusing numerical values with strings containing digits.
  • Comparing incompatible data types using ordering operators.

Practice exercises

Arithmetic Precedence

Predict the result of 12 + 4 * 3.

Hint: Multiplication is performed before addition.

Show solution

4 * 3 = 12. Then 12 + 12 = 24. The result is 24.

Quotient and Remainder

Calculate 23 // 5 and 23 % 5.

Hint: Determine how many complete groups of 5 fit into 23.

Show solution

23 = 5 * 4 + 3. Therefore, 23 // 5 returns 4 and 23 % 5 returns 3.

Calculating Powers

Evaluate 3 ** 4.

Hint: Multiply four factors equal to 3.

Show solution

3 * 3 * 3 * 3 = 81. The result is 81.

Comparing Equal Values

Evaluate 7 <= 7 and 7 != 7 separately.

Hint: The <= operator includes equality, while != means not equal.

Show solution

7 <= 7 returns True because equality is allowed. 7 != 7 returns False because the values are equal.

Boolean Logic

Evaluate True and False, True or False, and not True.

Hint: Use the Boolean truth tables.

Show solution

True and False returns False. True or False returns True. not True returns False.

Using Parentheses

Predict the result of (6 + 2) * 3.

Hint: Evaluate the expression inside the parentheses first.

Show solution

6 + 2 = 8. Then 8 * 3 = 24. The result is 24.

Checking a Range

Use a chained comparison to determine whether a score of 105 is between 0 and 100 inclusive.

Hint: Use the expression 0 <= score <= 100.

Show solution

0 <= 105 is True, but 105 <= 100 is False. Therefore, 0 <= 105 <= 100 returns False.

Division by Zero

Explain what happens when Python evaluates 10 / 0.

Hint: Zero is not a valid divisor.

Show solution

Python raises ZeroDivisionError because division by zero is undefined.

Negative Floor Division

Evaluate -7 // 3 and -7 % 3.

Hint: Floor division rounds toward negative infinity.

Show solution

The floor quotient is -3. The remainder is -7 - 3 * (-3) = 2. The results are -3 and 2.

Combining Practical Conditions

Given score = 75 and submitted = False, evaluate score >= 70 and submitted.

Hint: Both conditions must be true for Boolean and to return True.

Show solution

75 >= 70 returns True, but submitted is False. True and False returns False.

Key takeaways

  • Operators allow Python to perform calculations and evaluate conditions.
  • Operands are the values on which operators act.
  • Arithmetic operators include +, -, *, /, //, %, and **.
  • True division and floor division perform different operations.
  • Floor division rounds toward negative infinity.
  • Modulo calculates the remainder associated with floor division.
  • Exponentiation uses ** rather than ^.
  • Comparison operators normally return Boolean results for ordinary numerical values.
  • The = symbol assigns a value, while == compares values.
  • Boolean and requires both conditions to be true.
  • Boolean or requires at least one condition to be true.
  • The not operator reverses a truth value.
  • Python supports short-circuit evaluation.
  • Parentheses make expression grouping explicit.
  • Operator precedence determines how expressions are grouped.
  • Chained comparisons provide a concise way to check ranges.
  • Operators form the foundation of conditional statements and algorithms.