Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Python_Data_Structures

The document provides an overview of three key Python data structures: dictionaries, tuples, and sets. It defines each structure, highlights their features, and presents common operations along with examples. Additionally, it discusses their use cases and key differences, emphasizing the importance of selecting the appropriate data structure for efficient programming.

Uploaded by

mayurhanj98
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Python_Data_Structures

The document provides an overview of three key Python data structures: dictionaries, tuples, and sets. It defines each structure, highlights their features, and presents common operations along with examples. Additionally, it discusses their use cases and key differences, emphasizing the importance of selecting the appropriate data structure for efficient programming.

Uploaded by

mayurhanj98
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Data Structures:

Dictionary, Tuple, and Set


Introduction
• What are Data Structures?
• - Organize and store data efficiently.
• - Python provides several built-in data
structures.

• Why Focus on Dictionaries, Tuples, and Sets?


• - Widely used for efficient programming.
• - Serve different purposes in data handling.
Dictionary
• Definition
• - An unordered collection of key-value pairs.
• - Syntax: {key: value}

• Features
• - Keys are unique and immutable.
• - Values can be any data type.

• Example
• student = {"name": "Alice", "age": 21, "grade": "A"}

• Common Operations
• - Access: student["name"]
• - Add: student["major"] = "Math"
• - Delete: del student["grade"]
Tuple
• Definition
• - An ordered, immutable collection of items.
• - Syntax: (item1, item2, ...)

• Features
• - Immutable: Cannot modify after creation.
• - Supports indexing and slicing.

• Example
• coordinates = (10, 20, 30)

• Common Operations
• - Access: coordinates[0]
• - Length: len(coordinates)
• - Count: coordinates.count(20)
Set
• Definition
• - An unordered collection of unique items.
• - Syntax: {item1, item2, ...}

• Features
• - No duplicate elements.
• - Supports mathematical set operations (union, intersection).

• Example
• fruits = {"apple", "banana", "cherry"}

• Common Operations
• - Add: fruits.add("orange")
• - Remove: fruits.remove("banana")
• - Union: fruits.union({"grape"})
Key Differences
• Dictionary vs Tuple vs Set
• | Feature | Dictionary | Tuple | Set |
• |----------------|-----------------------|---------------|-------------|
• | Order | Unordered | Ordered | Unordered |
• | Mutability | Mutable keys/values | Immutable | Mutable |
• | Duplicates | Keys must be unique | Allows | No |
Use Cases
• Dictionary
• - Storing structured data (e.g., student records).

• Tuple
• - Fixed data (e.g., geographical coordinates).

• Set
• - Membership tests, removing duplicates, and set
operations.
Code Comparison
• Dictionary Example
• employee = {"id": 1, "name": "John"}
• print(employee["name"])

• Tuple Example
• colors = ("red", "green", "blue")
• print(colors[1])

• Set Example
• numbers = {1, 2, 3, 4}
• numbers.add(5)
• print(numbers)
Summary
• Dictionary
• - Key-value pairs, mutable.

• Tuple
• - Immutable, ordered.

• Set
• - Unique, unordered elements.

• Choosing the right data structure enhances code


efficiency.

You might also like