Exercise: intermediate Python

You will exercise the following concepts

  • sequences (lists)
  • loops
  • functions
  • exception handling

Problem: You are presented with aggregated census data containing the male and female population across different states. You need to calculate the sex ratio and print that out in the format:

Male:female ratio in AL is 1.22:0.82
Male:female ratio in AZ is 1.04:0.96

Complete the following code

In [1]:
states_list = ['AL', 'AZ', 'AK', 'AR', 'CA', 'CO', 'CT']
males = [55, 51, 52.9, 'tuesday', [53], 49, 50]
females = [45, 49, 48.1, 48.5, 47, (51), 50]
In [2]:
def sex_ratio_by_states(states_list, males, females):
    for i in range(len(states_list)):
        s = states_list[i]
        m = males[i]
        f = females[i]
            male_ratio = round(m/f, 2)
            female_ratio = round(f/m, 2)

            print("Male:female ratio in {} is {}:{}".format(s, str(male_ratio),
            print("Error processing state: " + s)
In [3]:
sex_ratio_by_states(states_list, males, females)
Male:female ratio in AL is 1.22:0.82
Male:female ratio in AZ is 1.04:0.96
Male:female ratio in AK is 1.1:0.91
Error processing state: AR
Error processing state: CA
Male:female ratio in CO is 0.96:1.04
Male:female ratio in CT is 1.0:1.0

Install arcgis package in a new env using conda

  1. Create a new environment called arcgis_handson with Python 3.6
  2. activate that environment
  3. Install arcgis package from esri channel
  4. Install pandas package from default channel
  5. Install matplotlib package
  6. Install numpy package, was it already installed?
conda create --name arcgis_handson python=3.6
activate arcgis_handson

if running on mac or linux then: source activate arcgis_handson

conda install -c esri arcgis
conda install pandas
conda install matplotlib
conda install numpy