0
0
Pythonprogramming~5 mins

len() function in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A5
B6
C7
DError
What will len([1, 2, 3, 4]) return?
A4
B3
C5
DError
Which of these can len() be used on?
AStrings
BInteger numbers
CFloating point numbers
DNone of the above
What does len({'a':1, 'b':2, 'c':3}) return?
A2
B6
CError
D3
What happens if you try len(100)?
AReturns 3
BReturns 1
CRaises a TypeError
DReturns 0
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.