Comparison, logical, identity, and membership operators used to build conditions and control the flow of security scripts.
Part of the [[Readme|Python for Security Professionals]] course.
Comparisons are what turn raw data into decisions. In offensive and defensive tooling they drive scan logic (is this port open and in scope?), input validation (is this a legal port number?), response filtering (is the status a 2xx?), and allow/deny enforcement (is this token in the wordlist?). This module walks the operators from the simplest relational checks up through logical composition, identity, and membership.
- Use
>,<,==,!=and their inclusive variants correctly, including the boundary cases. - Express range checks cleanly with chained comparisons (e.g.
0 <= port <= 65535). - Combine conditions with
and,or,notand exploit short-circuit evaluation for safe, fast gates. - Distinguish identity (
is) from equality (==) and know when each is correct. - Test allow/deny lists and wordlists with
in/not inusing the right container.
- [[Python-Objects-and-Data-Structure-Basics/Readme|Python Objects & Data Structure Basics]] — the types being compared.
- Familiarity with integers, strings, and the collection types.
Work through these in order:
- [[Greater-Than-Operator]] —
>threshold and upper-bound logic. - [[Less-Than-Operator]] —
<lower-bound and privileged-port checks. - [[Equal-To-Operator]] —
==exact matching (status codes, timing-safe token checks). - [[Not-Equal-To-Operator]] —
!=anomaly and drift detection. - [[Chained-Comparisons]] — range validation like
0 <= port <= 65535. - [[Logical-Operators]] —
and,or,notand short-circuit evaluation. - [[Identity-Operators]] —
is/is notvs==, and theNoneidiom. - [[Membership-Operators]] —
in/not infor allowlists and wordlists.
- Validate a user-supplied port with a chained comparison before opening a socket.
- Classify a batch of HTTP status codes into success / redirect / client-error / server-error.
- Gate an attack loop with compound
and/notlogic over in-scope, port-open, honeypot flags. - Screen a candidate password against a wordlist loaded into a
set.
- Port state and scope checks —
0 < port <= 65535validates input before a socket is opened, andport < 1024identifies privileged ports. - HTTP status classification —
200 <= status < 300separates success from redirect and error, which is how content-discovery tools decide what counts as a hit. - Allowlists and blocklists —
host in AUTHORIZED_SCOPEis the check that keeps a scan legal; using asetmakes it O(1). - Short-circuit safety gates —
if in_scope(host) and is_up(host):never probes a host that failed the scope check, becauseandstops at the first false value. - The
Noneidiom —if result is not None:correctly distinguishes "no result" from a legitimate empty or zero result. - Timing-safe comparison — plain
==on secrets short-circuits at the first differing byte. Usehmac.compare_digest()for tokens, MACs, and password digests.
- Using
isinstead of==for value comparison —istests object identity and only works by accident for small integers and interned strings. if x:when you meanif x is not None:— an empty list, an empty string, and0are all falsy, so a legitimate zero result is treated as missing.- Comparing
strtoint—"443" == 443isFalse; convert first. - Forgetting operator precedence —
not a == bmeansnot (a == b); parenthesise when in doubt. - Membership tests against a list where a set would be O(1).
- Comparing floats with
==— usemath.isclose(). - Comparing secrets with
==, leaking length and content through timing.
- Use chained comparisons for range checks; they read like the mathematical notation.
- Reserve
isandis notforNone,True, andFalse. - Order compound conditions so the cheapest and most restrictive test comes first.
- Store allowlists and wordlists in a
setfor fast membership tests. - Use
hmac.compare_digest()whenever comparing anything secret. - Extract complex boolean expressions into a well-named helper function.
- What is the difference between
==andis, and when is each correct? - When should you use a chained comparison rather than two conditions joined by
and? - What security issue can occur if a token is compared with
==instead ofhmac.compare_digest()? - How would you troubleshoot a condition that unexpectedly treats a valid result of
0as missing? - Why is
443 in {80, 443}faster than443 in [80, 443]on large collections? - Write a Python example that validates a port number and rejects out-of-range input.
# Run any example script from this module
python3 example.py
# Quick REPL experiments with the operators
python3 -c 'print(0 <= 8080 <= 65535)'
python3 -c 'print(443 in {80, 443, 8080})'- [[Python-Statements-and-Control-Flow/Readme|Python Statements and Control Flow]] — where these conditions are used
- [[Python-Objects-and-Data-Structure-Basics/Readme|Python Objects and Data Structure Basics]] — the types being compared
- [[Readme|Python for Security Professionals]] — course home
- Previous: [[Python-Objects-and-Data-Structure-Basics/Readme|2. Python Objects & Data Structure Basics]]
- Home: [[Readme|Course Home]]
- Next: [[Python-Statements-and-Control-Flow/Readme|4. Python Statements & Control Flow]]