Functions: Interactive Python Lessons
Write reusable code with Python functions: parameters, return values, scope, default arguments, and lambdas — each concept practiced in a real code editor with instant grading.
63 lessons in this topic. Every lesson runs real Python in your browser with instant feedback.
Functions
Create reusable code with functions
Function with Parameters
Create functions that take inputs
Multiple Return Values
Return multiple values from functions
Default Parameters
Set default values for parameters
Max and Min
Find largest and smallest values
Sum Function
Add all numbers in a list using the sum function.
Enumerate Function
Get index and value in loops
Zip Function
Combine multiple lists
Sorted Function
Sort lists without changing original
Reversed Function: Reversing List Order
Learn to reverse the order of items in a list using the reversed() function in Python.
Lambda Functions
Create small anonymous functions
Map Function
Apply function to all list items
Filter Function
Filter list items by condition
Square of 9
Write square(x) returning x*x, then print square(9).
Default argument adds 1
Write add(a, b=1) returning a+b. Print add(9).
Sum any count of args (3 numbers)
Write total(*nums) returning their sum. Print total of [1, 2, 3].
Return two values (min & max)
Write minmax(nums) returning (min, max). Print it for [1, 2, 3].
Lambda triples 9
Make a lambda `triple` that returns x*3, then print triple(9).
Function returning a string
Write shout(s) returning s in uppercase with '!'. Print shout('python').
Recursive factorial of 9
Write a recursive factorial(n) and print factorial(9).
Square of 7
Write square(x) returning x*x, then print square(7).
Default argument adds 5
Write add(a, b=5) returning a+b. Print add(7).
Sum any count of args (4 numbers)
Write total(*nums) returning their sum. Print total of [4, 5, 6, 7].
Return two values (min & max) ([4, 5, 6, 7])
Write minmax(nums) returning (min, max). Print it for [4, 5, 6, 7].
Lambda triples 7
Make a lambda `triple` that returns x*3, then print triple(7).
Function returning a string ([4, 5, 6, 7])
Write shout(s) returning s in uppercase with '!'. Print shout('hello').
Recursive factorial of 7
Write a recursive factorial(n) and print factorial(7).
Square of 6
Write square(x) returning x*x, then print square(6).
Default argument adds 2
Write add(a, b=2) returning a+b. Print add(6).
Sum any count of args (3 numbers) ([10, 20, 30])
Write total(*nums) returning their sum. Print total of [10, 20, 30].
Return two values (min & max) ([10, 20, 30])
Write minmax(nums) returning (min, max). Print it for [10, 20, 30].
Lambda triples 6
Make a lambda `triple` that returns x*3, then print triple(6).
Function returning a string ([10, 20, 30])
Write shout(s) returning s in uppercase with '!'. Print shout('clean').
Recursive factorial of 6
Write a recursive factorial(n) and print factorial(6).
Square of 5
Write square(x) returning x*x, then print square(5).
Default argument adds 3
Write add(a, b=3) returning a+b. Print add(5).
Sum any count of args (5 numbers)
Write total(*nums) returning their sum. Print total of [2, 2, 2, 2, 2].
Return two values (min & max) ([2, 2, 2, 2, 2])
Write minmax(nums) returning (min, max). Print it for [2, 2, 2, 2, 2].
Lambda triples 5
Make a lambda `triple` that returns x*3, then print triple(5).
Function returning a string ([2, 2, 2, 2, 2])
Write shout(s) returning s in uppercase with '!'. Print shout('data').
Recursive factorial of 5
Write a recursive factorial(n) and print factorial(5).
Square of 8
Write square(x) returning x*x, then print square(8).
Default argument adds 10
Write add(a, b=10) returning a+b. Print add(8).
Sum any count of args (3 numbers) ([9, 1, 5])
Write total(*nums) returning their sum. Print total of [9, 1, 5].
Return two values (min & max) ([9, 1, 5])
Write minmax(nums) returning (min, max). Print it for [9, 1, 5].
Lambda triples 8
Make a lambda `triple` that returns x*3, then print triple(8).
Function returning a string ([9, 1, 5])
Write shout(s) returning s in uppercase with '!'. Print shout('spark').
Recursive factorial of 8
Write a recursive factorial(n) and print factorial(8).
Square of 4
Write square(x) returning x*x, then print square(4).
Default argument adds 4
Write add(a, b=4) returning a+b. Print add(4).
Sum any count of args (4 numbers) ([3, 6, 9, 12])
Write total(*nums) returning their sum. Print total of [3, 6, 9, 12].
Return two values (min & max) ([3, 6, 9, 12])
Write minmax(nums) returning (min, max). Print it for [3, 6, 9, 12].
Lambda triples 4
Make a lambda `triple` that returns x*3, then print triple(4).
Function returning a string ([3, 6, 9, 12])
Write shout(s) returning s in uppercase with '!'. Print shout('cloud').
Recursive factorial of 4
Write a recursive factorial(n) and print factorial(4).
Square of 10
Write square(x) returning x*x, then print square(10).
Default argument adds 2 ([2, 3, 4])
Write add(a, b=2) returning a+b. Print add(10).
Sum any count of args (4 numbers) ([2, 3, 4])
Write total(*nums) returning their sum. Print total of [2, 3, 4].
Return two values (min & max) ([2, 3, 4])
Write minmax(nums) returning (min, max). Print it for [2, 3, 4].
Lambda triples 10
Make a lambda `triple` that returns x*3, then print triple(10).
Recursive factorial of 10
Write a recursive factorial(n) and print factorial(10).
Default argument adds 6
Write add(a, b=6) returning a+b. Print add(8).
Sum any count of args (5 numbers) ([5, 6, 7, 8])
Write total(*nums) returning their sum. Print total of [5, 6, 7, 8].