🌜Lists and Tuples
Lists and Tuples in Python 📚🔄
Lists: Dynamic and Versatile 🚀
A list in Python is a mutable, ordered sequence of elements. Each element can be of any data type, and you can modify, add, or remove items from a list. Let's explore the essentials of lists:
Creating a List:
In this example, fruits
is a list containing three string elements.
Accessing Elements:
Lists are zero-indexed, so the first element is at index 0.
Modifying Elements:
This updates the second element in the list to "grape".
Adding Elements:
The append()
method adds "kiwi" to the end of the list.
Removing Elements:
This removes the element "apple" from the list.
Tuples: Immutable and Ordered 🔄🧊
A tuple is similar to a list, but it's immutable, meaning you cannot modify its elements once it's created. Tuples are commonly used for fixed collections of items.
Creating a Tuple:
Here, colors
is a tuple with three string elements.
Accessing Elements:
Just like lists, tuples are zero-indexed.
Immutable Nature:
Attempting to modify a tuple will result in an error.
Use Cases:
Lists: When you need a collection that can be modified, extended, or reduced.
Tuples: When you want to create a fixed collection of items that shouldn't change during the program's execution.
Last updated