0
0
Pythonprogramming~5 mins

Methods with return values in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does a method with a return value do?
It performs a task and then sends back a result to the place where it was called.
Click to reveal answer
beginner
How do you define a method that returns a value in Python?
Use the def keyword to define the method, then use return followed by the value you want to send back.
Click to reveal answer
beginner
What happens if a method does not have a return statement?
It returns None by default, which means it sends back no useful value.
Click to reveal answer
beginner
Why is it useful for a method to return a value?
Because it lets you use the result later in your program, like saving it in a variable or printing it.
Click to reveal answer
beginner
Example: What does this method return?<br>
def add(a, b):
    return a + b
It returns the sum of a and b. For example, add(2, 3) returns 5.
Click to reveal answer
What keyword is used to send a value back from a method in Python?
Aoutput
Bsend
Cyield
Dreturn
What does a method return if there is no return statement?
ANone
BAn error
CFalse
D0
Which of these is a correct method that returns the square of a number?
Adef square(x): x * x
Bdef square(x): print(x * x)
Cdef square(x): return x * x
Ddef square(x): return
Why might you want a method to return a value?
ATo reuse the result later
BTo stop the program
CTo print something
DTo create a new method
What will this code print?<br>
def greet():
    return 'Hello!'
print(greet())
ANone
BHello!
Cgreet
DError
Explain how a method with a return value works and why it is useful.
Think about how you get answers back when you ask a question.
You got /3 concepts.
    Write a simple Python method that takes two numbers and returns their product.
    Use <code>def</code> and <code>return a * b</code>.
    You got /3 concepts.