Recall & Review
beginner
What does the
len() function do in Python?The
len() function returns the number of items in an object like a string, list, tuple, or dictionary.Click to reveal answer
beginner
How do you use
len() with a string?You pass the string inside the parentheses, like
len('hello'), which returns 5 because 'hello' has 5 characters.Click to reveal answer
beginner
Can
len() be used with dictionaries? What does it return?Yes,
len() works with dictionaries and returns the number of key-value pairs in the dictionary.Click to reveal answer
beginner
What will
len([]) return?It returns
0 because the list is empty and has no items.Click to reveal answer
beginner
Is it possible to use
len() on numbers like len(123)? Why or why not?No,
len() cannot be used on numbers because numbers are not collections or sequences. It only works on objects that contain multiple items.Click to reveal answer
What does
len('Python') return?✗ Incorrect
'Python' has 6 characters, so len('Python') returns 6.What will
len([1, 2, 3, 4]) return?✗ Incorrect
The list has 4 items, so
len() returns 4.Which of these can
len() be used on?✗ Incorrect
len() works on strings because they are sequences of characters.What does
len({'a':1, 'b':2, 'c':3}) return?✗ Incorrect
The dictionary has 3 key-value pairs, so
len() returns 3.What happens if you try
len(100)?✗ Incorrect
Numbers are not collections, so
len(100) raises a TypeError.Explain how the
len() function works with different data types in Python.Think about what kinds of things have a length or count.
You got /5 concepts.
Describe a real-life example where you might use the
len() function in a program.Imagine you want to know how many things are inside a box or how long a word is.
You got /4 concepts.