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.

advanced

Functions

Create reusable code with functions

intermediate

Function with Parameters

Create functions that take inputs

intermediate

Multiple Return Values

Return multiple values from functions

intermediate

Default Parameters

Set default values for parameters

intermediate

Max and Min

Find largest and smallest values

intermediate

Sum Function

Add all numbers in a list using the sum function.

intermediate

Enumerate Function

Get index and value in loops

intermediate

Zip Function

Combine multiple lists

intermediate

Sorted Function

Sort lists without changing original

intermediate

Reversed Function: Reversing List Order

Learn to reverse the order of items in a list using the reversed() function in Python.

advanced

Lambda Functions

Create small anonymous functions

advanced

Map Function

Apply function to all list items

advanced

Filter Function

Filter list items by condition

intermediate

Square of 9

Write square(x) returning x*x, then print square(9).

intermediate

Default argument adds 1

Write add(a, b=1) returning a+b. Print add(9).

intermediate

Sum any count of args (3 numbers)

Write total(*nums) returning their sum. Print total of [1, 2, 3].

intermediate

Return two values (min & max)

Write minmax(nums) returning (min, max). Print it for [1, 2, 3].

intermediate

Lambda triples 9

Make a lambda `triple` that returns x*3, then print triple(9).

intermediate

Function returning a string

Write shout(s) returning s in uppercase with '!'. Print shout('python').

intermediate

Recursive factorial of 9

Write a recursive factorial(n) and print factorial(9).

intermediate

Square of 7

Write square(x) returning x*x, then print square(7).

intermediate

Default argument adds 5

Write add(a, b=5) returning a+b. Print add(7).

intermediate

Sum any count of args (4 numbers)

Write total(*nums) returning their sum. Print total of [4, 5, 6, 7].

intermediate

Return two values (min & max) ([4, 5, 6, 7])

Write minmax(nums) returning (min, max). Print it for [4, 5, 6, 7].

intermediate

Lambda triples 7

Make a lambda `triple` that returns x*3, then print triple(7).

intermediate

Function returning a string ([4, 5, 6, 7])

Write shout(s) returning s in uppercase with '!'. Print shout('hello').

intermediate

Recursive factorial of 7

Write a recursive factorial(n) and print factorial(7).

intermediate

Square of 6

Write square(x) returning x*x, then print square(6).

intermediate

Default argument adds 2

Write add(a, b=2) returning a+b. Print add(6).

intermediate

Sum any count of args (3 numbers) ([10, 20, 30])

Write total(*nums) returning their sum. Print total of [10, 20, 30].

intermediate

Return two values (min & max) ([10, 20, 30])

Write minmax(nums) returning (min, max). Print it for [10, 20, 30].

intermediate

Lambda triples 6

Make a lambda `triple` that returns x*3, then print triple(6).

intermediate

Function returning a string ([10, 20, 30])

Write shout(s) returning s in uppercase with '!'. Print shout('clean').

intermediate

Recursive factorial of 6

Write a recursive factorial(n) and print factorial(6).

intermediate

Square of 5

Write square(x) returning x*x, then print square(5).

intermediate

Default argument adds 3

Write add(a, b=3) returning a+b. Print add(5).

intermediate

Sum any count of args (5 numbers)

Write total(*nums) returning their sum. Print total of [2, 2, 2, 2, 2].

intermediate

Return two values (min & max) ([2, 2, 2, 2, 2])

Write minmax(nums) returning (min, max). Print it for [2, 2, 2, 2, 2].

intermediate

Lambda triples 5

Make a lambda `triple` that returns x*3, then print triple(5).

intermediate

Function returning a string ([2, 2, 2, 2, 2])

Write shout(s) returning s in uppercase with '!'. Print shout('data').

intermediate

Recursive factorial of 5

Write a recursive factorial(n) and print factorial(5).

intermediate

Square of 8

Write square(x) returning x*x, then print square(8).

intermediate

Default argument adds 10

Write add(a, b=10) returning a+b. Print add(8).

intermediate

Sum any count of args (3 numbers) ([9, 1, 5])

Write total(*nums) returning their sum. Print total of [9, 1, 5].

intermediate

Return two values (min & max) ([9, 1, 5])

Write minmax(nums) returning (min, max). Print it for [9, 1, 5].

intermediate

Lambda triples 8

Make a lambda `triple` that returns x*3, then print triple(8).

intermediate

Function returning a string ([9, 1, 5])

Write shout(s) returning s in uppercase with '!'. Print shout('spark').

intermediate

Recursive factorial of 8

Write a recursive factorial(n) and print factorial(8).

intermediate

Square of 4

Write square(x) returning x*x, then print square(4).

intermediate

Default argument adds 4

Write add(a, b=4) returning a+b. Print add(4).

intermediate

Sum any count of args (4 numbers) ([3, 6, 9, 12])

Write total(*nums) returning their sum. Print total of [3, 6, 9, 12].

intermediate

Return two values (min & max) ([3, 6, 9, 12])

Write minmax(nums) returning (min, max). Print it for [3, 6, 9, 12].

intermediate

Lambda triples 4

Make a lambda `triple` that returns x*3, then print triple(4).

intermediate

Function returning a string ([3, 6, 9, 12])

Write shout(s) returning s in uppercase with '!'. Print shout('cloud').

intermediate

Recursive factorial of 4

Write a recursive factorial(n) and print factorial(4).

intermediate

Square of 10

Write square(x) returning x*x, then print square(10).

intermediate

Default argument adds 2 ([2, 3, 4])

Write add(a, b=2) returning a+b. Print add(10).

intermediate

Sum any count of args (4 numbers) ([2, 3, 4])

Write total(*nums) returning their sum. Print total of [2, 3, 4].

intermediate

Return two values (min & max) ([2, 3, 4])

Write minmax(nums) returning (min, max). Print it for [2, 3, 4].

intermediate

Lambda triples 10

Make a lambda `triple` that returns x*3, then print triple(10).

intermediate

Recursive factorial of 10

Write a recursive factorial(n) and print factorial(10).

intermediate

Default argument adds 6

Write add(a, b=6) returning a+b. Print add(8).

intermediate

Sum any count of args (5 numbers) ([5, 6, 7, 8])

Write total(*nums) returning their sum. Print total of [5, 6, 7, 8].