Welcome to another amazing course on programming with Python.
Today, we will delve into one of Python's key features, which plays a fundamental role in most applications. The day's agenda revolves around data structures, specifically lists
. So, where are we headed on this journey? Let's find out!
A list in Python is an ordered collection of items. It's akin to packing your travel bag with different items you'll need on your journey. For our travel-themed journey today, we'll compile a list of travel destinations. These lists are incredibly versatile; they can contain any number of items and may accommodate different data types like integers, floats, strings, etc.
Consider this list of travel destinations to explore.
This list contains five elements — all of which are strings. Notice the syntax here - we essentially put commas to separate items on our list and then we put square brackets ([
to open and ]
to close) around the whole list.
Accessing elements from the list is akin to picking out a travel destination from your list. Python uses a zero-indexing system, meaning the count starts from zero. Let's learn how to access different elements from our list.
To reach an element from the list, we use its index number. For instance, the first element in our list is "Paris". Here's how you can access it:
