Python Function Exercises With Six Beginner Problems
You have read what a function is, you have watched someone type def on a screen, and yet when you open a blank file you have no idea what to write. That gap is normal.

You have read what a function is, you have watched someone type def on a screen, and yet when you open a blank file you have no idea what to write. That gap is normal. Reading about functions and writing them are different skills, and the only way across is a set of small Python function exercises that you finish yourself.
Below are six problems I use with beginners in our club sessions. Each one adds one new idea, so do them in order. Give yourself 10 to 20 minutes per problem, and do not move on until your version runs and gives the expected output.
How to work through these Python function exercises
Every problem has the same structure: a task, an example of what the function should return, a hint, and a short walkthrough of one possible answer. Cover the walkthrough with your hand or scroll past it until you have tried. If you get stuck for more than five minutes, read the hint only, then try again.
Two rules make the practice stick. First, always test with at least three inputs, including a strange one like zero, an empty string or a negative number. Second, keep each function short. If it grows past 10 lines, it probably does two jobs, which is the point we make in why your first functions should do only one thing.
Warm up problems with one input
Problem 1: double it
Write a function called double that takes a number and returns it multiplied by two. Calling double(7) should give 14, and double(-3) should give -6.
Hint: the body is a single return line.
Walkthrough: start with def double(n): then on the next line, indented four spaces, write return n * 2. The most common mistake here is writing print instead of return. Print shows the value on screen, but the caller gets nothing back. Test it by writing result = double(7) and then print(result).
Problem 2: is it even
Write is_even(n) that returns True if a whole number is even and False if it is odd. is_even(10) gives True, is_even(7) gives False, and is_even(0) gives True.
Hint: the remainder operator % tells you what is left after dividing.
Walkthrough: the whole body can be return n % 2 == 0. Many beginners write an if statement that returns True in one branch and False in the other. That works, but notice that the comparison already produces True or False, so you can return it directly. If if and else still feel shaky, the Python if else exercises are a good warm up before this set.
Problems with more than one parameter
Problem 3: rectangle area
Write area(width, height) that returns the area of a rectangle. area(4, 5) gives 20. Then add a second function, perimeter(width, height), that returns 2 times the sum of the sides, so perimeter(4, 5) gives 18.
Hint: parameters are listed inside the brackets, separated by a comma.
Walkthrough: area is return width * height. Perimeter is return 2 * (width + height). The brackets matter here: without them Python multiplies first and gives 13 instead of 18. This is a good moment to test with the order swapped, area(5, 4), and confirm you still get 20.
Problem 4: greeting with a default
Write greet(name, greeting="Hello") that returns a sentence. greet("Sam") returns "Hello, Sam!" and greet("Sam", "Welcome back") returns "Welcome back, Sam!".
Hint: a default value is set with an equals sign in the parameter list.
Walkthrough: inside the function, build the string with an f-string: return f"{greeting}, {name}!". Default values let one function cover the common case without forcing every caller to type the same word. Keep defaults to simple values like strings and numbers for now. Using a list as a default has a famous trap that is worth learning later, not on day one.
Problems that work with lists
Problem 5: total of a shopping list
Write total(prices) that takes a list of prices and returns their sum. total([2.5, 4, 3.5]) gives 10.0, and total([]) gives 0.
Hint: make a variable that starts at zero and add to it inside a for loop.
Walkthrough: set running = 0, loop with for p in prices:, add with running = running + p, and after the loop return running. Watch the indentation of the return line. If you indent it inside the loop, the function returns after the first item and total([2.5, 4, 3.5]) gives 2.5. That single misplaced indent is the bug I see most in club sessions. Yes, Python has a built in sum, but writing it once yourself teaches you how loops and return work together.
Problem 6: longest word
Write longest(words) that returns the longest string in a list. longest(["cat", "giraffe", "dog"]) gives "giraffe". If two words tie, return the first one. If the list is empty, return an empty string.
Hint: track the best word so far, and compare lengths with len.
Walkthrough: start with best = "". Loop through the words, and whenever len(word) is bigger than len(best) (written with the greater than sign), set best = word. Return best after the loop. Because you use a strict greater than, a tie keeps the earlier word, which matches the rule. The empty list case works automatically, since the loop never runs and best stays empty. Test with ["aa", "bb"] to prove the tie rule.
Why you should not skip straight to built in functions
A lot of online advice says beginners should learn Python's built ins early: use sum, use max with a key, use list comprehensions, and stop writing loops by hand. For working programmers that is good advice. For your first two weeks, I disagree.
When you call max(words, key=len), you get the right answer without understanding what happened. The first time you need something slightly different, such as the longest word that starts with a vowel, you have nothing to build on. Writing the loop yourself in Problem 6 gives you a pattern you can bend. After you can write it by hand in under three minutes, switch to the built in and enjoy the shortcut. You will also read other people's code better, because you know what the shortcut hides.
What to do after the six problems
Once all six run, go back and change each one. Make double take a second parameter called times. Make is_even work for a whole list and return how many are even. Make longest return the shortest word instead. Each change takes five minutes and forces you to reread your own code, which is half of real programming.
After that, try combining functions: write a function that uses total and then applies a 10 percent discount if the total is over 50. When you are ready for more, our guide on practising Python with small daily exercises lays out a two week plan.
Frequently asked questions
What is the difference between print and return in a Python function?
Print shows a value on the screen but gives nothing back to the code that called the function. Return hands the value back so you can store it, test it or pass it to another function. In exercises, you almost always want return.
How many function exercises should a beginner do?
Aim for 15 to 20 small functions spread over a week or two, rather than 50 in one weekend. The goal is to write a working function from a blank file without looking anything up. Once that feels easy, move on to small projects.
Why does my Python function return None?
A function returns None when it reaches the end without a return statement. This usually means you used print instead of return, or the return line is inside an if branch that never ran. Add a return at the end and check your indentation.
More in Python and Practice
Python and Practice
Python Enumerate and Zip Practice With Five Exercises
A student at our Tuesday club spent eleven minutes last month hunting a bug in four lines of code.
Python and Practice
Python Set Practice With Five Beginner Exercises
A learner in my Thursday club brought me 34 lines of code that found the duplicate names in a sign-in sheet.
Python and Practice
Python List Comprehension Practice With Five Exercises
A student in my Tuesday group handed me a line of code last spring that was 118 characters long, had two for clauses and an if...