Compare and contrast lists, dictionaries, tuples, and sets in Python. Provide examples of their usage.
In Python, lists, dictionaries, tuples, and sets are fundamental data structures that serve different purposes and have distinct characteristics. Let's compare and contrast these data structures, and provide examples of their usage:
1. Lists:
* Lists are ordered collections of items, enclosed in square brackets ([]).
* Lists allow duplicates and can store elements of different data types.
* Elements in a list are accessed by their index, starting from 0.
* Lists are mutable, meaning you can modify, add, or remove elements after creation.
Example:
```
python`fruits = ['apple', 'banana', 'orange']
print(fruits[0]) # Output: 'apple'
fruits.append('grape')
print(fruits) # Output: ['apple', 'banana', 'orange', 'grape']`
```
2. Dictionaries:
* Dictionaries are unordered collections of key-value pairs, enclosed in curly braces ({}) or created using the `dict()` constructor.
* Each element in a dictionary consists of a unique key and its corresponding value.
* Dictionaries allow fast lookup of values based on their keys.
* Dictionaries are mutable.
Example:
```
python`student = {'name': 'John', 'age': 20, 'major': 'Computer Science'}
print(student['age']) # Output: 20
student['age'] = 21
print(student) # Output: {'name': 'John', 'age': 21, 'major': 'Computer Science'}`
```
3. Tuples:
* Tuples are ordered collections of elements, enclosed in parentheses ().
* Tuples can store elements of different data types.
* Tuples are immutable, meaning they cannot be modified once created.
* Tuples are commonly used for representing related pieces of data that should not be changed.
Example:
```
python`point = (3, 4)
print(point[0]) # Output: 3
# point[0] = 5 # Raises an error as tuples are immutable`
```
4. Sets:
* Sets are unordered collections of unique elements, enclosed in curly braces ({}) or created using the `set()` constructor.
* Sets do not allow duplicate values.
* Sets are useful for performing mathematical set operations like union, intersection, and difference.
* Elements in a set are not accessed by their index.
Example:
```
python`fruits = {'apple', 'banana', 'orange'}
fruits.add('grape')
print(fruits) # Output: {'orange', 'apple', 'banana', 'grape'}`
```
Here's a summary of the differences between these data structures:
* Lists are ordered and mutable, while dictionaries and sets are unordered. Tuples are ordered but immutable.
* Lists and dictionaries allow duplicates, while sets and tuples do not.
* Lists and dictionaries are accessed by index/key, while sets do not support indexing, and tuples are accessed by index.
* Lists, dictionaries, and sets can store elements of different data types, while tuples can as well.
* Lists and dictionaries have a variety of built-in methods for manipulation, while sets and tuples have more limited methods.
The choice of which data structure to use depends on the specific requirements of your program. Lists are commonly used for storing collections of items, dictionaries for mapping keys to values, tuples for representing unchangeable data, and sets for handling unique elements and set operations.