Iterators as function arguments
You've been using the iter()
function to get an iterator object, as well as the next()
function to retrieve the values one by one from the iterator object.
There are also functions that take iterators and iterables as arguments. For example, the list()
and sum()
functions return a list and the sum of elements, respectively.
In this exercise, you will use these functions by passing an iterable from range()
and then printing the results of the function calls.
This exercise is part of the course
Python Toolbox
Exercise instructions
- Create a
range
object that would produce the values from 10 to 20 usingrange()
. Assign the result tovalues
. - Use the
list()
function to create a list of values from the range objectvalues
. Assign the result tovalues_list
. - Use the
sum()
function to get the sum of the values from 10 to 20 from the range objectvalues
. Assign the result tovalues_sum
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a range object: values
values = ____
# Print the range object
print(values)
# Create a list of integers: values_list
values_list = ____
# Print values_list
print(values_list)
# Get the sum of values: values_sum
values_sum = ____
# Print values_sum
print(values_sum)