Skip to content

Instantly share code, notes, and snippets.

@IvanFrecia
Last active April 29, 2021 22:37
Show Gist options
  • Save IvanFrecia/556a8db4f4a93e75278480c095169579 to your computer and use it in GitHub Desktop.
Save IvanFrecia/556a8db4f4a93e75278480c095169579 to your computer and use it in GitHub Desktop.
Coursera Python 3 Programming Specialization - Course 2 - Week 3 - Function Basics - 12.5. Returning a value from a function
# Let’s start by creating a very simple mathematical function that we will call square.
# The square function will take one number as a parameter and return the result of squaring that number.
# Here is the black-box diagram with the Python code following.
def square(x):
y = x * x
return y
toSquare = 10
result = square(toSquare)
print("The result of {} squared is {}.".format(toSquare, result))
# First, an English plan for this new function to define called longer_than_five:
# *You’ll want to pass in a list of strings (representing people’s first names) to the function.
# *You’ll want to iterate over all the items in the list, each of the strings.
# *As soon as you get to one name that is longer than five letters,
# you know the function should return True – yes, there is at least one name longer than five letters!
# *And if you go through the whole list and there was no name longer than five letters,
# then the function should return False.
def longer_than_five(list_of_names):
for name in list_of_names: # iterate over the list to look at each name
if len(name) > 5: # as soon as you see a name longer than 5 letters,
return True # then return True!
# If Python executes that return statement, the function is over and the rest of the code will not run -- you already have your answer!
return False # You will only get to this line if you
# iterated over the whole list and did not get a name where
# the if expression evaluated to True, so at this point, it's correct to return False!
# Here are a couple sample calls to the function with different lists of names. Try running this code in Codelens a few times and make sure you understand exactly what is happening.
list1 = ["Sam","Tera","Sal","Amita"]
list2 = ["Rey","Ayo","Lauren","Natalie"]
print(longer_than_five(list1))
print(longer_than_five(list2)
# What is wrong with the following function definition:
def addEm(x, y, z):
return x+y+z
print('the answer is', x+y+z)
# Ansuer :
#B. You should not have any statements in a function after the return statement.
# Once the function gets to the return statement it will immediately stop executing the function.
# What will the following function return?
def addEm(x, y, z):
print(x+y+z)
# Answer:
# A. The value None
# What will the following code output?
def square(x):
y = x * x
return y
print(square(5) + square(5))
# Answuer:
# B. 50
# What will the following code output?
def square(x):
y = x * x
return y
print(square(square(2)))
# Answuer:
# B. 16 ( It squares 2, yielding the value 4. 4 is then passed as a value to square again, yeilding 16.)
# What will the following code output?
def cyu2(s1, s2):
x = len(s1)
y = len(s2)
return x-y
z = cyu2("Yes", "no")
if z > 0:
print("First one was longer")
else:
print("Second one was at least as long")
# Answer:
# C. First one was longer (cyu2 returns the value 1, which is assigned to z.)
# Which will print out first, square, g, or a number?
def square(x):
print("square")
return x*x
def g(y):
print("g")
return y + 3
print(square(g(2)))
# Ansuer:
# B. g (g has to be executed and return a value in order to know what paramater value to provide to x.)
# How many lines will the following code print?
def show_me_numbers(list_of_ints):
print(10)
print("Next we'll accumulate the sum")
accum = 0
for num in list_of_ints:
accum = accum + num
return accum
print("All done with accumulation!")
show_me_numbers([4,2,3])
# Answer:
# B. 2 (Yes! Two printed lines, and then the function body execution reaches a return statement.)
# Write a function named same that takes a string as input, and simply returns that string.
# Answer:
def same(string):
return string
# Write a function called same_thing that returns the parameter, unchanged.
# Answer:
def same_thing(x):
return x
# Write a function called subtract_three that takes an integer or any number as input,
# and returns that number minus three.
# Ansuer:
def subtract_three(x):
return x - 3
# Write a function called change that takes one number as its input and returns that number, plus 7.
# Answer:
def change(x):
return x + 7
# Write a function named intro that takes a string as input. Given the string “Becky” as input,
# the function should return: “Hello, my name is Becky and I love SI 106.”
# Answer:
def intro(string):
return("Hello, my name is " + string + " and I love SI 106.")
intro("Becky")
print(intro("Becky"))
# Write a function called s_change that takes one string as input and returns that string,
# concatenated with the string ” for fun.”.
# Answer:
def s_change(string):
return(string + " for fun.")
s_change("I printed this")
print(s_change("I printed this"))
# Write a function called decision that takes a string as input, and then checks the number of characters.
# If it has over 17 characters, return “This is a long string”, if it is shorter or has 17 characters,
# return “This is a short string”.
# Answer:
def decision(string):
if len(string) > 17:
return ("This is a long string")
return ("This is a short string")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment