Module 1 · Python crash course · Lesson 4 of 66

Dictionaries, tuples, sets and booleans

Look-ups by key, lists that can't change, and unique values.

In this lesson · about 12 minutes

  • Store and look up values by key
  • Know why tuples are immutable
  • Remove duplicates with a set

A dictionary maps keys to values, inside curly braces: {'key1': 'item1', 'key2': 'item2'}. You look things up by key, not by position. Values can be anything, including lists and other dictionaries.

A tuple is like a list in parentheses, (1, 2, 3), but it is immutable: once made, it can't change. Use tuples for values that belong together and shouldn't be edited, like a point (x, y).

A set holds only unique values: {1, 2, 2, 3} becomes {1, 2, 3}.

Booleans are the two values True and False (capitalized).

Exercise

The tricky one

Grab the word 'hello' from this nested dictionary. It's annoying on purpose: go one step at a time.

Exercise

How many different cities?

Customers came from these cities. Write an expression that gives the number of different cities.

Check your understanding0 of 2 correct

Question 1

1.What is the main difference between a tuple and a list?

Question 2

2.How do you get the value stored under 'price' in a dictionary item?

Quiz score: 0 of 2

Exercises solved: 0 of 2

Tip: answer every quiz question correctly first.

Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.