<jupyter_start><jupyter_text>Functions we have covered so far - `print()`- `sum()`- `len()`- `min()`- `max()`Generally, a function displays this pattern:1. It takes in an input2. It does something to that input3. It gives back an output<jupyter_code>list_1 = [21, 0, 72, 2, 5]
len(list_1)<jupyter_output><empty_output><jupyter_text>Above, the `len()` function:1. Took `list_1` as an input2. It did something to input (`list_1`) to be able to count its length.3. It returned the output 5 - the length of `list_1`<jupyter_code>length = 0

for element in list_1:
    length += 1
print(length)<jupyter_output>5<jupyter_text>Without using the `sum()` function, write code that computes the sum of the elements of a list.<jupyter_code>list_1 = [1, 8, 10, 9, 7, 5, 9, 1, 8, 4, 5, 3, 3, 2, 0, 9,
          6, 5, 2, 6, 9, 8, 8, 1, 2, 8, 0, 3, 4, 8, 6, 2,
          2, 6, 9, 2, 9, 9, 1, 8, 10, 10, 2, 2, 3, 6, 10,
          9, 8, 7]

list_2 = [678, 685, 870, 927, 176, 893, 366, 780, 261, 815,
          204, 946, 465, 670, 19, 632, 182, 46, 13, 202, 566,
          609, 481, 18, 992, 490, 878, 398, 942, 694, 763,
          986, 825, 843, 798, 658, 426, 613, 14, 369, 638,
          831, 585, 608, 588, 418, 117, 18, 755, 680]

list_3 = [4444, 8897, 6340, 9896, 4835, 4324, 10000, 6445,
          661, 1246, 1000, 7429, 1376, 8121, 647, 1280, 1318,
          3993, 4881, 9500, 6701, 1199, 6251, 4432, 3717,
          5929, 7061, 4214, 5127, 6171, 3782, 3798, 8970,
          3102, 9771, 746, 4974, 7996, 3122, 1362, 8140, 4412,
          1390, 2240, 3063, 4228, 7030, 8479, 5081, 66]
sum_1 = 0
for element in list_1:
    sum_1 += element

print(sum_1) # 275

sum_2 = 0 

for element in list_2:
    sum_2 += element

print(sum_2) #27431

sum_3 = 0
for element in list_3:
    sum_3 += element
    
print(sum_3) #239187
ratings = ['4+', '4+', '4+', '9+', '12+', '12+', '17+', '17+']

content_ratings = {}

for element in ratings:
    if element in content_ratings:
        content_ratings[element] += 1
    else:
        content_ratings[element] = 1

print(content_ratings)
n_unique = len(content_ratings)
def square(a_number): # header
    squared_number = a_number * a_number # body
    return squared_number # return statement
square(7)<jupyter_output><empty_output><jupyter_text>To create the `square()` function, we:- Start with the `def` statement, where we:    - Specified the name of the function (square)    - Specified the name of the variable (a_number) that will serve as input    - Surrounded the input variable `a_number` within parenthesis    - Ended the line of code with a colon: - Specified what we want to do with the input (`a_number`) - in the code below the `def` statement    - We multiplied `a_number` by itself: `a_number` * `a_number`    - Then we assigned the result to a variable named `squared_number`- Ended with the `return` statement, where we specified what we want returned as the output    - The output is the variable `squared_number`, which stores the result of `a_number` * `a_number` We'll often use the phrase "inside the function's definition" to refer to the function's body.<jupyter_code>squared_10 = square(10)
print(squared_10)
squared_16 = square(16)
print(squared_16)
def square_2(a_number, a_exponent): # header
    squared_number = a_number ** a_exponent # body
    return squared_number # return statement
square_2(7,2)
squared_20 = square_2(20,2)
print(squared_20)
squared_100 = square_2(100, 2)
print(squared_100)
def add_10(a_number):
    addition = a_number + 10
    return addition
add_30 = add_10(30)
print(add_30)
add_90 = add_10(90)
print(add_90)
def square(a_number): # header
    return a_number * a_number # body & return
squared_6 = square(6)
print(squared_6)
squared_11 = square(11)
print(squared_11)
a_list = [2, 1, 'data point', 'five', 89, 1, 'zero', True, 2.332]
a_dictionary = {1: 'Name', 2: True, 3: [1,2,3], 4: 9.2221, 5: 5}
a_list.in
def len_2(a_variable):
    length = 0
    for element in a_variable:
        length += 1
    return length
list_length = len_2(a_list)
print(list_length)
dictionary_length = len_2(a_dictionary)
print(dictionary_length)<jupyter_output>9
5<jupyter_text>Creating two functions that generates frequency tables for any column<jupyter_code>def extract(index):
    column = []
    for row in apps_data[1:]:
        value = row[index]
        column.append(value)
    return column
def freq_table(column):
    frequency = {}
    
    for value in column:
        if value in frequency:
            frequency[value] += 1
        else:
            frequency[value] = 1
            
    return frequency
