Conditions choose which code runs. Loops repeat work.

Conditions

status = 200

if status == 200:
    print("OK")
elif status == 404:
    print("Not found")
else:
    print("Other response")

Use and, or and not to combine boolean expressions. Prefer clear comparisons over deeply nested logic.

For loops

ports = [22, 80, 443]
for port in ports:
    print(f"Checking local configuration for {port}")

While loops

attempts = 3
while attempts > 0:
    print(attempts)
    attempts -= 1

Ensure every while loop has a reachable stopping condition. When processing files or network results, apply limits so malformed input cannot cause unbounded work.

05 · Python Basics

Continue learning

Back to Smartphone Academy