Syntax
== != < > <= >= and or notExamples
Comparison Operators
Comparing values to produce a boolean result.
print(5 == 5) # True
print(5 != 3) # True
print(5 > 3) # True
print(5 <= 4) # False
print("abc" == "abc") # True
print("abc" < "abd") # True (lexicographic comparison)Logical Operators
Combining boolean expressions with and, or, and not.
age = 25
has_id = True
if age >= 18 and has_id:
print("Entry allowed")
is_weekend = False
is_holiday = True
if is_weekend or is_holiday:
print("No work today")
is_banned = False
if not is_banned:
print("Access granted")Chained Comparisons
Writing range checks concisely by chaining comparison operators.
age = 25
# Instead of: age >= 18 and age <= 65
if 18 <= age <= 65:
print("Working age")
score = 72
grade = "Pass" if 50 <= score < 100 else "Check score"
print(grade)Best practices
- Use 'and'/'or'/'not' rather than symbolic operators - Python doesn't support && or ||
- Take advantage of chained comparisons (0 < x < 10) instead of writing (x > 0 and x < 10)
- Remember that 'and'/'or' short-circuit - the second operand isn't evaluated if the first already determines the result
- Use 'is'/'is not' for identity checks (like comparing to None), and '=='/'!=' for value equality
At a glance
- Purpose
- Scripting and general-purpose applications
- File extension
- .py
- Runs in
- Python interpreter
- Usually used with
- Python standard library and packages
Specifications & further reading
Related Python documentation
True
In Python, True is a built-in constant that represents the boolean value of true. Alongside False, True enables logical and conditional operations. It's a fundamental keyword used throughout Python programming for decision-making, loops, comparisons, and boolean logic. True plays a key role in if statements, while loops, and logical expressions.False
False is Python's built-in constant representing the boolean value false. It's the logical opposite of True and is essential for conditional logic, loop control, and boolean operations. False is used to indicate negative conditions, failed validations, or when something is not the case. Understanding False is crucial for writing effective control flow in Python programs.None
None is Python's built-in singleton value that represents the absence of a value or a null result. It is its own type (NoneType) and is commonly used as a default parameter value, a placeholder before a variable is assigned real data, or a function's implicit return value when nothing is explicitly returned. Unlike 0, an empty string, or an empty list, None specifically means 'no value here', not 'an empty value'.Variables & Assignment
Variables in Python are names bound to values, created the moment you first assign to them - there's no need to declare a type in advance, since Python is dynamically typed. A single variable can be reassigned to a completely different type later in the program. Python also supports several convenient assignment patterns, including assigning multiple variables at once and using augmented assignment operators.
In Python, True is a built-in constant that represents the boolean value of true. Alongside False, True enables logical and conditional operations. It's a fundamental keyword used throughout Python programming for decision-making, loops, comparisons, and boolean logic. True plays a key role in if statements, while loops, and logical expressions.False
False is Python's built-in constant representing the boolean value false. It's the logical opposite of True and is essential for conditional logic, loop control, and boolean operations. False is used to indicate negative conditions, failed validations, or when something is not the case. Understanding False is crucial for writing effective control flow in Python programs.None
None is Python's built-in singleton value that represents the absence of a value or a null result. It is its own type (NoneType) and is commonly used as a default parameter value, a placeholder before a variable is assigned real data, or a function's implicit return value when nothing is explicitly returned. Unlike 0, an empty string, or an empty list, None specifically means 'no value here', not 'an empty value'.Variables & Assignment
Variables in Python are names bound to values, created the moment you first assign to them - there's no need to declare a type in advance, since Python is dynamically typed. A single variable can be reassigned to a completely different type later in the program. Python also supports several convenient assignment patterns, including assigning multiple variables at once and using augmented assignment operators.