Writing your own functions Flashcards
Define the function shout,
concatenate the string, ‘congratulations’ with another string, ‘!!!’. Assign the result to shout_word, Print the value of shout_word, and Call the shout function.
Define the function shout
def shout():
“"”Print a string with three exclamation marks”””
# Concatenate the strings: shout_word
shout_word=’congratulations’ + ‘!!!’
# Print shout_word print(shout_word)
Call shout
shout()
Complete the function header by adding the parameter name, word.
Assign the result of concatenating word with ‘!!!’ to shout_word.
Print the value of shout_word.
Call the shout() function, passing to it the string, ‘congratulations’.
Define shout with the parameter, word
def shout(word):
“"”Print a string with three exclamation marks”””
# Concatenate the strings: shout_word
shout_word = word + ‘!!!’
# Print shout_word print(shout_word)
Call shout with the string ‘congratulations’
shout(‘congratulations’)
In the function body, concatenate the string in word with ‘!!!’ and assign to shout_word.
Replace the print() statement with the appropriate return statement.
Call the shout() function, passing to it the string, ‘congratulations’, and assigning the call to the variable, yell.
To check if yell contains the value returned by shout(), print the value of yell.
Define shout with the parameter, word
def shout(word):
“"”Return a string with three exclamation marks”””
# Concatenate the strings: shout_word
shout_word=word+’!!!’
# Replace print with return return(shout_word)
Pass ‘congratulations’ to shout: yell
yell= shout(‘congratulations’)
Print yell
print(yell)
output: congratulations!!!
Unpack nums to the variables num1, num2, and num3.
Construct a new tuple, even_nums composed of the same elements in nums, but with the 1st element replaced with the value, 2.
nums = (3, 4, 6)
Unpack nums into num1, num2, and num3
num1, num2, num3 = nums
Construct even_nums
even_nums = (2, num2, num3)
Define shout_all with parameters word1 and word2
# Concatenate word1 with '!!!': shout1 # Concatenate word2 with '!!!': shout2 # Construct a tuple with shout1 and shout2: shout_words # Return shout_words # Pass 'congratulations' and 'you' to shout_all(): yell1, yell2 # Print yell1 and yell2
def shout_all(word1, word2):
Concatenate word1 with '!!!': shout1 shout1=word1+'!!!' # Concatenate word2 with '!!!': shout2 shout2=word2+'!!!' # Construct a tuple with shout1 and shout2: shout_words shout_words=(shout1,shout2) # Return shout_words return shout_words
Pass ‘congratulations’ and ‘you’ to shout_all(): yell1, yell2
yell1,yell2=shout_all(‘congratulations’,’you’)
Print yell1 and yell2
print(yell1)
print(yell2)