Creating Lists
In programming, we often have lots of data to handle. Instead of creating a new variable for every single item, we use a List. Think of a list as a storage shelf where you can keep everything from your favorite snacks to your exam scores!
What Makes a List Special?
A List is a collection that is ordered, changeable, and allows duplicates.
-
Square Brackets: We always create lists using [ ].
-
Commas: We separate each item with a ,.
-
Mix & Match: You can put numbers, words (strings), and even True/False (booleans) all in the same list!
Real-World List Examples
-
Class Roster: Storing every student's name in your grade.
-
Grocery Bag: ["apple", "banana", "mango"]
-
Scoreboard: Saving the points you earned in each level of a game.
How it Looks (Syntax & Examples)
The Pattern:
list_name = [item1, item2, item3]
Example 1: A List of Strings
fruits = ["apple", "banana", "mango"]
print(fruits)
Output: ['apple', 'banana', 'mango']
Example 2: A List of Numbers
numbers = [1, 2, 3, 4, 5]
print(numbers)
Output: [1, 2, 3, 4, 5]
Summary:
-
Duplicates Allowed: You can have
[1, 1, 1]in a list, and Python won't mind! -
Mutable: You can "mutate" or change the items whenever you need.
-
Versatile: Lists can be totally empty
[]when you start and you can add items later.