Advanced Data Types in Python

Python provides several built-in data types that allow for complex data management and manipulation. Four of the most important advanced data types are tuple, list, set, and dictionary. Each of these data types has unique properties and use cases.

graph TD A[ Advance Data Types] --> B[List] A --> C[Tuple] A --> D[Dictionary] A --> E[Set]

1. Tuple

A tuple is an ordered collection of items that is immutable, meaning its elements cannot be changed after creation. Tuples can contain mixed data types and are defined using parentheses.

Key Points on Tuples:

Code Example of Tuple:

Code Example

coordinates = (10, 20, 30)
        print("X:", coordinates[0])
        print("Y:", coordinates[1])
        print("Z:", coordinates[2])

Output

X: 10
Y: 20
Z: 30

2. List

A list is an ordered and mutable collection of items. Lists can hold items of different data types and can be modified after creation, making them versatile for various tasks.

Key Points on Lists:

Code Example of List:

Code Example

fruits = ["apple", "banana", "cherry"]
        fruits.append("orange")  # Adding an element
        print(fruits)

Output

['apple', 'banana', 'cherry', 'orange']

3. Set

A set is an unordered collection of unique items. Sets are mutable, but they do not allow duplicate elements, making them ideal for membership testing and eliminating duplicates from a collection.

Key Points on Sets:

Code Example of Set:

Code Example

numbers = {1, 2, 3, 4, 4, 5}  # Duplicate will be ignored
        print(numbers)

Output

{1, 2, 3, 4, 5}

4. Dictionary

A dictionary is an unordered collection of key-value pairs. Each key is unique, and values can be accessed using their corresponding keys. Dictionaries are mutable and are defined using curly braces.

Key Points on Dictionaries:

Code Example of Dictionary:

Code Example

student = {"name": "John", "age": 21, "major": "Computer Science"}
        print(student["name"])  # Accessing value by key

Output

John

Conclusion

Understanding and utilizing advanced data types such as tuples, lists, sets, and dictionaries is essential for effective programming in Python. Each type serves its unique purpose, allowing for the efficient management of collections of data.