Here you have a collection of guided exercises for the first class on Python.
The exercises are divided by topic, following the topics reviewed during the theory session, and for each topic you have some mandatory exercises, and other optional exercises, which you are invited to do if you still have time after the mandatory exercises.
Remember that you have 5 hours to solve these exercises, after which we will review the most interesting exercises together. If you don't finish all the exercises, you can work on them tonightor tomorrow.
At the end of the class, we will upload the code with the solutions of the exercises so that you can review them again if needed. If you still have not finished some exercises, try to do them first by yourself, before taking a look at the solutions: you are doing these exercises for yourself, so it is always the best to do them your way first, as it is the fastest way to learn!
From Part 1
Exercise 1.1: The cover price of a book is 24.95 EUR, but bookstores get a 40 percent discount. Shipping costs 3 EUR for the first copy and 75 cents for each additional copy. Calculate the total wholesale costs for 60 copies.
Exercise 1.2: When something is wrong with your code, Python will raise errors. Often these will be "syntax errors" that signal that something is wrong with the form of your code (e.g., the code in the previous exercise raised a
SyntaxError). There are also "runtime errors", which signal that your code was in itself formally correct, but that something went wrong during the code's execution. A good example is the
ZeroDivisionError, which indicates that you tried to divide a number by zero (which, as you may know, is not allowed). Try to make Python raise such a
Exercise 1.3 (🌶️): You look at the clock and see that it is currently 14.00h. You set an alarm to go off 535 hours later. At what time will the alarm go off? Write a program that prints the answer. Hint: for the best solution, you will need the modulo operator. Second hint: The answer is 21.00h, but of course, this exercise is not about the answer, but about how you get it.
From Part 4
Exercise 4.1 (🌶️🌶️): Define a function that receives a string parameter, and returns an integer indicating the count for all the letters of the alphabet that there are in the string. The expected output format is in a dictionary with the relevant keys and values. The capital version of a lower case letter is considered to be the same letter.
This should sound quite familiar, try to do it without looking at your old code ;)
What is the challenge?: Don't code 27 times the same line of code, learn how to do things that scale well.
From Part 5
Exercise 5.1: Create a countdown function that starts at a certain count, and counts down to zero. Instead of zero, print "Blast off!". Use a
Exercise 5.2: Write and test three functions that return the largest, the smallest, and the number of dividables by 3 in a given collection of numbers. Use the algorithm described earlier in the Part 5 lecture :)
Exercise 5.4 (🌶️): "99 bottles of beer" is a traditional song in the United States and Canada. It is popular to sing on long trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. The song's simple lyrics are as follows: "99 bottles of beer on the wall, 99 bottles of beer. Take one down, pass it around, 98 bottles of beer on the wall." The same verse is repeated, each time with one fewer bottle. The song is completed when the singer or singers reach zero. Write a function that generates and prints all the verses of the song (though you might start a bit lower, for instance with 10 bottles). Make sure that your loop is not endless, and that you use the proper inflection for the word "bottle".
Exercise 5.5 (🌶️): The Fibonacci sequence is a sequence of numbers that starts with 1, followed by 1 again. Every next number is the sum of the two previous numbers. I.e., the sequence starts with 1, 1, 2, 3, 5, 8, 13, 21,... Write a function that calculates and prints the Fibonacci sequence until the numbers get higher than a
Exercise 5.6 (🌶️🌶️): A prime number is a positive integer that is dividable by exactly two different numbers, namely 1 and itself. The lowest (and only even) prime number is 2. The first 10 prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29. Write a function that returns a list off all prime numbers below a given number.
Hint: In a loop where you test the possible dividers of the number, you can conclude that the number is not prime as soon as you encounter a number other than 1 or the number itself that divides it. However, you can only conclude that it actually is prime after you have tested all possible dividers.
What is the challenge here? You have to try to optimize your code and try to make it work for the highest prime number you can encounter before you run out of memory. For low numbers you should know how to do it already
Exercise 5.7 (🌶️🌶️): Write a function that prints all integers between the parameters
b that can be written as the sum of two squares. Produce output in the form of
z = x**2 + y**2, e.g.,
58 = 3**2 + 7**2. If a number occurs on the list with multiple different ways of writing it as the sum of two squares, that is acceptable.
Exercise 5.8 (🌶️): A, B, C, and D are all different digits. The number DCBA is equal to 4 times the number ABCD. What are the digits? Note: to make ABCD and DCBA conventional numbers, neither A nor D can be zero. Use a quadruple-nested loop.