[go: up one dir, main page]

0% found this document useful (0 votes)
40 views4 pages

Class 11 Computer Science Lists

This document provides an overview of Python lists, including their creation, common functions, and examples for Class 11 students. It covers list operations such as appending, extending, slicing, and iterating, as well as nested lists and list comprehension. Additionally, it includes exercises to reinforce understanding of the topic.

Uploaded by

bagoulibhawana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

Class 11 Computer Science Lists

This document provides an overview of Python lists, including their creation, common functions, and examples for Class 11 students. It covers list operations such as appending, extending, slicing, and iterating, as well as nested lists and list comprehension. Additionally, it includes exercises to reinforce understanding of the topic.

Uploaded by

bagoulibhawana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Computer Science Class 11 - Notes on Lists

Lists are one of the most versatile data structures in Python. They allow storage of multiple items in

a single variable and

are mutable, meaning their elements can be changed.

This document provides an overview of Python lists, their functions, and some common examples to

help Class 11 students understand the topic better.

1. Creating Lists

Lists are defined using square brackets []. For example:

my_list = [1, 2, 3, 4, 5]

Lists can store data of different types:

mixed_list = [1, "Hello", 3.14, True]

2. Common List Functions

Here are some common list functions:

1. append(): Adds an item to the end of the list.

Example: my_list.append(6)

2. extend(): Adds multiple elements to the end of the list.

Example: my_list.extend([7, 8, 9])


3. insert(): Inserts an item at a specific index.

Example: my_list.insert(2, "NewItem")

4. pop(): Removes and returns the item at a specific index.

Example: my_list.pop(2)

5. remove(): Removes the first occurrence of a specific value.

Example: my_list.remove(3)

6. sort(): Sorts the list in ascending order.

Example: my_list.sort()

7. reverse(): Reverses the order of elements in the list.

Example: my_list.reverse()

8. index(): Returns the index of the first occurrence of a value.

Example: my_list.index(3)

9. count(): Counts the number of occurrences of a value in the list.

Example: my_list.count(2)

10. len(): Returns the number of elements in the list.

Example: len(my_list)

3. Slicing and Iterating

Slicing allows access to a subset of a list using the syntax list[start:end:step].


Example:

my_list = [0, 1, 2, 3, 4, 5]

print(my_list[1:4]) # Output: [1, 2, 3]

Iterating through lists:

for item in my_list:

print(item)

4. Nested Lists

Lists can contain other lists as elements, forming nested lists.

Example:

nested_list = [[1, 2, 3], ["a", "b", "c"], [True, False]]

Accessing elements in nested lists:

print(nested_list[0][1]) # Output: 2

5. List Comprehension

List comprehension provides a concise way to create lists.

Example:

squares = [x**2 for x in range(10)]

print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

6. Examples and Exercises


1. Create a list of the first 10 natural numbers.

2. Write a program to find the maximum and minimum values in a list.

3. Use list comprehension to create a list of even numbers from 1 to 20.

4. Write a program to find the sum of all elements in a list.

End of Document

You might also like