List comprehension with if-else
📖 Scenario: You are working in a fruit store. You have a list of fruit names. You want to create a new list that shows if each fruit is 'Sweet' or 'Sour'. For this, you will use list comprehension with an if-else condition.
🎯 Goal: Create a list of fruits, then use list comprehension with if-else to label each fruit as 'Sweet' or 'Sour'. Finally, print the new list.
📋 What You'll Learn
Create a list called
fruits with these exact values: 'apple', 'lemon', 'banana', 'lime', 'cherry'Create a list comprehension called
taste_labels that labels each fruit as 'Sweet' if the fruit is 'apple', 'banana', or 'cherry', otherwise 'Sour'Print the
taste_labels list💡 Why This Matters
🌍 Real World
Stores and shops often need to categorize items quickly based on properties like taste, price, or availability. Using list comprehension with if-else helps automate this.
💼 Career
Understanding list comprehension with conditions is useful for data processing, filtering, and labeling tasks in many programming jobs.
Progress0 / 4 steps