Lists in Python
In the previous lesson, we learned about Functions and how they help organize and reuse code efficiently. Today, we will learn about Lists, one of the most commonly used data structures in Python.
Lists are important because they allow programmers to store multiple values in a single variable.
What is a List?
A List is a collection of multiple items stored in a single variable.
Lists are:
Ordered
Changeable (Mutable)
Allow duplicate values
Lists are created using square brackets "[]".
Example
languages = [ " Python " , " Java " , " C++ " ]
Enter fullscreen mode
Exit fullscreen mode
Accessing List Items
Each item in a list has an index number.
Python indexing starts from "0".
Example
languages = [ " Python " , " Java " , " C++ " ]
print ( languages [ 0 ]) print ( languages [ 1 ])
Enter fullscreen mode
Exit fullscreen mode
Output
Python Java
Enter fullscreen mode
Exit fullscreen mode
Negative Indexing
Python allows accessing items from the end using negative…