Non-primitive data types in Python are more complex structures that can store multiple values or a combination of different types of data. They are derived from primitive data types and provide more functionality for handling collections and more complex data structures. Let’s explore each non-primitive data type in detail:
List #
Description:
- An ordered collection of items.
- Items in a list can be of different types.
- Lists are mutable, meaning their elements can be changed after the list is created.
Example:
codefruits = ["apple", "banana", "cherry"]
print(fruits) # Output: ['apple', 'banana', 'cherry']
# Accessing elements
print(fruits[0]) # Output: apple
# Modifying elements
fruits[1] = "blueberry"
print(fruits) # Output: ['apple', 'blueberry', 'cherry']
# Adding elements
fruits.append("date")
print(fruits) # Output: ['apple', 'blueberry', 'cherry', 'date']
# Removing elements
fruits.remove("apple")
print(fruits) # Output: ['blueberry', 'cherry', 'date']
Tuple #
Description:
- An ordered collection of items, similar to a list.
- Tuples are immutable, meaning once they are created, their elements cannot be changed.
- Often used for fixed collections of items.
Example:
codecoordinates = (10.0, 20.0)
print(coordinates) # Output: (10.0, 20.0)
# Accessing elements
print(coordinates[1]) # Output: 20.0
# Tuples cannot be modified
# coordinates[1] = 25.0 # This will raise a TypeError
Set #
Description:
- An unordered collection of unique items.
- Sets are mutable, but they do not allow duplicate elements.
- Useful for membership testing and eliminating duplicate entries.
Example:
codeunique_numbers = {1, 2, 3, 4, 5}
print(unique_numbers) # Output: {1, 2, 3, 4, 5}
# Adding elements
unique_numbers.add(6)
print(unique_numbers) # Output: {1, 2, 3, 4, 5, 6}
# Removing elements
unique_numbers.remove(3)
print(unique_numbers) # Output: {1, 2, 4, 5, 6}
# Sets do not allow duplicates
unique_numbers.add(1)
print(unique_numbers) # Output: {1, 2, 4, 5, 6}
Dictionary #
Description:
- A collection of key-value pairs.
- Each key is unique and maps to a value.
- Dictionaries are mutable, allowing for dynamic changes in keys and values.
Example:
codestudent = {"name": "Alice", "age": 22, "is_student": True}
print(student) # Output: {'name': 'Alice', 'age': 22, 'is_student': True}
# Accessing values
print(student["name"]) # Output: Alice
# Modifying values
student["age"] = 23
print(student) # Output: {'name': 'Alice', 'age': 23, 'is_student': True}
# Adding new key-value pairs
student["major"] = "Computer Science"
print(student) # Output: {'name': 'Alice', 'age': 23, 'is_student': True, 'major': 'Computer Science'}
# Removing key-value pairs
del student["is_student"]
print(student) # Output: {'name': 'Alice', 'age': 23, 'major': 'Computer Science'}
Summary #
Non-primitive data types in Python include:
- List: An ordered, mutable collection of items.
- Tuple: An ordered, immutable collection of items.
- Set: An unordered collection of unique items.
- Dictionary: A collection of key-value pairs.
These data types provide powerful ways to handle and organize data, offering flexibility and efficiency in various programming tasks.