Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

PY
Python
0/14 completed
Beginner
Hello PythonVariables and typesWorking with stringsConditions and loopsFunctions
Intermediate
Lists, tuples, dicts and setsFiles and exceptionsModules and the standard library
Advanced
Classes and OOPIterators and generatorsDecorators and context managers
Pro
Async PythonTesting and type hintsVirtual environments, packages and real projects
Tracks / Python / Beginner

Functions

A function is a named, reusable block of code. Functions let you write logic once and use it everywhere, give complicated steps a clear name, and test pieces of your program separately.

Defining and calling

Python · editable
  • def starts a function definition, followed by its parameters in brackets.
  • return sends a value back to whoever called it.
  • A function without return returns None.
Python · editable

Parameters and arguments

Default values

Python · editable

Keyword arguments

Name arguments when you call a function, in any order. This makes calls much easier to read:

Python · editable

⚠️ Never use a mutable value such as [] as a default: def add(item, items=[]) shares one list between all calls. Use items=None, then if items is None: items = [].

Any number of arguments: *args and **kwargs

Python · editable

Returning several values

Python functions can return several values at once (as a tuple), which you unpack:

Python · editable

Scope: where variables live

Variables created inside a function are local: they exist only while the function runs.

Python · editable

Pass data in as arguments and get results out with return. Avoid changing global variables from inside functions; it makes programs hard to follow.

Functions are values

You can store functions in variables, pass them to other functions and return them. That's what makes sorted(..., key=...) so powerful:

Python · editable

lambda: small anonymous functions

Python · editable

Use lambda for short, throwaway functions. Anything longer deserves a proper def with a name.

Recursion

A function can call itself. Each call must move towards a base case that stops it:

Python · editable

Writing good functions

  • One job per function, with a name that says what it does (calculate_total, is_valid_email).
  • Return values rather than printing them, so the caller decides what to do.
  • Add a docstring for anything non-obvious.
  • Type hints (covered later) document what goes in and out: def greet(name: str) -> str:

Practice

  1. Write is_even(n) that returns True or False.
  2. Write celsius_to_fahrenheit(c) and print a table for 0, 10, 20, 30 and 40 °C.
  3. Write count_vowels(text) and test it on a few words.
  4. Write fibonacci(n) that returns a list of the first n Fibonacci numbers.
Python · editable
✎ Check your understanding

1.What does a function return if it has no return statement?

2.What does *args collect?

3.Why is def add(item, items=[]) a classic bug?

← Conditions and loops