Welcome to our Python Lists tutorial! In this video, we will cover the basics of lists in Python. Lists are a fundamental data structure in Python and are used to store collections of items.

What is a List?

A list in Python is a collection of items that can be of different data types. Lists are ordered and mutable, meaning you can add, remove, and change items in the list.

List Syntax

To create a list, you can use square brackets [] and separate items with commas.

my_list = [1, "apple", 3.14, True]

List Operations

Accessing Items

You can access items in a list by their index. The index starts at 0 for the first item.

print(my_list[0])  # Output: 1
print(my_list[1])  # Output: "apple"

Adding Items

To add an item to the end of a list, you can use the append() method.

my_list.append("banana")
print(my_list)  # Output: [1, "apple", 3.14, True, "banana"]

Inserting Items

To insert an item at a specific position in a list, you can use the insert() method.

my_list.insert(2, "orange")
print(my_list)  # Output: [1, "apple", "orange", 3.14, True, "banana"]

Removing Items

To remove an item from a list, you can use the remove() method.

my_list.remove("orange")
print(my_list)  # Output: [1, "apple", 3.14, True, "banana"]

Looping Through a List

You can loop through a list using a for loop.

for item in my_list:
    print(item)

Practice

To practice what you've learned, try creating your own list and performing the following operations:

  1. Add an item to the end of the list.
  2. Insert an item at the second position.
  3. Remove the first item.
  4. Loop through the list and print each item.

For more information on Python lists, check out our Python Lists Advanced Tutorial.


Python Lists