Creating Lists
Lists in Python are one of the most important and commonly used data structures in Python programming.
A list is used to store multiple values together in a single variable.
Lists help programmers organize, manage, and process collections of data easily.
What is a List in Python
A list is a collection of items stored together inside square brackets.
Lists can store different types of data such as:
- numbers
- text
- decimal values
- mixed data
Why Lists are Important
Lists are important because they help programmers:
- store multiple values together
- organize data efficiently
- process collections easily
- reduce the need for multiple variables
- perform data operations quickly
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
| Real-Life Example | Stored as List |
|---|---|
| Student Names | ["Rahul", "Sneha", "Akhil"] |
| Marks | [90, 80, 70] |
| Shopping Items | ["Milk", "Rice", "Soap"] |
| Mobile Contacts | ["Ravi", "Anu", "David"] |
| Game Scores | [100, 250, 400] |
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]
Lists are Mutable
The word:
Mutable
means:
List values can be changed
Example
marks = [10, 20, 30, 40]
marks[0] = 99
print(marks)
Output:
[99, 20, 30, 40]
Advantages of Lists
- Store multiple values together
- Easy to modify
- Supports loops
- Allows duplicate values
- Supports mixed data types
Common Beginner Mistakes
| Mistake | Problem |
|---|---|
| Using parentheses instead of brackets | Wrong data type |
| Forgetting commas | Syntax error |
| Wrong indexing | Index error |
| Confusing list with string | Incorrect output |
Summary:
A list in Python is an ordered and mutable collection used to store multiple values together.
Important concepts include:
- list syntax
- indexing
- mutable nature
- nested lists
- loops with lists
- len() function
Lists are widely used in:
- school management systems
- gaming applications
- banking software
- e-commerce websites