0% found this document useful (0 votes)
7 views3 pages

Python Lists

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

Python Lists

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

PYTHON LISTS

What is a List?
A list is an ordered, mutable (changeable), and indexable collection
of items.
It can contain different data types — strings, numbers, booleans, or
even other lists.
fruits = ["apple", "banana", "cherry"]
numbers = [5, 2, 2,”apple”, 4, 5]

List Characteristics:
Feature Description
Ordered Items maintain the insertion order
Mutable You can change, add, remove items
Heterogeneous Can store different data types
Indexable Access items using index (starting at 0)

Accessing Elements
colors = ["red", "blue", "green"]
print(colors[1:3]) # Output: blue
print(colors[-1]) # Output: green

Modifying Lists

Operation Code Example Purpose


Modify Item colors[1] = "yellow" Change value at
index
Add Item (End) colors.append("purple") Add to end of list
Add Item colors.insert(1, "black") Insert at specific
(Position) index
Remove by Value colors.remove("red") Remove specific
item
Remove by Index del colors[0] Delete item by index
Remove Last colors.pop() Removes last item
Item
Loop Through for item in colors: Traverse all items
List print(item)
Check Item Exists if "red" in colors: Check if item is in
list

Useful List Functions


Method Description Example
append() Add item to end of list fruits.append("orange")
insert() Insert item at specific fruits.insert(1, "kiwi")
position
remove() Remove item by value fruits.remove("banana")
pop() Remove last item fruits.pop()
sort() Sort the list numbers.sort(reverse=”true”)
reverse() Reverse the list numbers.reverse()
len() Returns the number of len(fruits)
items
count() Count how many times an fruits.count("apple")
item appears
extend() Add elements of another fruits.extend(["grape",
list "pear"])

Real-World Examples
Store student marks
marks = [85, 78, 92, 88]
average = sum(marks) / len(marks)
print("Average:", average)

You might also like