Functions Flashcards

1
Q

Sum all the elements of the following list:

a_list = [4444, 8897, 6340, 9896, 4835, 4324, 10, 6445,
661, 1246, 1000, 7429, 1376, 8121, 647, 1280,
3993, 4881, 9500, 6701, 1199, 6251, 4432, 37]

A

sum(a_list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write a function that returns the square of any number

A
def square(a_number):
    squared_number = a_number * a_number
    return squared_number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

First line when writning a new function

A

def nameofthefunction(input):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Second step when writing a new function

A

squared_number = a_number * a_number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Third step when writing a funtion

A

return squared_number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a function that extracts any column from a dataset called apps_data.

A
def extract(index):
    column=[]
    for row in apps_data[1:]:
        value=row[index]
        column.append(value)
    return column
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a frequency table function

A
def freq_table(column):
    frequency_table={}
    for value in column:
        if value in frequency_table:
            frequency_table[value]+=1
        else:
            frequency_table[value]=1
    return frequency_table
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Loop through the apps_data data set (excluding the header).

A

for row in apps_data[1:]:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a function that obtains the mean of a column of a data set

A
def mean(data_set, index):
    column = extract(data_set, index)
    return find_sm(column) / find_length(column)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a function named max() that takes in a list but just returns the string “No max value returned”.

A
def max(a_list):
    return "No max value returned"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a function named max() that takes in a list but just returns the string “No max value returned”.
Use the max() function on the list a_list. Assign the result to a variable named max_val_test_0.

def max(a_list):
    return "No max value returned"
A

max_val_test_0=max(a_list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Run the code del max to delete the max() function you wrote. This will allow you to use the built-in max() function again.

A

del max

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Edit the open_dataset() function and add the name of iOS apps data set (‘AppleStore.csv’) as a default argument for the file_name parameter.

A

def open_dataset(file_name=”AppleStore.csv”):

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Without passing in any argument, use the open_dataset() function to open the AppleStore.csv file. Assign the data set to a variable named apps_data.

A

def open_dataset(file_name=”AppleStore.csv”):

opened_file = open(file_name)   from csv import reader
read_file = reader(opened_file)
data = list(read_file)

return data

apps_data=open_dataset()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

By using the right parameters and arguments:
Round 3.43 to one decimal point. Assign the result to one_decimal.
Round 0.23321 to two decimal points. Assign the result to two_decimals.
Round 921.2225227 to five decimal points. Assign the result to five_decimals.

A

one_decimal=round(3.43, ndigits=1)
two_decimals=round(0.23321,2)
five_decimals=round(921.2225227,5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Add an extra parameter to the open_dataset() function (already written in the code editor) such that it only returns data sets without header rows.

If the parameter indicates that data set has a header row, the function removes the header row before returning the data set.
Else (if the parameter doesn’t indicate that data set doesn’t have a header row), the function returns the data set as it is.
It’s up to you whether you use default arguments or not.

Use the updated open_dataset() function to open the AppleStore.csv file — recall that the AppleStore.csv data set has a header row. Assign the data set to a variable named apps_data.

# INITIAL CODE
def open_dataset(file_name='AppleStore.csv'):
    opened_file = open(file_name)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
A
# SOLUTION CODE
def open_dataset(file_name='AppleStore.csv', header=True):        
    opened_file = open(file_name)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
if header:
    return data[1:]
else:
    return data

apps_data = open_dataset()

17
Q

Edit the open_dataset() function (already written in the code editor) such that:

If the data set has a header, the function returns separately both the header and the rest of the data set.
Else (if there’s no header), the function returns the entire data set.

# INITIAL CODE
def open_dataset(file_name='AppleStore.csv', header=True):        
    opened_file = open(file_name)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
if header:
    return data[1:]
else:
    return data
A
# SOLUTION CODE
def open_dataset(file_name='AppleStore.csv', header=True):        
    opened_file = open(file_name)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
if header:
    return data[1:], data[0]
else:
    return data