Learn Python
Lesson 1 - Introduction To Python
Lesson 2 - Basic Python Syntax
Lesson 3 - Control Flow
Lesson 4 - Functions
Lesson 6 - Exception Handling
Lesson 7 - Modules And Packages
Lesson 8 - Basic String Operations
Lesson 9 - Object-Oriented Programming (OOP)
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.