📖 Scenario: Imagine you are organizing a row of mailboxes in your neighborhood. Each mailbox holds letters for one house. You want a simple way to keep track of the mailboxes and the letters inside them.
🎯 Goal: You will create a list (array) to hold the letters for each mailbox. Then you will add letters to specific mailboxes and finally see all the letters in order. This shows why arrays are useful for storing many items in a fixed order.
📋 What You'll Learn
Create a list called mailboxes with 5 empty strings representing empty mailboxes
Create a variable called mailbox_index and set it to 2
Add the letter 'Hello' to the mailbox at mailbox_index
Print the mailboxes list to show all mailboxes and their letters
💡 Why This Matters
🌍 Real World
Arrays are used everywhere to store lists of things like names, scores, or messages in order.
💼 Career
Understanding arrays is essential for programming jobs because they are the foundation for storing and managing data efficiently.
Progress0 / 4 steps
1
Create the mailboxes list
Create a list called mailboxes with exactly 5 empty strings '' representing empty mailboxes.
DSA Python
Hint
Use square brackets [] to create a list with 5 empty strings ''.
2
Set the mailbox index
Create a variable called mailbox_index and set it to the number 2.
DSA Python
Hint
Just write mailbox_index = 2 to store the number 2.
3
Add a letter to the mailbox
Add the string 'Hello' to the mailbox at position mailbox_index in the mailboxes list.
DSA Python
Hint
Use mailboxes[mailbox_index] = 'Hello' to put the letter in the right mailbox.
4
Show all mailboxes
Print the mailboxes list to display all mailboxes and their letters.
DSA Python
Hint
Use print(mailboxes) to see the list with the letter added.
Practice
(1/5)
1. Why do arrays exist in programming languages like Python?
easy
A. To store only one item at a time
B. To store multiple items of the same type in a continuous block of memory
C. To perform mathematical calculations automatically
D. To replace all other data structures
Solution
Step 1: Understand the purpose of arrays
Arrays are designed to hold multiple items of the same type together in memory for easy access.
Step 2: Compare with other options
Options B, C, and D do not correctly describe arrays' purpose; arrays do not store single items only, do not perform calculations automatically, and do not replace all data structures.
Final Answer:
To store multiple items of the same type in a continuous block of memory -> Option B
Quick Check:
Arrays store multiple same-type items [OK]
Hint: Arrays group same-type items together for fast access [OK]