Python Lists tutorial

Python list is a collection of items of any data type. Lists are mutable, which means they can be modified. Python provides various operations that can be performed on lists like insertion, deletion, and sorting.

Creating a List

A list can …

Python list is a collection of items of any data type. Lists are mutable, which means they can be modified. Python provides various operations that can be performed on lists like insertion, deletion, and sorting.

Creating a List

A list can be created in Python by enclosing a sequence of items inside square brackets [], separated by commas.

# empty list
my_list = []

# list with items
fruits = ['apple', 'banana', 'cherry']

Accessing List Items

Items in a list can be accessed by using the index value. The first item in the list has an index of 0, the second item has an index of 1, and so on.

fruits = ['apple', 'banana', 'cherry']
print(fruits[1])  # Output: banana

Modifying List Items

List items can be modified by accessing the item using its index and assigning a new value.

fruits = ['apple', 'banana', 'cherry']
fruits[1] = 'orange'
print(fruits)  # Output: ['apple', 'orange', 'cherry']

List Loop

A for loop can be used to iterate through the items in a list.

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(fruit)

output:

apple
banana
cherry

Print Share Comment Cite Upload Translate
APA
Max | Sciencx (2024-03-29T09:23:11+00:00) » Python Lists tutorial. Retrieved from https://www.scien.cx/2023/02/18/python-lists-tutorial/.
MLA
" » Python Lists tutorial." Max | Sciencx - Saturday February 18, 2023, https://www.scien.cx/2023/02/18/python-lists-tutorial/
HARVARD
Max | Sciencx Saturday February 18, 2023 » Python Lists tutorial., viewed 2024-03-29T09:23:11+00:00,<https://www.scien.cx/2023/02/18/python-lists-tutorial/>
VANCOUVER
Max | Sciencx - » Python Lists tutorial. [Internet]. [Accessed 2024-03-29T09:23:11+00:00]. Available from: https://www.scien.cx/2023/02/18/python-lists-tutorial/
CHICAGO
" » Python Lists tutorial." Max | Sciencx - Accessed 2024-03-29T09:23:11+00:00. https://www.scien.cx/2023/02/18/python-lists-tutorial/
IEEE
" » Python Lists tutorial." Max | Sciencx [Online]. Available: https://www.scien.cx/2023/02/18/python-lists-tutorial/. [Accessed: 2024-03-29T09:23:11+00:00]
rf:citation
» Python Lists tutorial | Max | Sciencx | https://www.scien.cx/2023/02/18/python-lists-tutorial/ | 2024-03-29T09:23:11+00:00
https://github.com/addpipe/simple-recorderjs-demo