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
Counting Specific Items in a Fruit Basket
๐ Scenario: Imagine you have a basket full of different fruits. You want to find out how many apples are in the basket.
๐ฏ Goal: You will write a program that counts how many times the fruit 'apple' appears in a list of fruits.
๐ What You'll Learn
Create a list called fruits with specific fruit names
Create a variable called target_fruit to hold the fruit name to count
Use a for loop to count how many times target_fruit appears in fruits
Print the count with a clear message
๐ก Why This Matters
๐ Real World
Counting specific items in a list is useful in many real-life situations, like counting votes, inventory items, or survey answers.
๐ผ Career
This skill helps in data analysis, quality control, and any job that requires summarizing or searching through data.
Progress0 / 4 steps
1
Create the fruit list
Create a list called fruits with these exact items in order: 'apple', 'banana', 'apple', 'orange', 'apple', 'banana'
Python
Hint
Use square brackets [] to create a list and separate items with commas.
2
Set the fruit to count
Create a variable called target_fruit and set it to the string 'apple'
Python
Hint
Use an equals sign = to assign the string 'apple' to target_fruit.
3
Count how many times the target fruit appears
Create a variable called count and set it to 0. Then use a for loop with the variable fruit to go through each item in fruits. Inside the loop, if fruit is equal to target_fruit, add 1 to count.
Python
Hint
Remember to start count at zero before the loop. Use for fruit in fruits: to check each fruit.
4
Print the count result
Write a print statement to display: "Number of apples: " followed by the value of count using an f-string.
Python
Hint
Use print(f"Number of apples: {count}") to show the result clearly.
Practice
(1/5)
1. Which Python operator checks if an element exists in a list?
easy
A. in
B. count()
C. find()
D. exists()
Solution
Step 1: Understand the purpose of in
The in operator checks if an element is present in a list or other collection.
Step 2: Compare with other options
count() counts occurrences, find() and exists() are not valid list operators in Python.
Final Answer:
in -> Option A
Quick Check:
Use in to check membership [OK]
Hint: Use in to check presence quickly [OK]
Common Mistakes:
Confusing count() with membership check
Using non-existent methods like find()
Trying to use exists() which is invalid
2. Which of the following is the correct syntax to count how many times the number 5 appears in list nums?
easy
A. count(nums, 5)
B. nums.count(5)
C. nums.count = 5
D. nums.count[5]
Solution
Step 1: Identify the correct method call
To count occurrences, use the list method count() with the element as argument: nums.count(5).
Step 2: Check syntax of other options
count(nums, 5) is invalid syntax, nums.count = 5 assigns a value incorrectly, and nums.count[5] is invalid indexing.
Final Answer:
nums.count(5) -> Option B
Quick Check:
Use list.count(element) to count [OK]
Hint: Use list.count(value) to count occurrences [OK]