Python Functions & Lambdas
Practice Exercises — No Solutions Included
Part 1 — Basic Functions
Exercise 1 — Multiply Two Numbers
Write a function called multiply that takes two parameters a and b and returns their product.
■ Call it with: multiply(4, 7) → expected output: 28
Exercise 2 — Greet a User
Write a function called greet that takes a name as a parameter and prints a greeting message.
■ greet("Alice") → expected output: Hello, Alice!
Exercise 3 — Cube of a Number
Write a function called cube that takes a number n and returns n raised to the power of 3.
■ cube(3) → expected output: 27
Part 2 — Default Parameters & Multiple Return Values
Exercise 4 — Temperature Converter
Write a function called convert_temp that takes a temperature value and a unit parameter (default:
'C'). If the unit is 'C', convert to Fahrenheit. If 'F', convert to Celsius. Return the converted value.
■ convert_temp(100) → 212.0
convert_temp(32, 'F') → 0.0
Exercise 5 — String Stats
Write a function called str_stats that takes a string and returns three values: the total number of
characters, the number of vowels, and the number of words.
■ str_stats("hello world") → (11, 3, 2)
Exercise 6 — Absolute Differences
Write a function called abs_diff that takes four numbers and returns the absolute differences
between each consecutive pair: (|a-b|, |b-c|, |c-d|).
■ abs_diff(1, 4, 2, 7) → (3, 2, 5)
Part 3 — Implementing Built-in Logic
Exercise 7 — Custom Power Function
Without using the ** operator or pow(), write a function called my_power that computes base raised
to the exponent using a loop.
■ my_power(2, 8) → 256
Exercise 8 — Custom Sum Function
Write a function called my_sum that takes a list of numbers and returns their total without using
Python's built-in sum().
■ my_sum([10, 20, 30, 40]) → 100
Part 4 — Lambda Functions
Exercise 9 — Lambda: Double a Value
Using a lambda, create a function called double that returns twice the input value.
■ double(15) → 30
Exercise 10 — Lambda: Check Even
Using a lambda, create a function called is_even that returns True if a number is even, and False
otherwise.
■ is_even(4) → True
is_even(7) → False
Exercise 11 — Sort by Last Character
Given the list below, use sort() with a lambda key to sort the strings by their last character
alphabetically.
words = ['python', 'java', 'rust', 'go', 'ruby']
■ Expected: ['java', 'go', 'python', 'ruby', 'rust']
Exercise 12 — Sort Dictionaries by Value
Given the list of dictionaries below, use sorted() with a lambda to sort by 'price' in descending
order.
products = [
{'name': 'Keyboard', 'price': 45},
{'name': 'Monitor', 'price': 300},
{'name': 'Mouse', 'price': 25},
{'name': 'Headset', 'price': 80}
]
Part 5 — map() and filter()
Exercise 13 — Map: String Lengths
Given the list below, use map() to create a new list containing the length of each string.
cities = ['Tunis', 'Casablanca', 'Cairo', 'Algiers']
■ Expected: [5, 10, 5, 7]
Exercise 14 — Map with Lambda: Celsius to Fahrenheit
Given a list of Celsius temperatures, use map() with a lambda to convert each to Fahrenheit.
Formula: F = C × 9/5 + 32
temps_c = [0, 20, 37, 100]
■ Expected: [32.0, 68.0, 98.6, 212.0]
Exercise 15 — Filter: Words Longer Than 4 Letters
Given the list below, use filter() with a lambda to keep only words that have more than 4 characters.
words = ['cat', 'elephant', 'dog', 'tiger', 'ox', 'giraffe']
■ Expected: ['elephant', 'tiger', 'giraffe']
Exercise 16 — Filter: Multiples of 3
Using filter() and a lambda, extract all numbers from the range 1–30 that are divisible by 3.
■ Expected: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
Good luck! Focus on understanding each concept before moving to the next section.