Student Login
Python Programming

Data Structures

Introduction

  • Now that you understand Python basics, let's learn how to organize and store multiple pieces of data efficiently.
  • Data structures are containers that help you manage related information together.

Lists: Ordered Collections

  • Lists store multiple items in a specific order. You can change, add, or remove items.
  • “`python
  • fruits = ['apple', 'banana', 'mango']
  • print(fruits[0]) # Prints 'apple' (first item)
  • fruits.append('orange') # Add item
  • fruits.remove('banana') # Remove item
  • print(len(fruits)) # Number of items
  • “`

Dictionaries: Key-Value Pairs

  • Dictionaries store data as pairs (like a real dictionary: word → meaning).
  • “`python
  • student = {
  • 'name': 'Ali',
  • 'age': 20,
  • 'grade': 'A'
  • }
  • print(student['name']) # Prints 'Ali'
  • student['city'] = 'Lahore' # Add new key
  • “`
  • Real Use Case: Store user information, product details, configuration settings.

Tuples: Unchangeable Lists

  • Tuples are like lists but cannot be changed after creation.
  • “`python
  • coordinates = (23.5, 67.8)
  • rgb_color = (255, 128, 0)
  • “`
  • Use tuples for data that should never change (e.g., coordinates, dates).

Real Example: Student Management

  • “`python
  • # List of dictionaries
  • students = [
  • {'name': 'Ahmed', 'marks': 85},
  • {'name': 'Sara', 'marks': 92},
  • {'name': 'Ali', 'marks': 78}
  • ]
  • # Find student with highest marks
  • top_student = students[0]
  • for student in students:
  • if student['marks'] > top_student['marks']:
  • top_student = student
  • print(f"Top student: {top_student['name']} with {top_student['marks']} marks")
  • “`
⬅️ Previous Lesson Your First Python Program Next Lesson ➡️ Functions and Modules
Scroll to Top