Mastering Python Lists: A Guide to Unleashing the Power of Python
One of the fundamental building blocks in Python is the List. Lists are versatile, mutable, and can hold a variety of data types. They are the Python equivalent of arrays, but with superpowers — they can grow and shrink dynamically and can contain elements of different types. Let’s dive in!
What is a List in Python?
In Python, a list is a collection of items that are ordered and changeable. Lists allow duplicate members and are one of the four built-in data types in Python used to store collections of data. Here’s how you can create a list:
my_list = ['apple', 'banana', 'cherry']
print(my_list)
When you run this code, it will output: [‘apple’, ‘banana’, ‘cherry’]
Accessing Elements in a List
Each item in a list corresponds to an index number, which is an integer value, starting with the index number 0.
To access an element in a list, you refer to its index number.
my_list = ['apple', 'banana', 'cherry']
print(my_list[1])