ThoughtSpot acquires Mode to define the next generation of collaborative BI >>Learn More

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:

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.

  1. Log into Mode or create an account.
  2. Navigate to this report and click 'Duplicate'. This will take you to the SQL Query Editor.
  3. 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:

Input

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():

Input

city_population.keys()
    
Output

['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.

Input

type(city_population.keys())
    
Output

list
    

To further illustrate this, you can get the third item in the list of keys by referencing its index:

Input

city_population.keys()[2]
    
Output

'Los Angeles'
    

You can also get the values of the dictionary using the values() method:

Input

city_population.values()
    
Output

[8400000, 1837442, 18550000, 13350000]
    

Practice Problem

To reiterate what you learned last lesson, get the population for Los Angeles.

View Solution

Using 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:

Input

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

Input

municipalities['Tokyo'][3]
    
Output

'Nakameguro'
    

The keys of the dictionary are:

Input