Exercises

Odd and even

For the numbers from 1 to 20 write which ones are odd and even.

Tip

range needs a start and stop.

You can use the module operator (%) to check if a number is even.

Tip

Multiplication table

Print the multiplication table for a given number.

Tip

Accumulate sums

Sum all numbers from 1 up to a given number.

Tip

Using sum and range with no for loop.

Sum digits

Write a program that sums all the digits of a number.

Tip

You can transform, type cast, strings into numbers and number into strings using int and str.

Remember also that strings are iterable, so if you give a string to a for it will iterate through every character.

Tip

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.

Tip

Using a functional approach with reduce.

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).

Tip

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.

Tip

Filter numbers

Create a program that given a list of numbers it filters out the ones bigger than 10.

Tip

Remember that you can create an empty list and append items to it one by one.

Tip

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.

Tip

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.

Tip

If the list were large it would be much efficient to use a dictionary or a set than a list. The solution using a set would be:

Using filter and a lambda we could even skip many iterations of the for.

Remove vowels

Write a program that removes all vowels (aeiou) from a given text.

Note

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.

Note

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.

Tip

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.

Tip

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.

Tip

Remember that you can use the range function to get the numbers and the modulo operator to get the remainder of an integer division.

Tip

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.

Tip

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.

Tip

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.

Tip