Bird
Raised Fist0
Pythonprogramming~10 mins

len() function in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - len() function
Start with object
Call len() function
Check object type
Calculate number of items
Return length as integer
Use length in program
The len() function takes an object and returns the number of items it contains as an integer.
Execution Sample
Python
my_list = [10, 20, 30]
length = len(my_list)
print(length)
This code finds how many items are in the list and prints that number.
Execution Table
StepActionObjectlen() ResultOutput
1Define my_list[10, 20, 30]
2Call len(my_list)[10, 20, 30]3
3Assign length = 3
4Print length3
💡 Program ends after printing the length 3
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
my_listundefined[10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30]
lengthundefinedundefinedundefined33
Key Moments - 3 Insights
Why does len(my_list) return 3 and not the list itself?
len() counts how many items are inside the list, it does not return the list. See execution_table step 2 where len(my_list) gives 3.
Can len() be used on numbers like 123?
No, len() works on collections like lists, strings, or tuples. Numbers are not collections, so len(123) would cause an error.
What type of value does len() return?
len() always returns an integer representing the count of items, as shown in execution_table step 2 where the result is 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of length after step 3?
Aundefined
B[10, 20, 30]
C3
D0
💡 Hint
Check the 'len() Result' and 'Action' columns in step 3 of the execution_table.
At which step is the len() function called?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table to find when len(my_list) is called.
If my_list had 5 items instead of 3, what would len(my_list) return at step 2?
A3
B5
C0
DError
💡 Hint
len() returns the number of items in the object, so it matches the list length.
Concept Snapshot
len(object)
- Returns the number of items in object
- Works on lists, strings, tuples, etc.
- Returns an integer
- Raises error if object has no length
- Use to find size of collections
Full Transcript
The len() function in Python counts how many items are inside a collection like a list or string. When you call len(my_list), Python checks the list and returns the number of items it contains as an integer. For example, if my_list has three items, len(my_list) returns 3. This value can be stored in a variable and printed. len() does not return the list itself, only the count of items. It works only on objects that have a length, like lists, strings, and tuples. Using len() on a number causes an error because numbers don't have length. The returned value is always an integer representing the size of the collection.

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

  1. Step 1: Understand the purpose of len()

    The len() function counts how many items are inside an object like a string, list, or dictionary.
  2. 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.
  3. Final Answer:

    It returns the number of items in an object like a string or list. -> Option A
  4. 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

  1. Step 1: Recall correct syntax for calling functions

    Functions in Python use parentheses with the object inside, like len(fruits).
  2. Step 2: Check each option's syntax

    length = len(fruits) uses correct syntax. Options B, C, and D misuse brackets, dot notation, or parentheses.
  3. Final Answer:

    length = len(fruits) -> Option C
  4. 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

  1. Step 1: Identify the list elements

    The list my_list contains 4 items: 10, 20, 30, and 40.
  2. Step 2: Apply len() to the list

    The len() function returns the number of items, which is 4.
  3. Final Answer:

    4 -> Option D
  4. 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

  1. Step 1: Check the syntax of the len() function call

    Functions require parentheses around their arguments. Here, len is called without parentheses.
  2. 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.
  3. Final Answer:

    Missing parentheses after len -> Option A
  4. 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

  1. Step 1: Understand what len() returns for a dictionary

    Using len() on a dictionary returns the number of keys.
  2. 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.
  3. Final Answer:

    if len(data) == 3: print("Correct number of keys") -> Option B
  4. Quick Check:

    len(dict) counts keys [OK]
Hint: len(dict) gives number of keys directly [OK]
Common Mistakes:
  • Using > or < instead of == for exact count
  • Checking values or items unnecessarily
  • Misunderstanding what len(dict) returns