There is a ton of built in functions such as str(), type() and so one and so forth.
But you can as well define your own functions
def square(): # <- Function header new_value = 4 ** 2 # <- Function body print(new_value) square() -> 16
You can also define parameters for your functions. In the example below value is the parameter and can be passed when we call the function. Here we pass ‘4’ as the value.
def square(value): new_value = value ** 2 print(new_value) square(4) ->16
You can as well specify a specifc return value from your function
def square(value): new_value = value ** 2 return new_value num = square(4) print(num) ->16
To document your function you put information about your function in docstrings, a sort of comment in the function :
def square(value): """Return the square of a value.""" new_value = value ** 2 return new_value
You can of course accept more than one parameter :
def raise_to_power(value1, value2): """Raise value1 to the power of value2.""" new_value = value1 ** value2 return new_value
Functions can return multiple values, those will be stored in a new object called a tuple ! but what is a tuple ?
- Like a list – can contain multiple values
- immutable – can’t modify values!
- Constructed using parentheses()
even_nums = (2, 4, 6)
You can unpack a tuple into several variables :
even_nums = (2, 4, 6) a, b, c = even_nums
You can access tuple elements like you do with lists
even_nums = (2, 4, 6) print(even_nums[1]) ->4 second_num = even_nums[1] print(second_num) ->4
How does this applies to returning multiple values ?
def raise_both(value1, value2): """Raise value1 to the power of value2 and vice versa.""" new_value1 = value1 ** value2 new_value2 = value2 ** value1 new_tuple = (new_value1, new_value2) return new_tuple result = raise_both(2, 3) print(result) ->(8,9)
End of module exercise is basically : We ingest a csv file containing tweets from twitter and we will try to get the languages of the tweets and how much tweets per language.
# Import pandas import pandas as pd # Import Twitter data as DataFrame: df df = pd.read_csv('tweets.csv') # Initialize an empty dictionary: langs_count langs_count = {} # Extract column from DataFrame: col col = df['lang'] # now what about the function : # Define count_entries() def count_entries(df, col_name): """Return a dictionary with counts of occurrences as value for each key.""" # Initialize an empty dictionary: langs_count langs_count = {} # Extract column from DataFrame: col col = df[col_name] # Iterate over lang column in DataFrame for entry in col: # If the language is in langs_count, add 1 if entry in langs_count.keys(): langs_count[entry]=langs_count[entry]+1 # Else add the language to langs_count, set the value to 1 else: langs_count[entry]=1 # Return the langs_count dictionary return(langs_count) # Call count_entries(): result result=count_entries(tweets_df,'lang') # Print the result print(result)