32 Free Python Coding Challenges, from Very Easy to Hard
Practise Python with 32 free coding challenges in your browser: four levels, hidden tests, hints and explained solutions. Plus a worked Two Sum example.
On this page
Reading about Python only gets you so far. The way it really sticks is by solving problems: staring at a blank function, trying something, watching it fail, and fixing it. That is why PythonExecutor now has 32 free Python challenges you can solve right in your browser, with no sign-up and nothing to install.
This post explains how the challenges work, what is in each of the four levels, and how to get the most out of them, using one classic problem as a worked example.
How a challenge works
Every challenge asks you to write one function. The page shows the problem on the left and a code editor on the right, with two buttons underneath:
- Run examples checks your function against the worked examples shown in the problem. It is quick feedback while you are still writing.
- Submit runs every test, including hidden edge cases such as empty lists, negative numbers and very large inputs. Pass them all and the challenge is marked as solved.
For each test you see the call, the expected answer, what your function actually returned, any error, and anything your code printed. Hidden tests stay hidden while they pass and show their details as soon as one fails, so you always know what to fix.
If you get stuck, the Hints tab opens one hint at a time, each giving away a little more. The Solution tab unlocks once you have solved the challenge, with the code and a short explanation of why it works. Your code and your progress are saved in your browser, so you can close the tab and carry on later.
The four levels
Very easy
One idea and a few lines each. These are perfect for your first week of Python: they practise writing a function, using return, and simple conditions and loops.
Add Two Numbers, Even or Odd, Celsius to Fahrenheit, Say Hello, Last Item, Biggest of Three, Reverse a String and Sum a List.
Easy
Loops, strings and lists working together, including a few problems every programmer meets sooner or later.
Count the Vowels, FizzBuzz, Factorial, Palindrome Check, Remove Duplicates, Sum of Digits, Capitalize Every Word and Fibonacci Number.
Medium
Dictionaries, sets, stacks and recursion, plus some classic interview questions.
Word Frequency, Anagram Check, Two Sum, Flatten a Nested List, Run-Length Encoding, Second Largest, Balanced Brackets and Roman Numerals to Integer.
Hard
Algorithms that need a plan before you start typing: sliding windows, dynamic programming, flood fill and a small parser.
Longest Substring Without Repeats, Merge Intervals, Spiral Matrix, Edit Distance, All Permutations, Count the Islands, Evaluate an Expression and Coin Change.
The mistake almost everyone makes first: print instead of return
Here is a very common first attempt at the very first challenge:
def add(a, b):
print(a + b)It looks right, and it even shows 5 when you call add(2, 3). But the test fails, saying your function returned None. Printing shows a value on the screen; returning hands it back to the code that called the function, and that is what the tests check. A function without a return gives back None.
def add(a, b):
return a + bYou are still free to use print() while you work. Whatever you print appears next to each test result, which makes it a handy way to look inside your function.
Worked example: Two Sum
Two Sum is a famous interview question. Given a list of numbers and a target, return the positions of the two numbers that add up to the target. For [2, 7, 11, 15] and a target of 9, the answer is [0, 1], because 2 + 7 = 9.
The first idea most people have is to try every pair:
def two_sum(nums, target):
for i in range(len(nums)):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]This passes the examples, and it is a perfectly good place to start. But one hidden test uses a list of 10,000 numbers, where checking every pair means about 50 million comparisons. That takes a few seconds even on a fast computer, and in the browser it can run into the 10-second limit. When that happens, the page tells you the code ran too long.
The better idea is to remember what you have already seen. For each number x, the partner you need is target - x. Keep a dictionary from each number to its position, and check it before adding the current number:
def two_sum(nums, target):
seen = {}
for j, x in enumerate(nums):
if target - x in seen:
return [seen[target - x], j]
seen[x] = jNow the list is read only once and each check is a single dictionary lookup. On the same 10,000-number list it finishes in about two thousandths of a second.
That jump, from "it works" to "it works fast", is exactly what the medium and hard challenges are designed to teach.
Tips for practising well
- Start at your level, not at the top. If very easy feels easy, great: it will only take a few minutes. Confidence and fluency matter as much as cleverness.
- Read the examples before you code. They often answer questions the description leaves open, such as what to return for an empty list.
- Run the examples first, then Submit. Small steps make mistakes easier to find.
- Read the failing test carefully. Compare the expected answer with what you returned. Is it the wrong type, the wrong order, or an edge case you didn't think about?
- Use hints one at a time. Each one is a nudge, not the answer. Try again after every hint.
- Rewrite the solution from memory. After reading a solution, close it and write it yourself a few minutes later. That is how a technique becomes yours.
Stuck on why your code behaves the way it does?
Paste it into the Python visualizer and step through it one line at a time. You will see every variable and list change as it happens, which makes bugs like a loop that stops too early or a list shared between two names easy to spot. And if you are brand new to Python, the free Python course covers everything the very easy and easy challenges need.
Ready? Open the Python challenges and start with the first one. It takes about a minute.
By PythonExecutor Team