Course 5.2Lesson 3 of 6
A parameter can have a default value, which Python uses when the call gives no argument for it. Arguments can be named, so the order is free and the call reads clearly. A function can return True or False to answer a question, and a name that starts with is_ tells the reader that it does. One function can call another, so small functions that do one job can be put together like building blocks. A function that returns a value should return it in every path.
Try the code and the simulations beside the lesson.
After this lesson you can
A parameter can have a default value. If the call gives no argument for it, Python uses the default. This is useful when one value is usual, and you want to change it only now and then.
One rule: the parameters with defaults come last. def power(exponent=2, base): is a SyntaxError.
You can name the arguments in a call. The names make a call easy to read, and they let you skip a parameter that has a default.
You have seen this form already: print("a", "b", sep="-") and sort(reverse=True) use named arguments.
A default value makes the common case short, and still lets you choose.
A function can return True or False. Look at the second part of the earlier program: is_even(n) returns the result of n % 2 == 0. A name that starts with is_ (or has_) tells the reader that the function answers yes or no. You can then write natural lines: if is_even(n):.
Here is a style tip. A beginner often writes
def is_even(n):
if n % 2 == 0:
return True
else:
return FalseThis is correct, but it is longer than needed. The condition n % 2 == 0 is already True or False, so return n % 2 == 0 says the same in one line.
describe calls is_even inside its body. Small functions that do one job each are easy to understand and to test, and they can be put together like building blocks. A big problem is then a list of small ones.
A guide: if you can describe what a function does without using the word "and", it does one job. "It finds the area" is one job. "It finds the area and prints a report and saves a file" is three.
If a function has an if that returns, think about what happens when the condition is False. If no return is reached, the function gives back None, and a later sum fails. The usual cure: put a final return at the end for the other case, as in describe.
SyntaxError.if x == True: when x is already a yes/no value. Write if x:.return. The answer is None.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
def power(base, exponent=2): gives exponent a default. Parameters with defaults come last.is_ tells the reader that the function gives True or False.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
power(3) with a default exponent of 2?is_even in one line.make_tag with only a new school?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.