LevelJar Sign in

Python course / Foundations

Functions

Turn a long script into named functions that take arguments and hand answers back. The default argument that quietly fills up between calls is the trap that catches everyone exactly once.

  • arguments
  • defaults
  • return vs print
  • scope
  • *args and **kwargs
  • recursion
  • docstrings
  • one job each

Start this module

Runs in your browser. Start straight away; create an account when you want your progress kept.

one job each, and a value handed back
def clean(name):
    """Tidy the spacing and normalise the case of a name."""
    return " ".join(name.split()).title()


def initials(name, separator="."):
    return separator.join(part[0] for part in name.split())


person = clean("  ada  lovelace ")
print(person)
print(initials(person))
print(initials(person, separator=" "))
Ada Lovelace
A.L
A L

What you will learn

The lessons in this module

  • Making a function
  • Arguments
  • return or print
  • Returning early
  • Default arguments
  • Calling by name
  • Functions calling functions
  • Names inside a function
  • Changing what you were passed
  • The default that remembers
  • Functions as values
  • Functions made in a loop
  • Naming and docstrings
  • One job per function
  • Any number of arguments
  • Any number of named arguments
  • Unpacking at the call
  • A function that calls itself
  • Building a list in a function
  • Functions inside functions
  • Checking the arguments
  • Same arguments, same answer
  • Arguments that must be named
  • The shape of the answer
  • A toolkit of text functions
  • Functions over records
  • Functions in a pipeline
  • Checking your own functions
  • A program made of functions
  • Tier review: the duty roster

Ready when you are

Start this module

Previous: Dictionaries, sets, tuples. Next: Errors and exceptions. All modules are on the course page.