Return/Python Question

In this section:

I get function and creating a for loop within the function. What I don’t get it this small detail in the end. The “return”

def freq_table(data_set, index):
frequency_table = {}

for row in data_set[1:]:
    value = row[index]
    if value in frequency_table:
        frequency_table[value] += 1
        frequency_table[value] = 1
return frequency_table 

why can’t I use return freq_table here instead? How does this part of the function work?

and how come when i get my answer:

content_ratings_ft = freq_table(data_set = apps_data, index = 10)

i’m using freq_table instead of frequency_table since i’m returning frequency_table. I might have this all mixed up at the moment.

freq_table is the name of your function you defined so so you wouldn’t want to return that unless you’re using recursion which is not the case here. The point of the return statement is to return the variable(s) you created or manipulated in your function to the main execution of the program.

So when you call your function freq_table(data_set = apps_data, index = 10), it takes those parameters as input to you’re defined function above. It then produces a frequency_table based on the code and returns that frequency_table variable back to your content_ratings_ft variable. Although freq_table function may hold the variable frequency_table within it, if you don’t return frequency_table back to the main program, you won’t be able to use it.

If you’d like to try it, you can remove the return statement completely and remove content_ratings_ft. Run your function and then create a print statement after your function call to see if you can print frequency_table.

Hi @scchoi31

After reading your question, what I understand, you messed up with your function concept.

So, first, let’s understand function.

  • Function is the same as your body parts. Your brain sends a signal (calling leg function with some arguments) to your leg and leg process (uses arguments) the signals to give output for moving (leg function returns the movement).

Same here with freq_table function.

  • freq_table called with some arguments (apps_data, 10) and the function process arguments to give an output of frequency_table.

Let’s understand the code of function freq_table:-

def freq_table(data_set, index):
frequency_table = {}   # assigning empty dictionary

for row in data_set[1:]:   # looping on each row
    value = row[index]     # assign value from row to variable value
    if value in frequency_table:   # checking value in frequency_table dictionary.
        frequency_table[value] += 1   # adding 1 in corressponding key
        frequency_table[value] = 1    # adding new key with value 1
return frequency_table    # return the dictionary frequency_table.

content_ratings_ft = freq_table(data_set = apps_data, index = 10) # Calling the function freq_table with 
# arguments (apps_data, 10) and then function returns the dictionary frequency_table and assign it to content_ratings_ft variable.

You cannot use the same function and variable name to return otherwise it will be difficult to understand what to do (move or return leg).

I hope this helps :slight_smile: .

1 Like

As I’m reviewing this section one last time, I want to point out another question that might be of interests to others and I want to get clarified.

In combining function section 11.

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

as the combing of the extract,find_sum, and find_length functions.

my question is would it be the same thing if we did not have the extract function, can we use the for loop in this situation in place of the extract?

i was trying to fiddle with it but couldn’t get it to work and i know how earlier this chapter we emphasized on using extract function for such situations.

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

You can also write the above code in this way, both are doing the same thing.

def mean(data_set, index):
    column = []
    for row in data_set[1:]:
        value = row[index]

    return find_sum(column) / find_length(column)
1 Like

under_100_m =
for app in android_final:
n_installs = app[5]
n_installs = n_installs.replace("," , " “)
n_install =n_installs. replace(”+", " “)
if (app[1] ==” COMMUNICATION ") and (float(n_installs) < 100000000):

When I run this code, I tend to get an error, telling me could not convert to string.

Please I need help

Hi @pius.emmanuel

Welcome to our Dataquest Community.

Go through the below post and create a good post. Show that everyone can understand your problem and help you to get out off it.

:slight_smile: .