Python Data Types - Detailed Notes
List
A List is a collection data type that is ordered, mutable, and allows duplicate elements.
Syntax: my_list = [1, 2, 3]
Characteristics:
- Ordered: Items have a defined order and maintain insertion order.
- Mutable: You can change, add, and remove items.
- Allows duplicates.
Tuple
A Tuple is a collection which is ordered and immutable. Tuples are faster than lists and are
often used for fixed data.
Syntax: my_tuple = (1, 2, 3)
Characteristics:
- Ordered: Items maintain insertion order.
- Immutable: Cannot change after creation.
- Allows duplicates.
- Can be used as dictionary keys (if they contain immutable elements).
Set
A Set is an unordered collection of unique elements.
Syntax: my_set = {1, 2, 3}
Dictionary
A Dictionary is an unordered, mutable collection that maps keys to values.
Syntax: my_dict = {"name": "Alice", "age": 25}
Characteristics:
- Unordered (as of Python 3.6+, maintains insertion order).
- Keys must be immutable (e.g., strings, numbers, tuples).
- Values can be any type.
- No duplicate keys.