Skip to content
Learn Netverks

Lesson

Step 8/36 22% through track

control-flow-functions

Control flow and functions

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Control flow and functions: the syntax, APIs, and habits you need before advancing in Django.

Functions keep scripts maintainable before you reach for classes and frameworks.

You will apply Control flow and functions in contexts like: SaaS dashboards, CMS-style products, internal tools, and APIs paired with React or mobile clients.

Write Python 3 in the editor and click Run on server—the dev runner executes your script; Django framework lessons also use local startproject for full MVT (LEARNING_RUNNER_ENABLED=true).

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

Django views are functions (or classes) that branch on HTTP method, user permissions, and validation results. Loops and conditionals appear in management commands, data migrations, and template logic.

Control flow essentials

  • if/elif/else — permission checks, method dispatch (if request.method == "POST")
  • for loops — iterate querysets, build lists for templates
  • try/except — handle DoesNotExist, validation errors

Functions in Django

def publish_status(article):
    if article.get("published"):
        return "live"
    return "draft"

Keep views thin—extract reusable logic into utility functions or model methods.

Important interview questions and answers

  1. Q: Where to put business logic?
    A: Prefer model methods or service modules over bloated views—easier to test and reuse.
  2. Q: Early return in views?
    A: Common pattern—return redirect or 404 immediately instead of deep nesting.
  3. Q: List comprehension vs loop?
    A: Comprehensions are concise for simple transforms; use loops when logic is complex.

Self-check

  1. How do you handle GET vs POST in a function-based view?
  2. When should logic move out of a view into a model method?

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

  • List comp use?
  • def vs lambda?

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