PYTHON Tutorial

Data Structures

Introduction

Data structures are fundamental to programming, providing efficient ways to organize and manage data. Python offers various data structures tailored to specific use cases.

Practical Steps

  • Choose the Right Structure: Identify the type of data you're working with and select the appropriate structure.
  • Create Structures: Use built-in functions (e.g., list(), tuple(), dict(), set()) to create instances of data structures.
  • Operate on Structures: Perform operations like adding, removing, and accessing elements using standard methods.
  • Access Specific Data: Use indexing (e.g., []) or key lookup (e.g., get()) to retrieve data from structures.

Key Concepts

  • Python Lists: Ordered collections of elements that can be modified (mutable).
  • Python Tuples: Immutable ordered collections of elements that cannot be changed.
  • Python Dictionaries: Collections of key-value pairs that map unique keys to values.
  • Python Sets: Unordered collections of unique elements that have no duplicates.

Simple Python Example

# Create data structures
my_list = ['apple', 'banana', 'cherry']
my_tuple = ('apple', 'banana', 'cherry')
my_dict = {'apple': 'red', 'banana': 'yellow', 'cherry': 'red'}
my_set = {'apple', 'banana', 'cherry'}

# Operate on structures
my_list.append('mango')
my_dict['orange'] = 'orange'

# Access data
print(my_list[2])  # Output: 'cherry'
print(my_dict['banana'])  # Output: 'yellow'