1. What are Lists? The Basics
A list is an ordered, mutable (changeable) collection of items. Key characteristics:
- Ordered: Items have a defined order, and that order will not change. You can access items by their position (index).
- Mutable: Unlike strings, lists can be changed after they are created. You can add, remove, or modify elements.
- Allows Duplicates: You can have multiple items with the same value.
- Can Hold Mixed Data Types: A single list can contain integers, strings, floats, Booleans, and even other lists!
Syntax: Lists are created using square brackets `[]`, with items separated by commas.
# An empty list
empty_list = []
# List of integers
numbers = [1, 2, 3, 4, 5]
# List of strings (UdaanPath courses)
courses = ["Python Basics", "Web Dev", "Data Science", "Python Basics"] # Duplicates allowed
# List with mixed data types
mixed_data = ["Alice", 25, True, 175.5]
print(f"Empty list: {empty_list}")
print(f"Numbers: {numbers}")
print(f"Courses: {courses}")
print(f"Mixed data: {mixed_data}")
print(f"Type of 'courses': {type(courses)}")
Empty list: [] Numbers: [1, 2, 3, 4, 5] Courses: ['Python Basics', 'Web Dev', 'Data Science', 'Python Basics'] Mixed data: ['Alice', 25, True, 175.5] Type of 'courses': <class 'list'>