0
0
NumPydata~15 mins

Random choice from array in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Random Choice from Array with NumPy
📖 Scenario: You are working with a list of fruits in a grocery store. You want to randomly select a fruit to feature as the "Fruit of the Day" on the store's website.
🎯 Goal: Build a small program that uses NumPy to randomly select one fruit from a list of fruits.
📋 What You'll Learn
Create a NumPy array with the exact fruits given
Create a variable to hold the number of fruits
Use NumPy's random choice function to select one fruit randomly
Print the selected fruit
💡 Why This Matters
🌍 Real World
Random selection is useful in many real-world cases like picking a winner in a contest, sampling data, or choosing a random product to promote.
💼 Career
Data scientists often use random sampling to analyze data subsets or to create randomized experiments.
Progress0 / 4 steps
1
Create a NumPy array of fruits
Import NumPy as np and create a NumPy array called fruits with these exact values: 'apple', 'banana', 'cherry', 'date', 'elderberry'.
NumPy
Need a hint?

Use np.array([...]) to create the array.

2
Create a variable for the number of fruits
Create a variable called num_fruits that stores the number of fruits in the fruits array using the len() function.
NumPy
Need a hint?

Use len(fruits) to get the number of items.

3
Select a random fruit using NumPy
Use np.random.choice() to select one random fruit from the fruits array and store it in a variable called random_fruit.
NumPy
Need a hint?

Use np.random.choice(fruits) to pick one item randomly.

4
Print the randomly selected fruit
Print the value stored in random_fruit to display the randomly selected fruit.
NumPy
Need a hint?

Use print(random_fruit) to show the chosen fruit.