Python Lists (Part 1) | Introduction, Indexing, List Operations & Traversing | CBSE Class 11 Computer Science
Class 11 · Computer Science
Python Lists (Part 1)
A list is one of the most versatile and widely used data structures in Python. A list is an ordered collection of items that can store elements of different data types such as integers, floating-point numbers, strings, Boolean values, and even other lists. Unlike strings, lists are mutable, which means their elements can be modified after the list is created.
Learning Objectives
- Understand the concept of lists.
- Create and initialize lists.
- Access elements using positive and negative indexing.
- Perform basic list operations.
- Traverse a list using loops.
What is a List?
A list is an ordered collection of elements enclosed within square brackets []. Each element in a list is separated by a comma.
Examples
numbers=[10,20,30,40]
names=["Aman","Riya","Rahul"]
mixed=[101,"Python",95.5,True]
Characteristics of Lists
- Lists are ordered collections.
- Lists are mutable (modifiable).
- Lists can store duplicate values.
- Lists can contain different data types.
- Each element has an index.
- Lists can contain other lists (Nested Lists).
Creating Lists
List of Integers
marks=[85,90,78,92]
List of Strings
cities=["Jaipur","Delhi","Mumbai"]
Mixed List
student=[101,"Rahul",91.5,True]
Empty List
data=[]
List Indexing
Every element in a list has an index. Python supports both positive and negative indexing.
subjects=["English","Physics","Chemistry","Mathematics","Computer"]
| Element | English | Physics | Chemistry | Mathematics | Computer |
|---|---|---|---|---|---|
| Positive Index | 0 | 1 | 2 | 3 | 4 |
| Negative Index | -5 | -4 | -3 | -2 | -1 |
Accessing List Elements
subjects=["English","Physics","Chemistry","Mathematics","Computer"]
print(subjects[0])
print(subjects[2])
print(subjects[-1])
Output
English
Chemistry
Computer
Updating List Elements
Since lists are mutable, elements can be modified.
marks=[85,90,78]
marks[1]=95
print(marks)
Output
[85,95,78]
List Operations
1. Concatenation (+)
The + operator joins two lists.
list1=[1,2,3]
list2=[4,5]
print(list1+list2)
Output
[1,2,3,4,5]
2. Repetition (*)
The * operator repeats a list.
numbers=[1,2]
print(numbers*3)
Output
[1,2,1,2,1,2]
3. Membership Operators
The in and not in operators check whether an element exists in a list.
numbers=[10,20,30]
print(20 in numbers)
print(50 in numbers)
print(50 not in numbers)
Output
True
False
True
4. List Slicing
Slicing extracts a portion of a list.
Syntax
list[start:stop]
Example 1
numbers=[10,20,30,40,50]
print(numbers[1:4])
Output
[20,30,40]
Example 2
print(numbers[:3])
Output
[10,20,30]
Example 3
print(numbers[2:])
Output
[30,40,50]
Example 4
print(numbers[::-1])
Output
[50,40,30,20,10]
Traversing a List Using for Loop
colors=["Red","Blue","Green"]
for color in colors:
print(color)
Output
Red
Blue
Green
Traversing a List Using while Loop
colors=["Red","Blue","Green"]
i=0
while i<len(colors):
print(colors[i])
i=i+1
Output
Red
Blue
Green
Real-Life Applications of Lists
- Storing marks of students.
- Maintaining attendance records.
- Managing product inventories.
- Creating shopping carts.
- Storing employee details.
- Managing examination results.
- Processing survey responses.
Common Programming Mistakes
1. Accessing an Invalid Index
numbers=[10,20,30]
print(numbers[5])
Result: IndexError
2. Forgetting Square Brackets
numbers=10,20,30
Explanation: This creates a tuple-like sequence instead of a list.
3. Assuming Slicing Includes the Stop Index
numbers=[10,20,30,40,50]
print(numbers[1:4])
Output
[20,30,40]
The element at index 4 is not included.
CBSE Important Programs
- Create a list of five numbers and display it.
- Print all elements using a
forloop. - Print all elements using a
whileloop. - Display the first and last element of a list.
- Reverse a list using slicing.
- Concatenate two lists.
- Repeat a list using the
*operator. - Check whether an element exists in a list.
- Extract a sub-list using slicing.
- Update an element in a list.
Viva Questions
- What is a list?
- Why are lists called mutable?
- Can a list contain different data types?
- What is list indexing?
- Differentiate between positive and negative indexing.
- How can you traverse a list?
- What is list slicing?
- Which operators are used for concatenation and repetition?
- What are membership operators?
- Give two real-life applications of lists.
Exam Tips
- Remember that lists are mutable.
- Indexing starts from 0.
- Negative indexing starts from -1.
- The stop index in slicing is excluded.
- Practice traversing lists using both
forandwhileloops.
Quick Revision
- A list is an ordered, mutable collection.
- Lists are enclosed within square brackets
[]. - Lists can store duplicate values and different data types.
- Use indexing to access elements.
- Use slicing to extract a portion of a list.
- Traverse lists using
fororwhileloops. - Lists support concatenation, repetition, and membership operations.