Operators & Expressions
Overview
Operators are symbols that let you perform math, compare values, and combine logical conditions.
Learning Objectives
- Use mathematical operators (+, -, *, /, //, %.
- Compare values using equality and inequality symbols.
- Combine multiple checks using and, or, and not.
Concept Explanation
Operators carry out operations. Math symbols include standard arithmetic along with // (floor division, which discards decimals) and % (modulo, which returns the remainder of a division). Comparison operators check equality (==) or inequality (!=). Logical operators let you join checks together.
Code Examples
Example 1 — Basics
This example introduces the fundamental syntax and concepts.
addition = 5 + 3
is_greater = 10 > 7
both_true = True and False
Example 2 — Everyday Usage
This example demonstrates a realistic scenario handling business parameters.
# Calculate leftover items
total_apples = 13
basket_capacity = 4
baskets_filled = total_apples // basket_capacity
leftovers = total_apples % basket_capacity
print('Baskets:', baskets_filled, 'Leftovers:', leftovers)
Example 3 — Advanced Example
This example shows clean, production-grade code structure following senior development standards.
# Verify user permission and system access
user_role = 'admin'
is_logged_in = True
has_permission = is_logged_in and (user_role == 'admin' or user_role == 'manager')
print('Access allowed:', has_permission)
Visual Flow
The following execution flow represents the step-by-step evaluation inside the interpreter:
Evaluate Left Expression → Check Logical operator → Skip Right if Left suffices (Short-Circuit) → Return final value
Common Mistakes
Review these common pitfalls when working with this topic:
- Using a single equals sign (=) for checking equality instead of double equals (==).
- Confusing floor division // with regular division /.
- Forgetting parentheses when mixing 'and' with 'or' in complex expressions.
- Using the identity check 'is' when you only want to compare values for equality.
Best Practices
Enforce these Pythonic best practices in your codebase:
if is_ready == True: # Redundant check
if is_ready: # Clean boolean check
Performance Notes
Keep these optimization guidelines in mind for performance-sensitive hotpaths:
- Logical operators (and, or) evaluate lazily (short-circuiting), stopping early if the first check satisfies the expression.
Quick Revision
Use these key summaries for last-minute revision:
- +, -, *, / perform standard math operations.
- Floor division (//) removes decimal fractional parts.
- Modulo (%) yields the remainder of a division.
- == checks if values are equal; != checks inequality.
- and/or operators stop checking early if the outcome is already clear.
DataPlayArena Technical Editorial Board
Curated & maintained by enterprise data platform engineers with 10+ years in distributed systems.
Runtime Verification Standards: Every code example, query, and architecture diagram in this tutorial is compiled and verified against real runtimes (Apache Spark 3.5, Python 3.11, PostgreSQL, and DuckDB). If you spot any errata or want to suggest improvements, please submit feedback.