content_ratings = extract(11)
content_ratings_ft = freq_table(content_ratings)
print(content_ratings_ft)
# print(content_rating)
ratings = extract(8)
ratings_ft = freq_table(ratings)
print(ratings_ft)
genres = extract(12)
genres_ft = freq_table(genres)
print(genres_ft)<jupyter_output>{'Games': 3862, 'Productivity': 178, 'Weather': 72, 'Shopping': 122, 'Reference': 64, 'Finance': 104, 'Music': 138, 'Utilities': 248, 'Travel': 81, 'Social Networking': 167, 'Sports': 114, 'Business': 57, 'Health & Fitness': 180, 'Entertainment': 535, 'Photo & Video': 349, 'Navigation': 46, 'Education': 453, 'Lifestyle': 144, 'Food & Drink': 63, 'News': 75, 'Book': 112, 'Medical': 23, 'Catalogs': 10}<jupyter_text>Create one function that generates frequency tables for any column<jupyter_code>def freq_table_2(index):
    frequency_table = {}
    
    for row in apps_data[1:]:
        value = row[index]
             
        if value in frequency_table:
            frequency_table[value] += 1
        else:
            frequency_table[value] = 1
            
    return frequency_table
content_ratings_ft = freq_table_2(11)
print(content_ratings_ft)
ratings_ft = freq_table_2(8)
print(ratings_ft)
genres_ft = freq_table_2(12)
print(genres_ft)<jupyter_output>{'4+': 4433, '12+': 1155, '17+': 622, '9+': 987}
{'4': 1626, '3.5': 702, '4.5': 2663, '5': 492, '3': 383, '2': 106, '2.5': 196, '0': 929, '1.5': 56, '1': 44}
{'Games': 3862, 'Productivity': 178, 'Weather': 72, 'Shopping': 122, 'Reference': 64, 'Finance': 104, 'Music': 138, 'Utilities': 248, 'Travel': 81, 'Social Networking': 167, 'Sports': 114, 'Business': 57, 'Health & Fitness': 180, 'Entertainment': 535, 'Photo & Video': 349, 'Navigation': 46, 'Education': 453, 'Lifestyle': 144, 'Food & Drink': 63, 'News': 75, 'Book': 112, 'Medical': 23, 'Catalogs': 10}<jupyter_text>Creating a function that is reusable<jupyter_code>def freq_table_3(index, dataset):
    frequency_table = {}
    
    for row in dataset:
        value = row[index]
        
        if value in frequency_table:
            frequency_table[value] += 1
        else:
            frequency_table[value] = 1
            
    return frequency_table
content_ratings_ft = freq_table_3(11, apps_data[1:])
print(content_ratings_ft)
ratings_ft = freq_table_3(8, apps_data[1:])
print(ratings_ft)
genres_ft = freq_table_3(12, apps_data[1:])
print(genres_ft)<jupyter_output>{'4+': 4433, '12+': 1155, '17+': 622, '9+': 987}
{'4': 1626, '3.5': 702, '4.5': 2663, '5': 492, '3': 383, '2': 106, '2.5': 196, '0': 929, '1.5': 56, '1': 44}
{'Games': 3862, 'Productivity': 178, 'Weather': 72, 'Shopping': 122, 'Reference': 64, 'Finance': 104, 'Music': 138, 'Utilities': 248, 'Travel': 81, 'Social Networking': 167, 'Sports': 114, 'Business': 57, 'Health & Fitness': 180, 'Entertainment': 535, 'Photo & Video': 349, 'Navigation': 46, 'Education': 453, 'Lifestyle': 144, 'Food & Drink': 63, 'News': 75, 'Book': 112, 'Medical': 23, 'Catalogs': 10}<jupyter_text>Calling functions by using postional or keyword arguments<jupyter_code># Using Positional Arguments
content_ratings_ft = freq_table_3(11, apps_data[1:])
print(content_ratings_ft)
# Using Keyword Arguments
ratings_ft = freq_table_3(dataset=apps_data[1:], index=8)
print(ratings_ft)
genres_ft = freq_table_3(index=12, dataset=apps_data[1:])
print(genres_ft)
def extract(data_set, index):
    column = []
    
    for row in data_set:
        value = row[index]
        column.append(value)
        
    return column
# extract(data_set=apps_data[1:], index = 3)
def find_sum(a_list):
    a_sum = 0
    
    for num in a_list:
        a_sum += float(num)
        
    return a_sum

def find_length(a_list):
    length = 0
    
    for num in a_list:
        length += 1
        
    return length

def mean(data_set, index):
    column = extract(data_set, index)
    return find_sum(column) / find_length(column)
    
avg_price = mean(new_apps_data[1:], 4)
print(avg_price)
avg_rating = mean(new_apps_data[1:], 7)
print(avg_rating)
def extract(data_set, index):
    column = []
    
    for row in data_set[1:]:
        value = row[index]
        column.append(value)
    
    return column

def find_sum(a_list):
    a_sum = 0
    for element in a_list:
        a_sum += float(element)
    return a_sum

def find_length(a_list):
    length = 0
    for element in a_list:
        length += 1
    return length

def mean(data_set, index):
    column = extract(data_set, index)
    return find_sum(column) / find_length(column)

avg_price = mean(apps_data, 4)
print(avg_price)
avg_rating = mean(apps_data, 7)
print(avg_rating)<jupyter_output>1.7262178685562626
3.526955675976101
{'fraction_non_alphanumeric': 0.13796095444685466, 'fraction_numerical': 0.10433839479392625, 'mean_word_length': 3.8328092243186584, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '342946', 'license': 'MIT', 'n_tokens_mistral': 4145, 'n_tokens_neox': 3363, 'n_words': 997, 'path': 'dataquest/01-python_for_data_science_fundamentals/dataquest-projects-master/Functions-Fundamentals.ipynb', 'repo_name': 'pbittencourt/python4DS', 'size': '9220'}