Course topics

By WebNest Studio Engineering Team

Python Tutorial

Python Operators

Operators are symbols that tell Python to perform a specific computation or comparison on one or more values. Python groups operators into families — arithmetic, comparison, and logical — and each family has its own rules and behaviors that are important to get exactly right.

One area where Python differs noticeably from many other languages is division: Python has two distinct division operators, one that always returns a float-style result and one that always rounds down to a whole number, and mixing them up is a very common source of bugs.

Why it matters

Operators are how conditions and calculations are written, and a few of Python's behave differently from other languages: and and or are words, comparisons can be chained, and is is not the same as ==. Mixing up the last pair is a classic bug that works in testing and fails later.

Arithmetic Operators

Python supports the standard arithmetic operators: + (addition), - (subtraction), * (multiplication), / (true division, always returns a float), // (floor division, rounds down to the nearest whole number), % (modulus, the remainder after division), and ** (exponentiation, e.g. 2 ** 3 is 8). Floor division and modulus both follow the sign of the divisor, which can produce results that surprise programmers coming from other languages when negative numbers are involved.

Comparison Operators

Comparison operators — == != > < >= <=</code> — compare two values and always produce a bool (True or False). == checks for equality of value, not identity; to check whether two names refer to the literally same object in memory, Python provides a separate keyword, is.

Logical Operators

Python spells out its logical operators as words rather than symbols: and, or, and not. Like most languages, and and or short-circuit — Python stops evaluating as soon as the overall result is determined, which matters when the right-hand expression has a side effect or could raise an error.

Examples

True division vs. floor division, modulus, and exponentiation

Python
print(7 / 2)     # true division, always float
print(7 // 2)    # floor division, rounds down
print(7 % 2)     # modulus/remainder
print(2 ** 5)    # exponentiation
print(-7 // 2)   # floor division rounds toward negative infinity
print(-7 % 2)    # follows the sign of the divisor
Output
3.5
3
1
32
-4
1

Short-circuit logical operators and comparison chaining

Python
age = 20
has_id = False

print(age >= 18 and has_id)
print(age >= 18 or has_id)
print(not has_id)

# Python allows chained comparisons
print(1 < age < 65)
Output
False
True
True
True

Common Mistakes

  • Using / when floor division (//) was intended, producing an unexpected float where a whole number was needed (or vice versa).
  • Forgetting that -7 // 2 is -4, not -3 — Python floor division always rounds toward negative infinity, not toward zero.
  • Using == to check whether two variables point to the exact same object instead of the "is" operator, or vice versa.
  • Writing "if x = 5:" out of habit from other languages — Python uses = only for assignment and == for comparison; using = in a condition is a SyntaxError.

Exercise

Write a function that returns True for a leap year. A year is a leap year if it is divisible by 4, except that years divisible by 100 are leap years only if they are also divisible by 400.

Expected output: True, False, True (one per line)

Python · starter code
def is_leap(year):
    # TODO: return True if the year is a leap year
    pass


print(is_leap(2024))
print(is_leap(1900))
print(is_leap(2000))
Show hints
  • year % 4 == 0 is true when the year is divisible by 4.
  • Combine the rules: divisible by 4 and (not divisible by 100 or divisible by 400).
Show solution
Python · solution
def is_leap(year):
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)


print(is_leap(2024))
print(is_leap(1900))
print(is_leap(2000))

Quick quiz

1. What is the value of 2 ** 3 ** 2?

  • A. 64
  • B. 36
  • C. 512
  • D. 12
Show answer

C. Exponentiation is evaluated from right to left: 3 ** 2 is 9, and 2 ** 9 is 512.

2. What is the value of -7 % 3?

  • A. -1
  • B. 1
  • C. -2
  • D. 2
Show answer

D. In Python the remainder takes the sign of the divisor: -7 is 3 × (-3) + 2.

3. What does 1 < 5 < 3 evaluate to?

  • A. False
  • B. True
  • C. It raises an error
  • D. 1
Show answer

A. A chained comparison means 1 < 5 and 5 < 3, and the second part is false.

Interview questions

What is the difference between == and is?

Show answer

== compares values: two different list objects with the same contents are equal. is compares identity: it is true only when both names refer to the very same object. Use == for values and reserve is for None, True and False. Small integers and some strings are cached by the interpreter, so is may appear to work for them, but that must not be relied on.

What do the and and or operators return?

Show answer

They return one of their operands, not necessarily True or False. a and b returns a if it is falsy, otherwise b. a or b returns a if it is truthy, otherwise b. This is why name or "Guest" works as a way of supplying a default.

Key Points to Remember

  • / always returns a float (true division); // performs floor division and rounds toward negative infinity.
  • % returns the remainder and follows the sign of the divisor; ** performs exponentiation.
  • Logical operators are the words and, or, not, and and/or short-circuit evaluation.
  • Use == for value equality and "is" for object identity — they are not interchangeable.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering