List and its methods in
python
Miss Laiba Batool
Coding Instructor
List in python
A Python list is like a collection of items, similar to a toy
box where you can store different toys. You can add,
remove, or change the toys in the box.
A list is created by placing all the items (elements) inside
square brackets [ ], separated by commas.
It can have any number of items andthey may be of
different types (integer, float, stringetc.).
List in python
Ordered - Lists are ordered, meaning items have a set order
that doesn't change, and new items are added to the end.
Changeable - The list is changeable, meaning that we can
change, add, and remove items in a list after it has been
created.
Allow duplicates - Since lists are indexed, lists can have
items with the same value.
creating a list
List in python
List lenght - To determine how many items a list has,
use the len() function:
Methods to add item in a list
Append - Adds an item to the end of the list.
Insert - Adds an item at a specific position in the list.
Methods to add item in a list
Extend - To append elements from another list
to the current list
Deleting items from list
Deleting items from list
Deleting items from list
Deleting items from list
Arrangement of list
Arrangement of list
Accessing list item
List items are indexed and you can access them by
referring to the index number:
Accessing list item
Accessing list item
Accessing list item
Negative indexing means start from the end
-1 refers to the last item, -2 refers to the second last item
etc.
Time a Task!!!
1. Create a list with 3 of your favorite dishes.
2. Add 2 more dishes to the list using append.
3. Insert a new sweet dish at the second position using
insert.
4. Remove the last dish from the list using pop.
5. Count how many times your favorite dishes appears in
the list using count.
6. Sort the list in alphabetical order using sort.
7. Reverse the order of the list using reverse.
8. Print the final copied list and the cleared list.
Time a Task!!!
Confusion is part of programming.
― Felienne Hermans