def three_shouts(word1, word2, word3):
"""Returns a tuple of strings concatenated with '!!!' """
def inner(word): # Define inner
"""Returns a string concatenated with '!!!'."""
return word + '!!!'
return (inner(word1), inner(word2), inner(word3)) # Return a tuple of strings
print(three_shouts('a', 'b', 'c'))
def echo_shout(word):
"""Change the value of a nonlocal variable"""
echo_word = word*2 # Concatenate word with itself
print(echo_word)
def shout(): # Define inner function shout()
"""Alter a variable in the enclosing scope"""
nonlocal echo_word # Use echo_word in nonlocal scope
echo_word = echo_word + "!!!" # Change echo_word to echo_word concatenated with '!!!'
shout() # Call function shout()
print(echo_word)
echo_shout('hello')
def power(number, pow=1):
“””Raise number to the power of pow.”””
new_value = number ** pow
return new_value
power(9, 2)
def add_all(*args):
“””Sum all values in *args together.”””
sum_all = 0 # Initialize sum
for num in args:
sum_all += num # Accumulate the sum
return sum_all
def print_all(**kwargs):
“””Print out key-value pairs in **kwargs”””
for key, value in kwargs.items(): # Print out the key-value pairs
print(key + ":" + value)
print_all(name=’a’, job=’b’)
def shout_echo(word1, echo=1):
"""Concatenate echo copies of word1 and three exclamation marks at the end of the string."""
echo_word = word1 * echo # Concatenate echo copies of word1
shout_word = echo_word + '!!!' # Concatenate '!!!' to echo_word
return shout_word # Return shout_word
no_echo = shout_echo("Hey") # Call shout_echo() with "Hey"
with_echo = shout_echo("Hey", 5) # Call shout_echo() with "Hey" and echo=5
print(no_echo)
print(with_echo)
def gibberish(*args):
"""Concatenate strings in *args together."""
hodgepodge = "" # Initialize an empty string
for word in args:
hodgepodge += word
return hodgepodge
one_word = gibberish("luke")
many_words = gibberish("luke", "leia", "han", "obi", "darth")
print(one_word)
print(many_words)
def report_status(**kwargs):
"""Print out the status of a movie character."""
print("\nBEGIN: REPORT\n")
for keys, values in kwargs.items(): # Iterate over the key-value pairs of kwargs
print(keys + ": " + values)
print("\nEND REPORT")
report_status(name="luke",affiliation="jedi",status="m") # First call to report_status()
report_status(name="anakin", affiliation="", status="d") # Second call to report_status()
import pandas as PD
tweets_df = pd.read_csv(‘tweets.csv’)
def count_entries(df, col_name='lang'):
"""Return a dictionary with counts of occurrences as value for each key."""
cols_count = {} # Initialize an empty dictionary
col = df[col_name] # Extract column from DataFrame
# Iterate over the column in DataFrame
for entry in col:
if entry in cols_count.keys(): # If entry is in cols_count, add 1
cols_count[entry] += 1
else:
cols_count[entry] = 1
return cols_count
result1 = count_entries(tweets_df, "lang")
result2 = count_entries(tweets_df, source")
print(result1)
print(result2)
import pandas as PD
tweets_df = pd.read_csv(‘tweets.csv’)
def count_entries(df, *args):
"""Return a dictionary with counts of occurrences as value for each key."""
cols_count = {} #Initialize an empty dictionary
for col_name in args: # Iterate over column names in args
col = df[col_name] # Extract column from DataFrame
# Iterate over the column in DataFrame
if entry in cols_count.keys(): # If entry is in cols_count, add 1
cols_count[entry] += 1
else:
cols_count[entry] = 1
return cols_count
result1 = count_entries(tweets_df, "lang")
result2 = count_entries(tweets_df, "lang", "source")
print(result1)
print(result2)