List
Introduction
In Python, a List is a collection that allows us to store multiple items in a single variable. Lists are one of the most commonly used data types because they are flexible, ordered, and mutable.
Example:
Features of Lists
-
Ordered: Items have a defined order.
-
Mutable: We can change items after creating a list.
-
Allow duplicates: Lists can store repeated values.
-
Store multiple data types: A single list can hold strings, numbers, and even other lists.
Example:
Creating a List
Accessing List Items
We can access items using indexing and slicing.
Modifying a List
Adding Items
Removing Items
List Functions and Methods
Some common ones are:
-
len(list)→ length of list -
min(list),max(list)→ smallest/largest element -
sum(list)→ sum of numeric values -
list.sort()→ sort list -
list.reverse()→ reverse list
Example:
Looping Through a List
List Comprehension
A short way to create lists.
Nested Lists
Lists inside another list.
Conclusion
Python Lists are powerful and versatile. They allow easy data storage, modification, and access. Understanding lists is essential for mastering Python programming.
Comments
Post a Comment