🌜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:

fruits = ["apple", "banana", "orange"]

In this example, fruits is a list containing three string elements.

Accessing Elements:

print(fruits[0])  # Output: "apple"

Lists are zero-indexed, so the first element is at index 0.

Modifying Elements:

fruits[1] = "grape"

This updates the second element in the list to "grape".

Adding Elements:

fruits.append("kiwi")

The append() method adds "kiwi" to the end of the list.

Removing Elements:

fruits.remove("apple")

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:

colors = ("red", "green", "blue")

Here, colors is a tuple with three string elements.

Accessing Elements:

print(colors[1])  # Output: "green"

Just like lists, tuples are zero-indexed.

Immutable Nature:

colors[1] = "yellow"  # Raises an error

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