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!