In [1]:
animals = ['lion', 'tiger', 'crocodile', 'vulture', 'hippo']
print(animals)
for creature in animals:
    print(creature)


['lion', 'tiger', 'crocodile', 'vulture', 'hippo']
lion
tiger
crocodile
vulture
hippo

In [2]:
for creature in animals:
    pass
print('The loop variables is now: ' + creature)


The loop variables is now: hippo

In [3]:
import os
#os.mkdir('data/yearly_files')

In [4]:
os.listdir('data')


Out[4]:
['data_1024.csv',
 'out.csv',
 'species.csv',
 'speciesSubset.csv',
 'surveys.csv',
 'yearly_files']

In [5]:
import pandas as pd
surveys_df = pd.read_csv('data/surveys.csv')
surveys2002 = surveys_df[surveys_df.year == 2002]
surveys2002.to_csv('data/yearly_files/surveys2002.csv')

In [6]:
surveys_df['year'].unique()


Out[6]:
array([1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987,
       1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
       1999, 2000, 2001, 2002])

In [7]:
for year in surveys_df['year'].unique():
    surveys_year = surveys_df[surveys_df.year == year]
    filename = 'data/yearly_files/surveys' + str(year) + '.csv'
    surveys_year.to_csv(filename)

In [8]:
def this_is_the_function_name(input_argument1, input_argument2):
    print('The function arguments are:', input_argument1, input_argument2, '(this is done inside the function!)')
    return input_argument1*input_argument2

In [9]:
product_of_inputs = this_is_the_function_name(2, 5)
print('Their product is:', product_of_inputs, '(this is done outside the function!)')


The function arguments are: 2 5 (this is done inside the function!)
Their product is: 10 (this is done outside the function!)

In [10]:
def one_year_csv_writer(this_year, all_data):
    """
    Writes a csv file for data from a given year.

    this_year --- year for which data is extracted
    all_data --- DataFrame with multi-year data
    """
    surveys_year = all_data[all_data.year == this_year]
    filename = 'data/yearly_files/function_surveys' + str(this_year) + '.csv'
    surveys_year.to_csv(filename)

In [11]:
one_year_csv_writer?

In [12]:
one_year_csv_writer(2002, surveys_df)

In [13]:
def yearly_data_csv_writer(start_year, end_year, all_data):
    """
    Writes separate csv files for each year of data.

    start_year --- the first year of data we want
    end_year --- the last year of data we want
    all_data --- DataFrame with multi-year data
    """
    for year in range(start_year, end_year+1):
        one_year_csv_writer(year, all_data)

In [14]:
surveys_df = pd.read_csv('data/surveys.csv')
yearly_data_csv_writer(1977, 2002, surveys_df)

In [15]:
def yearly_data_arg_test(all_data, start_year = 1977, end_year = 2002):
    """
    Modified from yearly_data_csv_writer to test default argument values!

    start_year --- the first year of data we want --- default: 1977
    end_year --- the last year of data we want --- default: 2002
    all_data --- DataFrame with multi-year data
    """
    return start_year, end_year

start,end = yearly_data_arg_test (surveys_df, 1988, 1993)
print('Both optional arguments:\t', start, end)

start,end = yearly_data_arg_test (surveys_df)
print('Default values:\t\t\t', start, end)


Both optional arguments:	 1988 1993
Default values:			 1977 2002

In [16]:
def yearly_data_arg_test(all_data, start_year = None, end_year = None):
    """
    Modified from yearly_data_csv_writer to test default argument values!

    start_year --- the first year of data we want --- default: None - check all_data
    end_year --- the last year of data we want --- default: None - check all_data
    all_data --- DataFrame with multi-year data
    """
    if not start_year:
        start_year = min(all_data.year)
    if not end_year:
        end_year = max(all_data.year)

    return start_year, end_year

start,end = yearly_data_arg_test (surveys_df, 1988, 1993)
print('Both optional arguments:\t', start, end)

start,end = yearly_data_arg_test (surveys_df)
print('Default values:\t\t\t', start, end)


Both optional arguments:	 1988 1993
Default values:			 1977 2002

In [17]:
a = 5
if a < 0:
    print('a is a negative number')
elif a > 0:
    print('a is a positive number')
else:
    print('a must be zero!')


a is a positive number

In [18]:
def yearly_data_arg_test(all_data, start_year = None, end_year = None):
    """
    Modified from yearly_data_csv_writer to test default argument values!

    start_year --- the first year of data we want --- default: None - check all_data
    end_year --- the last year of data we want --- default: None - check all_data
    all_data --- DataFrame with multi-year data
    """
    if not start_year:
        start_year = min(all_data.year)
    if not end_year:
        end_year = max(all_data.year)

    return start_year, end_year

start,end = yearly_data_arg_test (surveys_df)
print('Default values:\t\t\t', start, end)

start,end = yearly_data_arg_test (surveys_df, 1988, 1993)
print('No keywords:\t\t\t', start, end)

start,end = yearly_data_arg_test (surveys_df, start_year = 1988, end_year = 1993)
print('Both keywords, in order:\t', start, end)

start,end = yearly_data_arg_test (surveys_df, end_year = 1993, start_year = 1988)
print('Both keywords, flipped:\t\t', start, end)

start,end = yearly_data_arg_test (surveys_df, start_year = 1988)
print('One keyword, default end:\t', start, end)

start,end = yearly_data_arg_test (surveys_df, end_year = 1993)
print('One keyword, default start:\t', start, end)


Default values:			 1977 2002
No keywords:			 1988 1993
Both keywords, in order:	 1988 1993
Both keywords, flipped:		 1988 1993
One keyword, default end:	 1988 2002
One keyword, default start:	 1977 1993

Challenge - Loops


In [19]:
for creature in animals:


  File "<ipython-input-19-eac8f13983eb>", line 1
    for creature in animals:
                            ^
SyntaxError: unexpected EOF while parsing

In [20]:
ans = ''
for creature in animals:
    if ans != '':
        ans += ', '
    ans += creature
print(ans)


lion, tiger, crocodile, vulture, hippo