Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
✗ Incorrect
'Python' has 6 characters, so len('Python') returns 6.
What will len([1, 2, 3, 4]) return?
A4
B3
C5
DError
✗ Incorrect
The list has 4 items, so len() returns 4.
Which of these can len() be used on?
AStrings
BInteger numbers
CFloating point numbers
DNone of the above
✗ Incorrect
len() works on strings because they are sequences of characters.
What does len({'a':1, 'b':2, 'c':3}) return?
A2
B6
CError
D3
✗ Incorrect
The dictionary has 3 key-value pairs, so len() returns 3.
What happens if you try len(100)?
AReturns 3
BReturns 1
CRaises a TypeError
DReturns 0
✗ 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.
Practice
(1/5)
1. What does the len() function do in Python?
easy
A. It returns the number of items in an object like a string or list.
B. It adds two numbers together.
C. It prints text to the screen.
D. It creates a new list.
Solution
Step 1: Understand the purpose of len()
The len() function counts how many items are inside an object like a string, list, or dictionary.
Step 2: Compare options with the function's purpose
Only It returns the number of items in an object like a string or list. correctly describes this behavior. Other options describe different actions.
Final Answer:
It returns the number of items in an object like a string or list. -> Option A
Quick Check:
len() counts items [OK]
Hint: Remember: len() counts items inside, not math or printing [OK]
Common Mistakes:
Thinking len() adds numbers
Confusing len() with print()
Believing len() creates new lists
2. Which of the following is the correct way to use len() to find the length of a list named fruits?
easy
A. length = fruits.len()
B. length = len[fruits]
C. length = len(fruits)
D. length = len.fruits()
Solution
Step 1: Recall correct syntax for calling functions
Functions in Python use parentheses with the object inside, like len(fruits).
Step 2: Check each option's syntax
length = len(fruits) uses correct syntax. Options B, C, and D misuse brackets, dot notation, or parentheses.
Final Answer:
length = len(fruits) -> Option C
Quick Check:
Use parentheses with len() [OK]
Hint: Use parentheses with len(), not brackets or dots [OK]
Common Mistakes:
Using square brackets instead of parentheses
Trying to call len() as a method on the object
Using dot notation incorrectly
3. What will be the output of this code?
my_list = [10, 20, 30, 40]
print(len(my_list))
medium
A. 3
B. Error
C. 40
D. 4
Solution
Step 1: Identify the list elements
The list my_list contains 4 items: 10, 20, 30, and 40.
Step 2: Apply len() to the list
The len() function returns the number of items, which is 4.
Final Answer:
4 -> Option D
Quick Check:
len([10,20,30,40]) = 4 [OK]
Hint: Count items inside the list to find len() [OK]
Common Mistakes:
Confusing last item value with length
Off-by-one counting errors
Expecting len() to return sum
4. The following code gives an error. What is the problem?
my_string = "hello"
print(len my_string)
medium
A. Missing parentheses after len
B. Using double quotes instead of single quotes
C. Variable name is invalid
D. len() cannot be used on strings
Solution
Step 1: Check the syntax of the len() function call
Functions require parentheses around their arguments. Here, len is called without parentheses.
Step 2: Identify the error cause
Missing parentheses cause a syntax error. Other options are incorrect because double quotes are allowed, variable name is valid, and len() works on strings.
Final Answer:
Missing parentheses after len -> Option A
Quick Check:
Always use parentheses with len() [OK]
Hint: Always put parentheses after len() [OK]
Common Mistakes:
Forgetting parentheses after function name
Thinking quotes affect len() usage
Assuming len() can't handle strings
5. You have a dictionary data = {'a': 1, 'b': 2, 'c': 3}. You want to check if it has exactly 3 keys before processing. Which code correctly uses len() for this check?
hard
A. if len(data.values()) < 3:
print("Correct number of keys")
B. if len(data) == 3:
print("Correct number of keys")
C. if len(data.keys()) > 3:
print("Correct number of keys")
D. if len(data.items()) != 3:
print("Correct number of keys")
Solution
Step 1: Understand what len() returns for a dictionary
Using len() on a dictionary returns the number of keys.
Step 2: Check the condition for exactly 3 keys
if len(data) == 3:
print("Correct number of keys") checks if len(data) equals 3, which is correct. Other options check for greater than, less than, or not equal, which do not match the requirement.
Final Answer:
if len(data) == 3:
print("Correct number of keys") -> Option B
Quick Check:
len(dict) counts keys [OK]
Hint: len(dict) gives number of keys directly [OK]