Solved Python exercises for beginners (FP1)
Every solved Python exercise on SergioLearns in one place, by topic and from easy to hard: first the FP1 topics, to start from scratch, and below them the object-oriented programming topics from FP2. Each topic comes with challenges and commented solutions, an exam-style quiz and its cheat sheet.
sergio@learns:~$ ls -l ~/exercises/python --sort=level
total 20 topics · 66 exercises · 263 quiz questions
[ OK ] every solution is commented and was run before publishing
Exercises by topic
From scratchFP113 topics
- variables.pyVariablestheoryguided practicequizcheat sheet
- operators.pyOperatorstheoryguided practicequizcheat sheet
- conditionals.pyConditionalstheoryguided practicequizcheat sheet
- loops.pyLoopstheoryguided practicequizcheat sheet
- functions.pyFunctionstheoryguided practicequizcheat sheet
- modules.pyModulestheoryguided practicequizcheat sheet
- lists-and-tuples.pyLists and tuplestheoryguided practicequizcheat sheet
- mutability-and-copies.pyMutability and copiestheoryguided practicequizcheat sheet
- recursion.pyRecursiontheoryguided practicequizcheat sheet
- associative-containers.pyAssociative containerstheoryguided practicequizcheat sheet
- strings.pyStringstheoryguided practicequizcheat sheet
- regex.pyRegextheoryguided practicequizcheat sheet
- text-files.pyText filestheoryguided practicequizcheat sheet
IntermediateFP27 topics
- exceptions.pyExceptionstheoryguided practicequizcheat sheet
- classes.pyClassestheoryguided practicequizcheat sheet
- inheritance.pyInheritancetheoryguided practicequizcheat sheet
- operator-overloading.pyOperator overloadingtheoryguided practicequizcheat sheet
- iterators-and-generators.pyIterators and generatorstheoryguided practicequizcheat sheet
- testing.pyTestingtheoryguided practicequizcheat sheet
- linkedlist.pyLinkedListtheoryguided practicequizcheat sheet
How each exercises article works
They all follow the same pattern, so once you get used to one you can find your way around the rest. They open with “How to use this article”, then come the challenges on three levels (basic, intermediate and a final challenge that brings the whole topic together), followed by three “What’s wrong here?” snippets: code that looks right but hides a mistake people really make in class. The solutions are all together at the end, commented line by line and with the real output Python gives when you run them, so you don’t see them by accident while you think.
In the list above each line is a topic: how many exercises it has, how many questions its quiz has, and its name, which takes you straight to the article. Next to it you have the guided practice for the same topic, the quiz and the cheat sheet. The list updates itself every time I publish a new topic.
Which order to do them in
If you are starting from scratch, follow the order of the list, which is the order of FP1: variables and data types, operators and conditionals; then loops (for and while) and functions; after that modules, lists and tuples, mutability and matrices, recursion, dictionaries and sets, strings, regular expressions and text files. Each topic assumes the one before, and skipping one usually shows two topics later.
The FP2 block is the next step up: exceptions, classes, inheritance, operator overloading, iterators and generators, testing and a linked list built by hand. There it is no longer enough for the program to work: how it is organised counts too.
If you are short of time before an exam, you don’t need to do them all. Do the final challenge of each topic: if you can solve it without looking, you have that topic; if not, go back to the basic ones.
How to practice so it sticks
What has worked best for me at GCID is this: I read the statement, cover the solution and give myself about twenty minutes to try it, even if it comes out ugly. I write it and run it, never just in my head; if you don’t have Python yet, the site’s Python playground runs it in your browser with nothing to install. When it works, or when I give up, I compare it with the commented solution and look at what I did differently, not only at whether it prints the same thing.
Don’t skip the “What’s wrong here?” snippets. In the exam you are asked to read code as much as to write it, and finding the mistake in someone else’s code trains exactly that. Try to say what each snippet prints before you open the explanation.
The exam-style quiz
Almost every topic ends with an interactive quiz with questions like the ones in the multiple-choice exam: single answer, true or false, fill in the gaps, put the lines in order… It marks you straight away and explains every answer. Do it at the end, once you have solved the exercises, and write down the questions you get wrong: those are the ones to revise. If you only have two minutes, at the top you have the question of the day, taken each day from a different Python quiz.
Once you have done a few topics, try the mock exams: they are timed and marked, like the real exam.
The cheat sheet and Python Tutor
At the end of each article there is the topic’s cheat sheet: the syntax and the typical mistakes on a single page, to revise the night before without reading all the theory again. All the cheat sheets are together in Resources.
In “See it step by step in Python Tutor” you have the code of one exercise ready to open in Python Tutor, a website that runs the program line by line and draws the variables and lists in memory. It is the quickest way to understand why copying a list changes the original, or what a recursive function really does.
Before you start and after
If you find it hard to picture what a file, a folder or a byte is, read Basic computing before programming first; and if you want Python on your own computer, here is how to install Python on Windows. When you finish FP1, the next step at GCID is the solved Java exercises of FP2, and in IC2 come the solved C exercises. Everything else for practising is in Practice.

IC2 theory mock exam — first midterm, second midterm or the full exam, with your real mark and a review of every mistake (as if you were in the exam room)