Lesson 7 of 30

In progress

Course progress

Python

Comparisons, and, or and not

Compare values with confidence, combine conditions with and, or and not, and understand why some values count as false.

Two conditions that look the same can behave differently: if name: and if name == "Ana": are not the same question. Getting comparisons right is what makes a decision trustworthy.

What you will learn

  • Use ==, !=, <, >, <= and >= correctly.
  • Combine conditions with and, or and not, and know which one wins.
  • Recognise the values Python treats as false.

Equality is not identity

== asks whether two values are equal. is asks whether they are the same object in memory. For numbers, strings and booleans, use == almost always.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

10 == 10.0 is true because the numeric values are equal: Python widens the integer for the comparison. 10 == "10" is false because a number is not text. Do not "fix" that with int(c) unless you actually want to convert; comparing different types is often a sign that a value arrived from input() without conversion.

Text comparison is case-sensitive: "Ana" == "ana" is false. Normalise with .lower() or .casefold() when the comparison should ignore case.

Ordering operators

<, >, <= and >= work on numbers, text and dates. Text is compared character by character using Unicode order, which is rarely what a person means by alphabetical.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

The last line is true because uppercase letters sort before lowercase ones. When sorting names for display, use sorted(names, key=str.lower) later in the track.

You can chain comparisons, which reads better than joining two of them:

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

Combining conditions

and is true only when both sides are true. or is true when at least one side is true. not flips a boolean.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

Python stops evaluating as soon as the answer is certain. In age >= 18 and has_permission, if the first part is false, the second is never checked. This short-circuit behaviour is useful, not just an optimisation: it lets you guard an expression.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

The name and part prevents the rest from mattering when the name is empty. It is not needed here because len("") is safe, but the pattern protects calls that would fail on empty input.

and binds tighter than or, just as multiplication binds tighter than addition. a or b and c means a or (b and c). Add brackets when a reader could hesitate.

Truthiness: what counts as false

Python treats these as false: False, None, 0, 0.0, "", [], {}, set() and range(0). Everything else is true.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

"0" is true because it is a non-empty string. [0] is true because it is a non-empty list. This trips people up when they mean "the value is zero" but write if value: and catch every non-empty thing.

When the difference matters, be explicit: if count > 0: rather than if count:. If a function expects a list, an empty list is still a list; test is not None when presence, rather than length, is the question.

Writing if answer == "yes" or "y" looks reasonable and is always true. Python evaluates "y" as a non-empty string, which is truthy, so the condition succeeds for every answer. Write if answer in ("yes", "y"): instead.

A learner can enter the lab if they are at least 13 and either have a permission slip or are accompanied by a guardian. Write the condition and test it with three different combinations of values.

Show solution
Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

The brackets matter. Without them, and runs before or, so a 10-year-old with a slip would be allowed. Change the three values and run it again to see each branch.

Recap

  • == compares values; is compares identity and is rarely what you want for text or numbers.
  • and requires both sides, or requires one, not flips, and brackets control the order.
  • Empty and zero values are false, so write explicit comparisons when zero is meaningful.

Next, you will repeat work without copying lines, starting with for loops and range.

Why is '0' true in Python?
Because it is a non-empty string. Truthiness asks whether the container is empty, not whether the content looks like a number. Convert it with int() if you mean numerically zero.
Does and always check both sides?
No. It stops at the first false value, and or stops at the first true value. That is called short-circuit evaluation.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.