Documente Academic
Documente Profesional
Documente Cultură
#they take input and present the output ex. open(file,r) - inputs - file and r,
while output is file itself
#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
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
wonder_woman_in_color = index_equals_stg(wonder_woman,2,'Color')
print(wonder_woman_in_color)
>>> 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.
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)