Skip to content
Learn Netverks

Lesson

Step 10/36 28% through track

functions-python

Functions

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Functions: the syntax, patterns, and safety habits you need before advancing in Python.

Teams still ship Functions in Python codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Functions in contexts like: Scripts, Django/FastAPI apps, notebooks, and glue code between systems.

Write Python 3 in the editor and click Run on server—the dev runner executes your script with print() for output; stdlib only in playground snippets (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Functions are defined with def and called by name. Python supports default arguments, keyword arguments, and *args/**kwargs—flexible like JavaScript but with explicit parameter rules.

Defining and calling

def greet(name, prefix="Hi"):
    return f"{prefix}, {name}!"

print(greet("Ada"))
print(greet("Grace", prefix="Hello"))

Return and scope

Functions return None implicitly if no return statement. Local variables do not leak unless declared global or nonlocal—prefer passing values and returning results.

Important interview questions and answers

  1. Q: Mutable default argument pitfall?
    A: Defaults like def f(x=[]) share one list—use None and create inside the function.
  2. Q: *args vs **kwargs?
    A: *args collects extra positional args as a tuple; **kwargs collects extra keyword args as a dict.

Self-check

  1. What keyword defines a function?
  2. What is returned if a function has no return statement?

Pitfall: Never use mutable defaults like def f(x=[])—use None and create inside the function.

Interview prep

Mutable default pitfall?

def f(x=[]) shares one list—use None default and assign x = [] inside.

*args and **kwargs?

*args tuple of extra positional args; **kwargs dict of extra keyword args.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • *args **kwargs?
  • Lambda limits?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump