Skip to content
Intermediate Python
  • AI Chat
  • Code
  • Report
  • # Pre-defined lists
    names = ['United States', 'Australia', 'Japan', 'India', 'Russia', 'Morocco', 'Egypt']
    dr =  [True, False, False, False, True, True, True]
    cpc = [809, 731, 588, 18, 200, 70, 45]
    
    # Import pandas as pd
    import pandas as pd
    
    # Create dictionary my_dict with three key:value pairs: my_dict
    my_dict = {"country":names, "drives_right":dr, "cars_per_cap":cpc}
    
    # Build a DataFrame cars from my_dict: cars
    cars = pd.DataFrame (my_dict)
    
    # Print cars
    print (cars)

    Intermediate Python

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

    # Dictionary of dictionaries
    europe = { 'spain': { 'capital':'madrid', 'population':46.77 },
               'france': { 'capital':'paris', 'population':66.03 },
               'germany': { 'capital':'berlin', 'population':80.62 },
               'norway': { 'capital':'oslo', 'population':5.084 } }
    
    
    # Print out the capital of France
    print (europe ["france"] ["capital"])
    
    # Create sub-dictionary data
    data = {"capital":"rome", "population":59.83}
    
    # Add data to europe under key 'italy'
    europe ["italy"] = data
    
    # Print europe
    print (europe)
    # Definition of dictionary
    europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' }
    
    # Print out the keys in europe
    print (europe.keys())
    
    # Print out value that belongs to key 'norway'
    print (europe["norway"])
    
    # Definition of dictionary
    europe = {'spain':'madrid', 'france':'paris', 'germany':'berlin', 'norway':'oslo' }
    
    # Add italy to europe
    europe ["italy"] = "rome"
    
    # Print out italy in europe
    print ("italy" in europe)
    
    # Add poland to europe
    europe ["poland"] = "warsaw"
    
    # Print europe
    print (europe)
    
    # Definition of dictionary
    europe = {'spain':'madrid', 'france':'paris', 'germany':'bonn',
              'norway':'oslo', 'italy':'rome', 'poland':'warsaw',
              'australia':'vienna' }
    
    # Update capital of germany
    europe ["germany"] = "berlin"
    
    # Remove australia
    del (europe ["australia"])
    
    # Print europe
    print (europe)

    Take Notes

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

    # Import numpy as np
    import numpy as np
    
    # Store pop as a numpy array: np_pop
    np_pop = np.array (pop)
    
    # Double np_pop
    np_pop = np_pop *2 
    
    # Update: set s argument to np_pop
    plt.scatter(gdp_cap, life_exp, s = np_pop)
    
    # Previous customizations
    plt.xscale('log') 
    plt.xlabel('GDP per Capita [in USD]')
    plt.ylabel('Life Expectancy [in years]')
    plt.title('World Development in 2007')
    plt.xticks([1000, 10000, 100000],['1k', '10k', '100k'])
    
    # Display the plot
    plt.show()

    Add your notes here

    # Add your code snippets here

    Explore Datasets

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

    • Create a loop that iterates through the brics DataFrame and prints "The population of {country} is {population} million!".
    • Create a histogram of the life expectancies for countries in Africa in the gapminder DataFrame. Make sure your plot has a title, axis labels, and has an appropriate number of bins.
    • Simulate 10 rolls of two six-sided dice. If the two dice add up to 7 or 11, print "A win!". If the two dice add up to 2, 3, or 12, print "A loss!". If the two dice add up to any other number, print "Roll again!".