Skip to content

Python Data Science Toolbox (Part 1)

Run the hidden code cell below to import the data used in this course.


1 hidden cell

Take Notes

Add notes about the concepts you've learned and code cells with code you want to keep.

Nested functions

Define echo_shout()

def echo_shout(word): """Change the value of a nonlocal variable"""

# Concatenate word with itself: echo_word echo_word = word * 2 # Print echo_word print(echo_word) (This is the first run on the message) # Define inner function shout() def shout(): """Alter a variable in the enclosing scope""" # Use echo_word in nonlocal scope nonlocal echo_word (reverts the code back to the original) # Change echo_word to echo_word concatenated with '!!!' echo_word = echo_word + '!!!' # Call function shout() shout() # Print echo_word print(echo_word) (calls with updates)

Call function echo_shout() with argument 'hello'

echo_shout('hello')

# Add your code snippets heredef find_all_odds(lst):
    # Write your code here.
    
   def odds_list(lst): 
    odds_list = []

    for element in lst:
        if element % 2 == 1:
            odds_list.append(element)
    
    return odds_list


def find_all_odds(lst):
    # Write your code here.
    odds_list = []

    for element in lst:
        if element % 2 == 1:
            odds_list.append(element)
    
    return odds_list

# Define echo_shout()
def echo_shout(word):
    """Change the value of a nonlocal variable"""
    
    # Concatenate word with itself: echo_word
    echo_word = word * 2
    
    # Print echo_word
    print(echo_word)
    
    # Define inner function shout()
    def shout():
        """Alter a variable in the enclosing scope"""    
        # Use echo_word in nonlocal scope
        nonlocal echo_word
        
        # Change echo_word to echo_word concatenated with '!!!'
        echo_word = echo_word + '!!!'
    
    # Call function shout()
    shout()
    
    # Print echo_word
    print(echo_word)

# Call function echo_shout() with argument 'hello'
echo_shout('hello ')

Explore Datasets

Use the DataFrame imported in the first cell to explore the data and practice your skills!

  • Write a function that takes a timestamp (see column timestamp_ms) and returns the text of any tweet published at that timestamp. Additionally, make it so that users can pass column names as flexible arguments (*args) so that the function can print out any other columns users want to see.
  • In a filter() call, write a lambda function to return tweets created on a Tuesday. Tip: look at the first three characters of the created_at column.
  • Make sure to add error handling on the functions you've created!