Example
Return Statement Basics
Return statement returns a value from function. Function can perform calculations and give result.
Showing 1–10 of 78
Example
Return statement returns a value from function. Function can perform calculations and give result.
Quiz
Free beginner quiz on Python return values: 15 questions on return, None, several values, lists, dictionaries and early exit. Instant answers.
Challenge
to_kelvin() prints its result instead of returning it, so the caller gets None. Predict the last line, find out what the function gives back, fix it with return, then write a conversion function that…
Example
Functions can return complex data structures like dictionaries or lists.
Example
Function can return multiple values as a tuple. Unpacking on assignment.
Challenge
…the four nested ifs in can_dock() as guard clauses that return a reason as soon as a check fails, keeping every answer the same, then add a new docking rule as one more guard with a default parameter.
Challenge
status_light() returns None for Scrub because that branch sets a variable but never returns it. Predict the last line, find which statuses give None, make every path return, then write a function…
Challenge
Write summarise(readings), which returns the count, min, max and mean of a list of readings as one dict, then make it handle a pass with no data and round the mean, so any program can use the summary.
Challenge
…parser from "Line to record" into parse_line(line), which returns the record dict, then let it return None for blank, comment and damaged lines so the loop only keeps real records.
Challenge
…the first line, prove both names share one list, fix it by returning a new list, then write a filter that leaves its input alone.