0
0
Pythonprogramming~20 mins

List length and membership test in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
List Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this code?
Consider the following Python code. What will be printed?
Python
fruits = ['apple', 'banana', 'cherry', 'date']
print(len(fruits))
print('banana' in fruits)
print('grape' in fruits)
A
4
True
False
B
3
True
False
C
4
False
True
D
3
False
True
Attempts:
2 left
๐Ÿ’ก Hint
Remember, len() counts all items in the list. The 'in' keyword checks if an item exists.
โ“ Predict Output
intermediate
2:00remaining
What is the length of the list after modification?
What will be the output of the following code?
Python
numbers = [1, 2, 3, 4, 5]
numbers.append(6)
numbers.remove(2)
print(len(numbers))
A7
B6
C4
D5
Attempts:
2 left
๐Ÿ’ก Hint
Appending adds one item, removing deletes one item.
โ“ Predict Output
advanced
2:00remaining
What does this code print?
Analyze the code and select the correct output.
Python
items = ['pen', 'pencil', 'eraser', 'pen']
print(items.count('pen'))
print('pen' in items)
print(len(items))
A
2
True
4
B
1
True
3
C
2
False
4
D
1
False
3
Attempts:
2 left
๐Ÿ’ก Hint
count() returns how many times an item appears.
โ“ Predict Output
advanced
2:00remaining
What error does this code raise?
What error will this code produce when run?
Python
my_list = [10, 20, 30]
print(my_list[3])
AValueError
BKeyError
CIndexError
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
List indices start at 0 and go up to length-1.
๐Ÿง  Conceptual
expert
2:00remaining
How many items are in the resulting list?
What is the length of the list after this code runs?
Python
a = [1, 2]
b = a
b.append(3)
print(len(a))
A2
B3
C1
DError
Attempts:
2 left
๐Ÿ’ก Hint
Remember that lists are mutable and variables can reference the same list.