What is the purpose of lists in Python?

What is the purpose of lists in Python?

a) To store ordered collections of items
b) To store unique unordered elements
c) To store key-value pairs
d) To perform arithmetic operations

Answer:

a) To store ordered collections of items

Explanation:

Lists in Python are used to store ordered collections of items. Each item in a list can be of any data type, and lists can even contain other lists. The items in a list are stored in a specific order, and you can access, modify, or iterate over them using their index positions.

# Example of a list
fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # Output: apple

# Modifying a list item
fruits[1] = "blueberry"
print(fruits)  # Output: ['apple', 'blueberry', 'cherry']

In this example, the list fruits contains three elements. Lists are versatile data structures that allow you to perform various operations like adding, removing, and modifying items.

Lists are one of the most commonly used data structures in Python because they are flexible, easy to use, and can store a collection of items in a sequence.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top