Sunteți pe pagina 1din 3

#Intro to functions

#csv-Comma Sperated Values

#function - a packaged body of code that can be reused by Calling


with the relevent parameters ex. open() print()

#they take input and present the output ex. open(file,r) - inputs - file and r,
while output is file itself

#Other than reusability, there are 3 main advantages of using functions:

#FUNCTIONS -
They allow us to use other people's code without the necessity to have a deep
understanding of how it was written (e.g., we use the print() function without
reading the code inside it). We call this information hiding.
They break down complex logic into smaller components or modules. Instead of
writing very lengthy and complicated code, we can progress function by function.
For example, if we were writing a larger piece of code, parser() as a function
would be easier to manage rather than the code that executes the same behavior.
This would make testing easier as well. We refer to this as modularity, which is
especially important when working on teams. Modularity makes it easier for someone
else to read, understand, use, and build upon our code.
They streamline our code and make it easier to maintain. Programmers reuse the same
functions in multiple situations across a project. This means that they generalize
the function as much as possible to maximize its usefulness. we call this process
abstraction, which is an important part of reducing our code's complexity,
especially for larger projects.

#ex.

def first_elt(input_lst):
first = input_lst[0]
return first

#Here, first_elt() is the function


input_lst and first are temp. variables and can't be used anywhere else
return value returns when the code terminates

Ex.

def first_elt(m_list):
first = m_list[0]
return first

movie_names = []
for row in movie_data:
temp = first_elt(row)
movie_names.append(temp)
print(movie_names[0:5])

#Ex.Write a function named is_usa() that checks whether or not a movie was made in
the United States.

def is_usa(input_list):
if "USA" == input_list[6]:
return True
else:
return False

us = []
for movie in movie_data:
temp = is_usa(movie)
us.append(temp)
print(us[0:5])

Out - [False,True...

#multiple inputs

def index_equals_stg(input_list, index, stg):


if input_list[index] == stg:
return True
else:
return False

wonder_woman_in_color = index_equals_stg(wonder_woman,2,'Color')

print(wonder_woman_in_color)

#taking care of header row

def counter(input_lst,header_row = False):


num_elt = 0
if header_row == True:
input_lst = input_lst[1:len(input_lst)]
for each in input_lst:
num_elt = num_elt + 1
return num_elt

>>> print(counter(movie_data))
4933
>>> print(counter(movie_data, True))
4932

#find out how many of the movies were made in USA, and store the value in
num_of_us_movies.

#needs a for loop, index == USA, counter


def feature_counter(movie_list, index, name):
num = 0
for row in movie_list:
if row[index] == name:
num = num + 1
return num

num_of_us_movies = feature_counter(movie_data, 6, "USA")


print(num_of_us_movies)

#function inside a function

ex.Write a summary_statistics() function that will take movie_data as input, and


output a dictionary that will give useful numbers from the data.

def feature_counter(input_lst,index, input_str, header_row = False):


num_elt = 0
if header_row == True:
input_lst = input_lst[1:len(input_lst)]
for each in input_lst:
if each[index] == input_str:
num_elt = num_elt + 1
return num_elt

def summary_statistics(input_list):
num_japan_films = feature_counter(input_list, 6, "Japan", True)
num_color_films = feature_counter(input_list, 2, "Color", True)
num_of_films_in_english = feature_counter(input_list, 5, "English", True)
return {
'japan_films':num_japan_films,
'color_films':num_color_films,
'films_in_english':num_of_films_in_english
}

summary = summary_statistics(movie_data)

print(summary)

S-ar putea să vă placă și