Chapter5 Python
Introduction to Lists in Python
A list is a collection of items in Python that is ordered, changeable, and allows duplicate
elements.
Key Points:
o Lists are used to store multiple items in a single variable.
o They can hold different data types (e.g., numbers, strings, etc.).
Example:
my_list = [1, "hello", 3.14]
Features of Lists
Ordered: Elements have a specific position and order.
Mutable: You can add, remove, or change items.
Allows Duplicates: Same value can appear multiple times.
Example
fruits = ["apple", "banana", "apple"]
Creating a List
Syntax:
Use square brackets [ ] to define a list. Items are separated by commas.
empty_list = []
numbers = [10, 20, 30]
mixed = [1, "apple", 3.5]
Accessing List Items
By Index: Use the position of the item (starts at 0).
Negative Indexing: Start from the end using -1.
my_list = ["hello",56 ,59 60, “python”, 96.2]
print (my_list[0]) # Output: hello
print (my_list[-1]) # Output: 96.2
Modifying Lists
Changing Values: Assign a new value using the index.
Adding Items: Use .append() or .insert()or .extend().
Removing Items: Use .remove(), .pop(), or del.
Task Sample Code Output
Using append() List = [ ] Initial blank List: [ ]
method print("Initial blank List: ")
print(List)
# Addition of Elements
List.append(1) [1, 2, 4]
List.append(2)
List.append(4)
print(List)
[1, 2, 4,['Good', 'Morning']]
List2 =['Good’, 'Morning']
List.append(List2)
print(List)
Using insert() # Creating a List Initial List:[1, 2, 3, 4]
method List = [1,2,3,4]
print("InitialList: ")
print(List)
# Addition of Element at
# specific Position [ 1, 2, 3, 12, 4]
List.insert(3, 12)
print( List)
[‘Kabir’, 1,2,3,12,4]
List.insert(0, 'Kabir')
print(List)
Using extend() List=[1, 22,2,3,4] [1,22,2,3,4,9, “Artificial Intelligence”]
method List.extend[9, ‘Artificial Intelligence”]
print(List)
.append () : Elements can be added to the List by using built-in append() function. Only one
element at a time can be added to the list by using append() method.
insert() Method :append() method only works for addition of elements at the end of the List, for
addition of element at the desired position, insert() method is used.
extend() method: Other than append() and insert() methods, there's one more method for
Addition of elements, extend(), this method is used to add multiple elements at the same time at
the end of the list.
.remove ():removes one element at a time.
.pop() : used to remove and return an element from the set, but by default it removes only the last
element of the set, to remove an element from a specific position of the List, index of the element
is passed as an argument to the pop() method.
List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12]
List.remove(5)
print[List]
Output:
[1, 2, 3, 4, 6, 7, 8, 9, 10, 11,12]
List1=[12,13,14,15,16,17]
List1.pop(5)
print[List]
Output:
[12,13,14,15,16]
Common List Methods
.append(item): Add item to the end.
.insert(index, item): Add item at specific index.
.remove(item): Remove the first occurrence.
.pop(index): Remove item by index.
.clear(): Remove all items.
.sort(): Sort the list (ascending by default).
.reverse(): Reverse the list.
Looping Through a List
For Loop:
fruits = ["apple", "banana", "cherry"]
for list in fruits:
print(list)
While Loop
i=0
while i < len(fruits):
print(fruits[i])
i += 1
TRY BY YOURSELF
1) Create a list in Python of children selected for science quiz with following names- Arjun,
Sonakshi, Vikram, Sandhya, Sonal, Isha, Kartik
○ Print the whole list
○ Delete the name “Vikram” from the list
○ Add the name “Jay” at the end
○ Remove the item which is at the second position.