Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Lesson 5 - Basic Data Structures

5.1 Lists

Lists are ordered collections of items enclosed in square brackets [], capable of storing elements of different data types.

Imagine a classroom with 40 students, and you want to collect their names. This collection of student names is programmatically known as a list in Python.

Here's an example of a simple list containing various types of values:

my_list = [1, 2, 'apple', False]

To access a specific item from a list, you use its index (position). Remember, list item indices start from 0, not 1.

For example:

my_list = [1, 2, 'apple', True]
print(my_list[2])

Try changing the index inside the print() function to access different elements of my_list.

Similarly, you can change specific list items using their index, as shown below:

my_list = [1, 2, 'apple', True]
my_list[1] = 100
print(my_list)

The line my_list[1] = 100 modifies the item at index 1 in the list.