Skip to contentSkip to navigation

Module 5 of 6, Python Foundations

Functions

Name a piece of work, pass it arguments, and let a function call itself.

module checksSign in to track

  • Lessons complete-, not done
  • Lesson quizzes taken-, not done
  • Module Labs passed-, not done
  • Module exam passed at 80%-, not done
lessons
3
quiz questions
36
reading time
25 min
exam length
15 Q
coding exercises
7

Lessons

  1. Functions & Modular CodePackage reusable code with def: give a function parameters, call it with arguments, send results back with return, and document it with a docstring. 8 min 12 questions
  2. Positional & Keyword ArgumentsMake functions flexible: pass arguments by position or by name, give parameters default values, avoid the shared default list trap, and accept any number of arguments with *args and **kwargs. 8 min 12 questions
  3. Recursion & the Call StackUnderstand where variables live, local or global, and solve problems with functions that call themselves: base cases, recursive cases, the call stack and RecursionError. 9 min 12 questions

Lab: write it yourself

Real Python, real checks: write your answer, press Run checks, and keep going until it passes. (4 exercises)

All module exercises

The Lab needs an account. Sign in or create a free ID to run these exercises.

Lab 1: Write double()

Sign-in required

Write a function double(n) that RETURNS n multiplied by 2, then print double(21).

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.

Lab 2: Celsius to Fahrenheit

Sign-in required

Write to_f(c) returning c × 9/5 + 32, rounded to 1 decimal. Print to_f(37).

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.

Lab 3: Greeting with a default

Sign-in required

Write greet(name, greeting="Assalamu alaikum") returning "greeting, name!". Print greet("Mim") and then greet("Rafi", "Hello"), two lines.

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.

Lab 4: Sum any number of bills

Sign-in required

Write total(*bills) that accepts ANY count of amounts and returns their sum. Print total(120, 80.5, 60).

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.

References & Further Reading

Related entries from PyLabs's own reference library.

  • callable() (Check whether an object can be called with parentheses.)
  • locals() (Get the local names currently available in this part of the code.)
  • sorted() (Make a new list containing items in sorted order.)