teluzia.Cloud Engineer · Day 25
CHAPTER 04 · PYTHON FUNDAMENTALS

Functions and modules

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

Move repeated logic into a function and import it from a separate module. A function should have a clear input, output and failure contract. You will test one value at a time and use the main guard so importing a module does not accidentally run its command-line demonstration. def creates a function object with named parameters. Calling it binds arguments and executes the body; return passes a result to the caller. Printing is an observable side effect and is not the same as returning a value. A function that reaches its end without return produces None. Parameters are local names inside a function call. Avoid depending on changing global variables when an explicit argument describes the dependency better. Default argument expressions are evaluated when the function is defined, so a mutable default can retain changes between calls. Use None as a sentinel for a fresh optional collection.

Think of it this way

A function is a reusable recipe; a module is a labeled recipe book; importing opens the book without necessarily cooking dinner.

TOPIC 01 / 03Step by step

Functions and return values

def creates a function object with named parameters. Calling it binds arguments and executes the body; return passes a result to the caller. Printing is an observable side effect and is not the same as returning a value. A function that reaches its end without return produces None. def classify(value): return "warning" if value>=80 else "normal" lets tests inspect the result without capturing terminal output or depending on a printed label. Clear return values make logic reusable in scripts, tests and APIs and separate computation from presentation so callers can choose how to display, store or act on the result in their own context. Replacing return with print changes the function’s contract to None and can break downstream comparisons; use printing only when the function’s explicit responsibility is presentation rather than calculation for other callers.

WORKED EXAMPLE
def classify(value): return "warning" if value>=80 else "normal" lets tests inspect the result without capturing terminal output or depending on a printed label.
Why this matters

Clear return values make logic reusable in scripts, tests and APIs and separate computation from presentation so callers can choose how to display, store or act on the result in their own context.

A common trap

Replacing return with print changes the function’s contract to None and can break downstream comparisons; use printing only when the function’s explicit responsibility is presentation rather than calculation for other callers.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

Functions and return values

def creates a function object with named parameters. Calling it binds arguments and executes the body; return passes a result to the caller. Printing is an observable side effect and is not the same as returning a value. A function that reaches its end without return produces None. def classify(value): return "warning" if value>=80 else "normal" lets tests inspect the result without capturing terminal output or depending on a printed label. Clear return values make logic reusable in scripts, tests and APIs and separate computation from presentation so callers can choose how to display, store or act on the result in their own context. Replacing return with print changes the function’s contract to None and can break downstream comparisons; use printing only when the function’s explicit responsibility is presentation rather than calculation for other callers.

WORKED EXAMPLE
def classify(value): return "warning" if value>=80 else "normal" lets tests inspect the result without capturing terminal output or depending on a printed label.

Why this matters

Clear return values make logic reusable in scripts, tests and APIs and separate computation from presentation so callers can choose how to display, store or act on the result in their own context.

A common trap

Replacing return with print changes the function’s contract to None and can break downstream comparisons; use printing only when the function’s explicit responsibility is presentation rather than calculation for other callers.

Arguments, scope and defaults

Parameters are local names inside a function call. Avoid depending on changing global variables when an explicit argument describes the dependency better. Default argument expressions are evaluated when the function is defined, so a mutable default can retain changes between calls. Use None as a sentinel for a fresh optional collection. def collect(value,items=None): items=[] if items is None else items; items.append(value); return items avoids sharing a default list across unrelated calls to the function. Explicit dependencies and safe defaults prevent state leaking between calls and make small functions predictable enough to test independently before they become building blocks in a larger automation project used repeatedly by others. Using items=[] as a parameter default shares one list across calls that omit it; choose a fresh list inside the function when independent call state is required by the intended contract.

WORKED EXAMPLE
def collect(value,items=None): items=[] if items is None else items; items.append(value); return items avoids sharing a default list across unrelated calls to the function.

Why this matters

Explicit dependencies and safe defaults prevent state leaking between calls and make small functions predictable enough to test independently before they become building blocks in a larger automation project used repeatedly by others.

A common trap

Using items=[] as a parameter default shares one list across calls that omit it; choose a fresh list inside the function when independent call state is required by the intended contract.

Modules and the main guard

A Python file can be imported as a module, which executes its top-level statements once during normal import. The conditional if __name__=="__main__" protects demonstration or CLI behavior so it runs only when that file is executed directly. Keep reusable definitions separate from immediate file writes or network actions. rules.py can define classify and place print(classify(80)) under a main guard; another file imports classify without unexpectedly printing a demonstration or performing unrelated work at import time. Controlled import behavior makes modules safe to reuse and test and prevents a dependency import from unexpectedly starting a server, modifying a file or requesting network access before the caller is ready to act. Putting operational work at module top level makes importing trigger side effects; move execution under a main guard or an explicitly called function and document any unavoidable initialization behavior for callers who reuse it.

WORKED EXAMPLE
rules.py can define classify and place print(classify(80)) under a main guard; another file imports classify without unexpectedly printing a demonstration or performing unrelated work at import time.

Why this matters

Controlled import behavior makes modules safe to reuse and test and prevents a dependency import from unexpectedly starting a server, modifying a file or requesting network access before the caller is ready to act.

A common trap

Putting operational work at module top level makes importing trigger side effects; move execution under a main guard or an explicitly called function and document any unavoidable initialization behavior for callers who reuse it.

KEEP THESE WITH YOU

Today’s takeaways

Return results instead of only printing.

Keep dependencies explicit.

Make modules safe to import.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed