Python · Chapter 2.5 · Intermediate level

Lists & Dictionaries (Train Bogies & Contact Book)

लिस्ट [ ] और डिक्शनरी { } (ट्रेन के डिब्बे)

Instead of making 10 separate variables for 10 fruits, pack them all into one List [ ] — like compartments (bogies) of a train! Golden Coding Secret: Computers start counting from 0, NOT 1! So the 1st item is at fruits[0], the 2nd item is at fruits[1], and the 3rd item is at fruits[2]! Also meet Dictionary { } — stores pairs like a phone contact book ({"Chintu": 98, "Riya": 99})!

10 फलों के लिए 10 अलग वेरिएबल बनाने की जगह एक List [ ] (चौकोर ब्रैकेट) बना लो — बिल्कुल ट्रेन के डिब्बों की तरह! कोडिंग का सबसे बड़ा रहस्य: कंप्यूटर की गिनती हमेशा 0 से शुरू होती है, 1 से नहीं! यानी पहला फल fruits[0] पर, दूसरा fruits[1] पर, और तीसरा fruits[2] पर मिलेगा! और Dictionary { } फ़ोन की कॉन्टैक्ट बुक की तरह नाम (Key) के आगे वैल्यू जोड़ी रखती है!

In the classroom you can run and edit this code in your browser, get hints and mark the chapter as done. क्लास में यह code ब्राउज़र में चलाकर बदल सकते हो।

Words and symbols in this chapter

[ ] (Square Brackets)
Creates a List holding multiple items separated by commas ,.
चौकोर ब्रैकेट [ ] एक लिस्ट बनाते हैं जिसमें कॉमा , से कई चीज़ें रखते हैं।
fruits[0]
Gets the 1st item (Index 0).
लिस्ट का पहला आइटम निकालता है (इंडेक्स 0)।
fruits[1]
Gets the 2nd item (Index 1).
लिस्ट का दूसरा आइटम निकालता है (इंडेक्स 1)।
{ "name": "Ravi" }
Dictionary — stores Key: Value pairs inside curly braces { }.
डिक्शनरी — { } के अंदर नाम: वैल्यू की जोड़ी सहेजती है।

Ready example

fruits = ["Apple", "Mango", "Banana"]
print("Index 0 (1st):", fruits[0])
print("Index 1 (2nd):", fruits[1])

student = {"naam": "Aarav", "marks": 98}
print("Topper:", student["naam"], "Marks:", student["marks"])

What you will see: 3 connected Train Bogies (Index [0], Index [1], Index [2]) appear with your accessed index glowing bright pink!

प्रिव्यू में ट्रेन के 3 डिब्बे (Index [0], Index [1], Index [2]) दिखेंगे और चुना हुआ इंडेक्स चमक उठेगा!

Line by line

fruits = ["Apple", "Mango", "Banana"]
Creates a 3-bogie train: [0]='Apple', [1]='Mango', [2]='Banana'.
3 डिब्बों वाली ट्रेन बनी: [0]='Apple', [1]='Mango', [2]='Banana'।
print(..., fruits[1])
Fetches bogie [1], which is 'Mango', and highlights it in the preview!
डिब्बा नंबर [1] यानी 'Mango' को निकालता और प्रिव्यू में चमकाता है!
student = {"naam": "Aarav", "marks": 98}
Creates a dictionary contact card with 'naam' and 'marks' keys.
'naam' और 'marks' वाली डिक्शनरी बनाता है।

Common mistake

✗ Wrong

items = ["Pen", "Book"]
print(items[2])

✓ Right

items = ["Pen", "Book"]
print(items[1])

List index starts at 0! In a 2-item list, valid indexes are [0] and [1] — [2] causes IndexError.

लिस्ट की गिनती 0 से शुरू होती है! 2 आइटम वाली लिस्ट में [0] और [1] होते हैं — [2] लिखने पर एरर आएगा।

Your task

Create fruits = ["Apple", "Mango", "Banana"] and print "Mango" using print(fruits[1])!

fruits = ["Apple", "Mango", "Banana"] लिस्ट बनाओ और print(fruits[1]) से "Mango" प्रिंट करो!

  1. Line 1: Write fruits = ["Apple", "Mango", "Banana"].
    लाइन 1: fruits = ["Apple", "Mango", "Banana"] लिखो।
  2. Line 2: Write print(fruits[1]) to pick index 1 ("Mango").
    लाइन 2: print(fruits[1]) लिखो ताकि इंडेक्स 1 ("Mango") छपे।
  3. Click ▶ Run & Preview!
    ▶ Run & Preview दबाकर ट्रेन के डिब्बे देखो!
Starter code for the task
veggies = ["Aloo", "Pyaaz"]
print("Pehli sabzi:", veggies[0])

# TODO 1: fruits naam ki list banao: ["Apple", "Mango", "Banana"]
# TODO 2: print(fruits[1]) se doosra fruit (Mango) dikhao - ginti 0 se shuru hoti hai!

Do this task in the classroom: your code is checked automatically and you get hints if something is missing.

Recap

Quick quiz

If fruits = ["Apple", "Mango", "Banana"], what is fruits[0]?

अगर fruits = ["Apple", "Mango", "Banana"] है, तो fruits[0] क्या देगा?

  1. Mango
  2. Apple
  3. Banana
  4. Error
Show the answer

Answer: B (Apple)

Index [0] is always the very first item in a list ("Apple")!

इंडेक्स [0] हमेशा लिस्ट का सबसे पहला आइटम ("Apple") होता है!

Practice problems for this chapter

Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.