Python Tutorial
Learn Python for business analysis using real-world data. No coding experience necessary.
Start Now
Mode Studio
The Collaborative Data Science Platform
Python Methods, Functions, & Libraries
Starting here? This lesson is part of a full-length tutorial in using Python for Data Analysis. Check out the beginning.
Goals of this lesson
In this lesson, you’ll learn about:
- Methods of dictionaries, specifically
.keys()and.values() - Functions, specifically print, type(), and len()
- Importing libraries with
import
Using Mode Python notebooks
Mode is an analytics platform that brings together a SQL editor, Python notebook, and data visualization builder. Throughout this tutorial, you can use Mode for free to practice writing and running Python code.
- Log into Mode or create an account.
- Navigate to this report and click 'Duplicate'. This will take you to the SQL Query Editor.
- Click 'Python Notebook' under 'Notebook' in the left navigation panel. This will open a new notebook.
Now you’re all ready to go.
Methods
Let's use a dictionary you created in the previous lesson:
city_population = {
'Tokyo': 13350000, # a key-value pair
'Los Angeles': 18550000,
'New York City': 8400000,
'San Francisco': 1837442,
}
For a quick refresher of dictionaries, click here.
Say you want to see all the keys in the dictionary (in this case, cities). You can use a method called .keys() to get a list of the dictionary's keys. A method is an action that you can take on an object. If a real-world window was an object, its methods might be .open() or .close().
In this case, the object is the dictionary, and the action is to return the .keys():
city_population.keys()
['New York City', 'San Francisco', 'Los Angeles', 'Tokyo']
Remember how everything in Python is an object? That output is a list object! You can double-check this using type() as in the previous lesson.
type(city_population.keys())
list
To further illustrate this, you can get the third item in the list of keys by referencing its index:
city_population.keys()[2]
'Los Angeles'
You can also get the values of the dictionary using the values() method:
city_population.values()
[8400000, 1837442, 18550000, 13350000]
Practice Problem
To reiterate what you learned last lesson, get the population for Los Angeles.
View SolutionUsing methods on combined objects
You can use methods on combined objects as well.
In the last lesson, you created a dictionary of lists by nesting individual lists of cities and their municipalities in a dictionary object. Here it is again:
municipalities = {
'New York City': [
'Manhattan',
'The Bronx',
'Brooklyn',
'Queens',
'Staten Island'
],
'Tokyo': [
'Akihabara',
'Harajuku',
'Shimokitazawa',
'Nakameguro',
'Shibuya',
'Ebisu/Daikanyama',
'Shibuya District',
'Aoyama',
'Asakusa/Ueno',
'Bunkyo District',
'Ginza',
'Ikebukuro',
'Koto District',
'Meguro District',
'Minato District',
'Roppongi',
'Shinagawa District',
'Shinjuku',
'Shinjuku District',
'Sumida District',
'Tsukiji',
'Tsukishima']
}
As a reminder, to get a single municipality, you would enter the dictionary’s variable name—municipalities—and the list index—['Tokyo'][3].
municipalities['Tokyo'][3]
'Nakameguro'
The keys of the dictionary are: