Python exercises — basic

Python Exercises — basic

In this page I am writing a list of python exercises.

    Exercise: load all files from a directory — part one

In this exercise we start creating a directory that contain some files. You can use an existing directory, with some existing files, or create a new directory and put some files in it.
Then, create a new python script and:
1) import the library called «glob»;
2) define a variable containing the path to a directory in your computer
(hint: the variable is a string, but is a special type of string, is
a «raw string», so you need to write a «r» just before it);
3) add «\*» at the end of the directory, to mean «all the files contained
in the directory»;
4) use the command glob.iglob() in a for loop, to print all the paths of
all the files contained in the directory;

optional additional part:
import os library, and use os.path.splitext() and os.path.basename()
to list just the names of each file, and not the full path.

    Exercise: load all files from a directory — part two

In this second part we use the python code created in the first part, and modify it.
Also, now we want to use the files in the directory. SO, instead of using just «any file», we want to create some files, with some specific content. Therefore we start creating the files.

1) Create several different text files (around 3 or 4) in a directory (NB: text files are created with text editors. In this case we want to give them the extension «.txt»)

2) In each file write a list of several numerical values (around 10 or 15); write one value per line, going to a new line for each value.

3) Using the part one of this exercise, and the command open(file_path, «r»), for each step of the «for» loop, open the files in the directory, read the content with the command file.read(), close the file, and write all the numerical values in a list, one list for each file.

4) at each step we need to create a different name for the data, and store it in a different place. One solution is to use the command «n, filepath = enumerate()» in the for loop (enumerate the list of paths generated by glob.iglob). Then, we can create a different label at each step of the for loop, using the «format()» command (something similar to «label_{0}».format(n) ).
In this way we can store the data in a dictionary, and use the label as the «key» of the new element of the dictionary.
(note: create an empty dictionary before the «for» loop; then add a new element to the dictionary, at each step, with the syntax dictionary[key] = data_from_the_file )

    Exercise: load data from all the text files contained in a directory and plot the data

Similarly to what done in the previous exercise, let’s use the same idea of a cycle over all the files contained in a directory, and let’s create 2D plots of the data, one plot for each file.
We can use the command for path in glob.iglob(r»C:\path\to\the\directory\*.txt»): to create a cycle over all the text files contained in the directory, and at each step of the cycle we open the file, we read data from it, we create a plot, and then we save it.

Remenber to import matplotlib (use the command import matplotlib.pyplot as plt) at the beginning of the script.

When you read the data, put the data in a variable, and then use that variable for the plt.plot() command.
Depending on how you write the data in the files, you can use the command string.split(«\n») or string.split(«, «) (where «string» is the data you read from the file) depending on how you separate the values in the file, with a newline or a comma and a space. Remember to convert from string to int or to floating point, before using in plt.plot().

We have seen a good way to do that:

list_of_integers = [int(string) for string in list_of_strings]

Also, the command string.strip(«») can remove a character from the end or the beginning of a string. The default is tring.strip() which removes all the white space.

    Exercise: Compare text files with lists of email addresses

Using a text editor, create two text files with email addresses, one address in each line, with no spaces.
— Case 1 — there is a longer file, with around 15/20 addresses, and a shorter file with around 10 addresses, chosen out of the list in the first file. The task is to find the missing addresses in the second file. Create a third file, called «missing addresses» and write the missing addresses in it.
— Case 2 — the two files have lists of similar length, only few addresses are present in both files. The task is to find the addresses present in both files, and write them in a third file.

Hints:
open the first file, load all the content in a string variable, close the file.
open the second file, and do the same.
It is useful to use the commands string.split(). If you don’t remember the use of those commands, search on the book (Lutz) or on the internet, or, do some test in the console (use a string with several words separated by a space).

create a third file (command output_file_id = open(path_to_new_file, «w»)) to write the results

Then, to process the lists, use two nested «for loops», one inside the other, the first going through the first list, the second going through the second list.

at each step of the internal file use «email1 == email2» to compare the emails;

use output_file_id.write(email + «\n») to write the right emails in the output file.

An alternative to the use of two loops is this: use only one loop, on the «longer list», and use the structure «if element in short_list:» (or its opposite, «if element not in short_list:», depending on the case).

    Exercise: find anagrams

An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; e.g., orchestra = carthorse. The new word has to be a word existing in the language (english, in our case).
Given a word, find all the anagrams.

hints:
• use the command input(«please insert a word: «) to insert a word. To avoid problems, use a short word, max 6 letters;
• use the library «itertools», and use the command itertools.permutations() to find all the permutations of the letter in the initial word (permutation = a re-ordering of the same elements);
• use the dictionary at http://www.puzzlers.org/pub/wordlists/unixdict.txt to check if a random combination of letters is an english word or not. To use the dictionary:
— open the link above, select all the text, copy it and paste it in a text file. Save the text file and use it in your python script.
— from python, open the file, read it and put the content into a variable (called e.g. «all_words_string»).
— transform this big string into a list of all the words using words_list = all_words_string.split(«\n»)

— Final test —

1) Use a for loop to print all numbers from 10 to zero, backward.
2) Use the input() command to ask for two numbers and print their sum.
3) Create a list of five words then use a for loop to print all the words, on five different lines.
4) Create a text file, with a text editor, and write 10 words, one word on each line. Save it. With python, open the file, read it, and print all the words, each word on a different line.
5) Create a text file, with a text editor, and write 15 numbers, one number on each line. Save it *in a folder separate from the one with the python script*. With python, open the file and print the sum of all the numbers.
6) Using the file of the previous task, open it in python, and use a for loop to print the numbers, on different lines, but use an «if» statement so to print only the numbers smaller than 10.

plotting

7) using the file of the previous task, open it in python, read the content and create a variable that contains the list of all the numbers (use the command split(«\n») ). Then use matplotlib to create a 2D plot with all the numbers represented as stars, using plt.plot(list, «*») (see figure).
plot
8) create two more text files with numbers, in the same *separate* folder, one number on each line. Create a 2D plot of the values, one for each file (see previous task).
8-bis) if you didn’t use it in the previous task, repeat it, using the «glob» library, and using the command » glob.iglob() » to read all the files contained in the folder. (If you used glob.iglob() in the previous task, go to the next task).

strings

9) Create a variable containing a list, the list has 10 elements, each element is a string of one word. From python create a text file. Then use a for loop and write all the words in the file, one on each different line.
10) With the command «range(0, 20, 2)» generate ten even numbers, and write them in a new text file, one for each line.
11) Use the input() command and ask for three words. Then use the «+» operator to join them, and print the result.