How to create and use lists in Python

1 min

n Python, a list is a data structure used to store multiple values in a single variable. Lists are enclosed in square brackets [] and the values inside are separated by commas.

Here is an example of how to create a list in Python:

#creating a list of numbers
numbers = [1, 2, 3, 4, 5]

#creating a list of strings
fruits = ['apple', 'banana', 'orange']

#creating a list of mixed data types
mixed_list = [1, 'hello', 3.14, True]

You can also create a list using the list() constructor which takes an iterable object as an argument:

#creating a list from a range of numbers
numbers = list(range(1, 6))

#creating a list from a string
letters = list("hello")

You can add, remove, or modify elements in a list, and use various list methods such as append(), insert(), remove() and more.