Exercises
Odd and even
For the numbers from 1 to 20 write which ones are odd and even.
range needs a start and stop.
You can use the module operator (%) to check if a number is even.
Multiplication table
Print the multiplication table for a given number.
Accumulate sums
Sum all numbers from 1 up to a given number.
Sum digits
Write a program that sums all the digits of a number.
You could also create a list with the numbers, and the use sum.
The same result could be calculated without using a for loop using functional programming.
Factorial
Calculate the factorial for a number.
Long and short words
Create a program that given a list of words prints if they are short (less than 4 characters long), not so long (between 4 and 7 characters), and very long (more than 7 characters).
Remember that you can use the len function to get the number of characters of a string and you could create an if, elif, else.
Filter numbers
Create a program that given a list of numbers it filters out the ones bigger than 10.
Remember that you can create an empty list and append items to it one by one.
It could be solved using continue.
Or with the filter function.
Find Beatles
Write a program that outputs which people were a Beatle member and their index in the list.
Remember that you can use enumerate to get the index in any for, and that the in operator allows you to check if an item is in a list.
Remove vowels
Write a program that removes all vowels (aeiou) from a given text.
You can use the in operator to check if a substring is found inside another string.
A possibility is to create a for loop that iterates through every letter in the text and that stores the letter in a list only if it is not a vowel.
The same could be done without a for loop, using the filter function.
Or with a list comprenhension.
Read numbers in CSV
We are reading a comma-separated values (CSV) text file. In each file we have measures and we want to calculate the mean, max, min and sum.
Remember that the str split method can split a text string into a list and the float funtion can transform a text into a number.
Alternativerly you could use the map and sum functions.
Or the mean function and a list comprenhension.
Store multiples
Write a program that stores all the multiples of 7 from 1 to n.
Alternatively, it could be solved using filter and a lambda function.
Or a list comprenhension (this is a very common style that you will find in Python code).
Count words
Create a program that given some text it counts how many times a word appears.
Alternatively you could use the Counter class of the collections module.
Or you could do it with a nested list comprenhension.
Collect quotes
Create a program that collects the quotes from each author in different lists.
Reverse and complement DNA
Write a program capable of creating a reversed and complementary sequence of DNA. To do it you have to go from the end to the start and transform A into T, T into A, C into G and G into C.