Discussion 1: Functions
Ice Breaker
Everyone say your name and some activity you enjoy doing outside. For each activity, ask if anyone else in your group likes to do that too. (Optional: after discussion you could make plans to go do one of these activities together.)
Then, each person share an expression that people say when they really like something, such as "awesome" or "nice one" or "that's tuff." Each person should try to come up with a different expression. Feel free to ask your group for help if you're stuck. You can even use other languages than English. Then, during the discussion, if someone says or does something well, use your expression!
Functions
Functions (such as pow) are called using call expressions (such as
pow(10, 3) which returns 1000; 10 to the third power). The body of a function
can print or return values (or both).
- When
printis called anywhere, its arguments are displayed right away. - Executing a
returnstatement stops the function call and provides the value for its call expression. - When the end of a function body is reached without a
return, the function returnsNone.
Tip: If a value needs to be used by code outside of a function, then return the value (instead of printing it).
Q1: Multiply
Implement multiply, which takes two numbers x and y. It prints out an
equation showing the result of multiplying x and y, then returns the result.
Try to multiply numbers together just once in the body of multiply so that the
Python interpreter doesn't have to perform multiplication more times than
necessary (pun intended).
def multiply(x, y):
"""Multiply x by y and print what happened.
>>> multiply(3, multiply(multiply(4, 5), 6))
4 * 5 = 20
20 * 6 = 120
3 * 120 = 360
360
"""
Hint 1: Printing an equation (at the end)
Q2: Cut One Out
Implement cut, which takes non-negative integers n and k and has only a
return statement in its body. It returns a positive integer with all of the
digits of n except the digit that is k positions to the left of the
rightmost digit (the one's digit). If k is 0, then it returns n without its
one's digit. If there is no digit of n that is k positions to the left of
the rightmost digit, then it returns n.
def cut(n, k):
"""Return n with the kth digit from the right removed.
>>> cut(3579, 2)
379
>>> cut(3579, 0)
357
>>> cut(3579, 1)
359
>>> cut(3579, 5)
3579
"""
return ____
Please don't look at the hint until everyone in your group agrees that you're stuck and need some extra help.
Hint 2 (at the end)
Hints
Hint 1: Printing an equation
Calling print on multiple arguments displays those values separated by spaces.
For example, the call expression print(1, '+', 2, '=', 3) displays 1 + 2 = 3.
Hint 2
To implement a function, it can be helpful to open a Python interpreter and
focus on an example. Here are all the pieces you need to put together to solve
this problem for the example n=3579 and k=2.
>>> n = 3579
>>> k = 2
>>> pow(10, k)
100
>>> pow(10, k + 1)
1000
>>> n // 1000
3
>>> 3 * 100
300
>>> 3579 % 100
79
>>> 300 + 79
379