Recall & Review
beginner
What does the
len() function do in Python?The
len() function returns the number of items in an object like a list, string, tuple, or dictionary.Click to reveal answer
beginner
How can you loop through each item in a list named
fruits?Use a
for loop: <br>for fruit in fruits:<br> print(fruit) This goes through each item one by one.Click to reveal answer
intermediate
What is the difference between
for and while loops?for loops run a set number of times over a sequence. while loops run as long as a condition is true.Click to reveal answer
beginner
How do you find the length of a string
name = 'Alice'?Use
len(name). It will return 5 because 'Alice' has 5 characters.Click to reveal answer
intermediate
What happens if you try to use
len() on an integer like len(5)?You get a
TypeError because integers don’t have a length. len() works only on collections like strings, lists, or tuples.Click to reveal answer
What does
len([1, 2, 3, 4]) return?✗ Incorrect
len() counts the number of items in the list, which is 4.Which loop is best to use when you know exactly how many times to repeat?
✗ Incorrect
for loops are ideal for repeating a fixed number of times over a sequence.What will this code print?<br>
for i in range(3):<br> print(i)✗ Incorrect
range(3) generates numbers 0, 1, 2. The loop prints each number.What does
len('hello') return?✗ Incorrect
The string 'hello' has 5 characters, so
len() returns 5.Which of these can you use
len() on?✗ Incorrect
len() works on collections like lists, strings, tuples, but not on single numbers or booleans.Explain how to find the length of a list and how to loop through its items.
Think about counting items and visiting each one.
You got /4 concepts.
Describe the difference between a for loop and a while loop with examples.
One repeats over a sequence, the other repeats while a condition is true.
You got /3 concepts.