Python: the basics
First steps with data: filtering, aggregating, debugging
In this chapter, you will learn to
- Use lists and dictionaries to filter, sort and count
- Break a problem into small functions
- Read an error message and fix code that doesn't work
Tools: Python
9 exercises
Each exercise is graded automatically, in your browser. The full statements are available once you have an account.
What this is for in real life
Before doing AI, you need to make a list of data talk, and Python is the common language of almost every AI tool. - Newsrooms. An online newspaper logs every article read. Finding out which sections really hold…
Monday's chart
You have just joined the newsroom of an online newspaper. On Monday morning, the editorial meeting always starts with the same question: how did last week go? The editor-in-chief would like to see it at a glance…
The checkerboard
The newspaper's games page publishes a draughts puzzle every week, and it needs a blank board of any size. Write
draw_board(n), which prints a grid ofnrows andncolumns. Black squares are#, white squares are…Lesson: types in Python
Why care. In data analysis, the same piece of information sometimes arrives as a number, sometimes as text. Mixing them up produces silent bugs:
"12" + "3"equals"123", not15. The four ubiquitous types -…Lesson: sorting in Python
Sorting means ordering a list according to a criterion (a ranking, a top-N). Python offers two tools that must not be confused: -
sorted(liste)returns a new sorted list, without touching the original. -…Front page
You have just joined the newsroom of an online newspaper. Its homepage only has room for a handful of articles, and you are the one choosing them: you start with those readers actually opened. Your task. Write…
Monthly review
The month is drawing to a close, and the newsroom you work for is already preparing the next one. Which sections should it focus its efforts on: investigations? sports? culture? The editor-in-chief asks for your…
The intern's code
An intern at the newsroom wrote two functions to track readership: for each section, we want its average number of views over the past weeks. His code doesn't work. -
make_grid(n_topics, n_weeks)must return a…The intern returns
The intern is back, this time with 4 functions for going through the connection logs of the newspaper's website: -
count_logins(logs): counts the number of appearances of each user. Returns adictuser → count.…