Functions with one default argument
# Define shout_echo def shout_echo(word1, echo = 1): """Concatenate echo copies of word1 and three exclamation marks at the end of the string.""" # Concatenate echo copies of word1 using *: echo_word echo_word = word1 * echo # Concatenate '!!!' to echo_word: shout_word shout_word = echo_word + '!!!' # Return shout_word return shout_word # Call shout_echo() with "Hey": no_echo no_echo = shout_echo("Hey") # Call shout_echo() with "Hey" and echo=5: with_echo with_echo = shout_echo("Hey", echo = 5) # Print no_echo and with_echo print(no_echo) print(with_echo)<script.py> output: Hey!!! HeyHeyHeyHeyHey!!!
Functions with multiple default arguments
# Define shout_echo def shout_echo(word1, echo = 1, intense = False): """Concatenate echo copies of word1 and three exclamation marks at the end of the string.""" # Concatenate echo copies of word1 using *: echo_word echo_word = word1 * echo # Capitalize echo_word if intense is True if intense is True: # Capitalize and concatenate '!!!': echo_word_new echo_word_new = echo_word.upper() + '!!!' else: # Concatenate '!!!' to echo_word: echo_word_new echo_word_new = echo_word + '!!!' # Return echo_word_new return echo_word_new # Call shout_echo() with "Hey", echo=5 and intense=True: with_big_echo with_big_echo = shout_echo('Hey', echo = 5, intense = True) # Call shout_echo() with "Hey" and intense=True: big_no_echo big_no_echo = shout_echo('Hey', intense = True) # Print values print(with_big_echo) print(big_no_echo)<script.py> output: HEYHEYHEYHEYHEY!!! HEY!!!
Functions with variable-length arguments (*args)
# Define gibberish def gibberish(*args): """Concatenate strings in *args together.""" # Initialize an empty string: hodgepodge hodgepodge = '' # Concatenate the strings in args for word in args: hodgepodge += word # Return hodgepodge return hodgepodge # Call gibberish() with one string: one_word one_word = gibberish('luke') # Call gibberish() with five strings: many_words many_words = gibberish("luke", "leia", "han", "obi", "darth") # Print one_word and many_words print(one_word) print(many_words)<script.py> output: luke lukeleiahanobidarth
Functions with variable-length keyword arguments (**kwargs) What makes **kwargs different is that it allows you to pass a variable number of keyword arguments to functions. kwargs is a dictionary.
# Define report_status def report_status(**kwargs): """Print out the status of a movie character.""" print("\nBEGIN: REPORT\n") # Iterate over the key-value pairs of kwargs for key, value in kwargs.items(): # Print out the keys and values, separated by a colon ':' print(key + ": " + value) print("\nEND REPORT") # First call to report_status() report_status(name='luke', affiliation='jedi', status='missing') # Second call to report_status() report_status(name='anakin', affiliation='sith lord', status='deceased')这里是引用
<script.py> output: BEGIN: REPORT name: luke affiliation: jedi status: missing END REPORT BEGIN: REPORT name: anakin affiliation: sith lord status: deceased END REPORT # Define count_entries() def count_entries(df, *args): """Return a dictionary with counts of occurrences as value for each key.""" #Initialize an empty dictionary: cols_count cols_count = {} # Iterate over column names in args for col_name in args: # Extract column from DataFrame: col col = df[col_name] # Iterate over the column in DataFrame for entry in col: # If entry is in cols_count, add 1 if entry in cols_count.keys(): cols_count[entry] += 1 # Else add the entry to cols_count, set the value to 1 else: cols_count[entry] = 1 # Return the cols_count dictionary return cols_count # Call count_entries(): result1 result1 = count_entries(tweets_df, 'lang') # Call count_entries(): result2 result2 = count_entries(tweets_df, 'lang', 'source') # Print result1 and result2 print(result1) print(result2)