Part 5: Lists, Tuples, and Dictionaries โ€” Mastering Python Data Structures (with AI Practice Tools)

Python gives you powerful ways to store and organize data. In this post, youโ€™ll learn how to work with three of the most important data structures:

  • Lists
  • Tuples
  • Dictionaries

You’ll also build a simple contact book and learn how to use AI tools like ChatGPT to generate and explain real-world data examples.


๐Ÿ“‹ Lists โ€” Ordered, Mutable Collections

A list is a collection of items in a specific order. You can change them, add to them, and remove items.

fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
fruits.append("orange") # Add to the list
print(fruits)

Useful list functions:

len(fruits)
fruits.remove("banana")
fruits.sort()

๐Ÿ”’ Tuples โ€” Ordered, Immutable Collections

Tuples are like lists, but you canโ€™t change them after theyโ€™re created.

colors = ("red", "green", "blue")
print(colors[1]) # Output: green

Use a tuple when your data shouldnโ€™t change.


๐Ÿ”‘ Dictionaries โ€” Key/Value Pairs

Dictionaries store data using keys and values โ€” perfect for things like user info, configuration settings, or contact lists.

person = {
"name": "Alice",
"age": 30,
"is_student": False
}
print(person["name"]) # Output: Alice

Add or update data:

person["email"] = "alice@example.com"

Loop through it:

for key, value in person.items():
print(key, ":", value)

๐Ÿค– AI Tip: Use ChatGPT to Simulate Data

Ask:

โ€œGenerate a Python dictionary of 5 fake users with name, age, and email.โ€

Youโ€™ll get a quick dataset to practice with โ€” great for building projects and testing logic!


๐Ÿ›  Mini Project: Contact Book (CLI)

Hereโ€™s a small project you can build and expand:

contacts = {}

while True:
name = input("Enter contact name (or 'q' to quit): ")
if name == 'q':
break
phone = input("Enter phone number: ")
contacts[name] = phone

print("\nYour Contacts:")
for name, phone in contacts.items():
print(f"{name}: {phone}")

Bonus: Ask ChatGPT how to save this to a file or add edit/delete features!


๐ŸŽฏ Practice Challenge

Create a program that:

  • Uses a list to store 5 student names
  • Uses a dictionary to assign each student a random grade (0โ€“100)
  • Prints each student and their grade
  • Calculates and prints the class average

Ask ChatGPT: โ€œHow can I sort this dictionary by grade?โ€


๐Ÿ“š Free Learning Resources


๐Ÿ Coming Up in Part 6โ€ฆ

Next time, weโ€™ll unlock the power of functions and modules โ€” so you can organize your code like a pro and reuse it across projects. We’ll also explore how AI can help write cleaner, more modular Python code.

Share the Post:

Related Posts

Join the Codexa Crew โ€“ Itโ€™s free!

Stay Updated with the Latest in Tech & Automation

Be the first to know when new content drops on Green Codexa!