DRY Revisited and Function Fundamentals

numbers = [2, 3, 5]
squared = list()
for number in numbers: 
    squared.append(number ** 2)
squared
[4, 9, 25]


def squares_a_list(numerical_list):
    new_squared_list = list()
    for number in numerical_list:
        new_squared_list.append(number ** 2)
    return new_squared_list


squares_a_list(numbers)
[4, 9, 25]
larger_numbers = [5, 44, 55, 23, 11]
promoted_numbers = [73, 84, 95]
executive_numbers = [100, 121, 250, 103, 183, 222, 214]


squares_a_list(larger_numbers)
[25, 1936, 3025, 529, 121]


squares_a_list(promoted_numbers)
[5329, 7056, 9025]


squares_a_list(executive_numbers)
[10000, 14641, 62500, 10609, 33489, 49284, 45796]

Scoping

def squares_a_list(numerical_list):
    new_squared_list = list()
    for number in numerical_list:
        new_squared_list.append(number ** 2)
        print(new_squared_list)
    return new_squared_list


squares_a_list(numbers)
[4]
[4, 9]
[4, 9, 25]
[4, 9, 25]


new_squared_list
NameError: name 'new_squared_list' is not defined

Detailed traceback: 
  File "<string>", line 1, in <module>
def squares_a_list(numerical_list):
    new_squared_list = list()
    for number in numerical_list:
        new_squared_list.append(number ** 2)
        print(new_squared_list)
    return new_squared_list


a_new_variable = "Peek-a-boo"
a_new_variable
'Peek-a-boo'

Global and Local Variables

def squares_a_list(numerical_list):

    print(a_new_variable)
    
    new_squared_list = list()
    for number in numerical_list:
        new_squared_list.append(number ** 2)
    return new_squared_list


squares_a_list([12, 5, 7, 99999])
Peek-a-boo
[144, 25, 49, 9999800001]


404 image

Attribution - Starbucks

Attribution - 49th and Parallel

When things get tricky

a_new_variable = "Peek-a-boo"


def squares_a_list(numerical_list):
    a_new_variable = "Ta-Da!"
    print(a_new_variable)
    
    new_squared_list = list()
    for number in numerical_list:
        new_squared_list.append(number ** 2)
    return new_squared_list


squares_a_list([1, 2])
Ta-Da!
[1, 4]
def squares_a_list(numerical_list):
    a_new_variable = "Ta-Da!"
    print(a_new_variable)
    
    new_squared_list = list()
    for number in numerical_list:
        new_squared_list.append(number ** 2)
    return new_squared_list


squares_a_list([1, 2])
a_new_variable
Ta-Da!
'Peek-a-boo'
def squares_a_list(numerical_list, a_new_variable):
    print(a_new_variable)
    
    new_squared_list = list()
    for number in numerical_list:
        new_squared_list.append(number ** 2)
    return new_squared_list


a_new_variable = "Peek-a-boo"
squares_a_list([1,2], "BAM!")
BAM!
[1, 4]

Modifying global variables

global_list = [50, 51, 52]


def squares_a_list(numerical_list):
    global_list.append(99)
    print("print global_list:", global_list)
    
    new_squared_list = list()
    for number in numerical_list:
        new_squared_list.append(number ** 2)
    return new_squared_list


squares_a_list([1, 2])
global_list
print global_list: [50, 51, 52, 99]
[50, 51, 52, 99]

Function Side Effects

cereal = pd.read_csv('data/cereal.csv')
cereal.head()
name mfr type calories ... shelf weight cups rating
0 100% Bran N Cold 70 ... 3 1.0 0.33 68.402973
1 100% Natural Bran Q Cold 120 ... 3 1.0 1.00 33.983679
2 All-Bran K Cold 70 ... 3 1.0 0.33 59.425505
3 All-Bran with Extra Fiber K Cold 50 ... 3 1.0 0.50 93.704912
4 Almond Delight R Cold 110 ... 3 1.0 0.75 34.384843

5 rows × 16 columns


  • .drop()
  • .assign()
  • .sort_values()
  • .rename()
cereal_dropped = cereal.drop(columns = ['sugars','potass','vitamins', 'shelf', 'weight', 'cups'])
cereal_dropped.head(2)
name mfr type calories ... sodium fiber carbo rating
0 100% Bran N Cold 70 ... 130 10.0 5.0 68.402973
1 100% Natural Bran Q Cold 120 ... 15 2.0 8.0 33.983679

2 rows × 10 columns


cereal.head(2)
name mfr type calories ... shelf weight cups rating
0 100% Bran N Cold 70 ... 3 1.0 0.33 68.402973
1 100% Natural Bran Q Cold 120 ... 3 1.0 1.00 33.983679

2 rows × 16 columns


cereal.drop(columns = ['sugars','potass','vitamins', 'shelf', 'weight', 'cups'], inplace=True)


cereal.head(2)
name mfr type calories ... sodium fiber carbo rating
0 100% Bran N Cold 70 ... 130 10.0 5.0 68.402973
1 100% Natural Bran Q Cold 120 ... 15 2.0 8.0 33.983679

2 rows × 10 columns

cereal.to_csv('data/cereal.csv')


regular_list = [7, 8, 2020]
regular_list
[7, 8, 2020]


regular_list.append(3)


regular_list
[7, 8, 2020, 3]

Side Effect Documentation

  • If your functions have side-effects, they should be documented.

The deal with print()

print('A regular string')
A regular string


a_number_variable = 54.346

print(a_number_variable)
54.346
def squares_a_list(numerical_list):
    new_squared_list = list()
    for number in numerical_list:
        new_squared_list.append(number ** 2)
    return new_squared_list


def squares_a_list_print(numerical_list):
    new_squared_list = list()
    for number in numerical_list:
        new_squared_list.append(number ** 2)
    print(new_squared_list)
numbers = [2, 3, 5]


squares_a_list(numbers)
[4, 9, 25]


squares_a_list_print(numbers)
[4, 9, 25]
return_func_var = squares_a_list(numbers)


print(return_func_var)
[4, 9, 25]


print_func_var = squares_a_list_print(numbers)
[4, 9, 25]


print(print_func_var)
None

Let’s apply what we learned!