Python Lists

Hey friends 👋 welcome back, In the last lesson, we learned about functions, Today, we are going to learn about lists, which are used to store multiple values in a single variable, Lists are very useful when you want to organize and work with collections of data.

Creating a List

You can create a list using square brackets [], Example:

fruits = ["apple", "banana", "mango"] numbers = [1, 2, 3, 4, 5] mixed = ["Yusuf", 14, 1.6, True]
  • Lists can store different types of values together

  • Items in a list are ordered, which means they have a specific position called an index

Accessing List Items

You can access items using their index, starting from 0, Example:

fruits = ["apple", "banana", "mango"] print(fruits[0]) print(fruits[2])

Output:

apple mango

Modifying List Items

You can change the value of any item by its index, Example:

fruits = ["apple", "banana", "mango"] fruits[1] = "orange" print(fruits)

Output:

['apple', 'orange', 'mango']

Adding Items to a List

You can add items using append() or insert(), Example:

fruits = ["apple", "banana"] fruits.append("mango") # adds at the end fruits.insert(1, "orange") # adds at index 1 print(fruits)

Output:

['apple', 'orange', 'banana', 'mango']

Removing Items from a List

You can remove items using remove() or pop(), Example:

fruits = ["apple", "banana", "mango"] fruits.remove("banana") # removes by value fruits.pop(0) # removes by index print(fruits)

Output:

['mango']

Practice Time

Create a Python file called lists.py and try these exercises, 1, Create a list of five fruits and print the first and last items, 2, Change one fruit in the list and print the updated list, 3, Add two new items to your list and print it, 4, Remove one item by value and one by index, then print the final list

Great job, You just learned how to store, access, and modify multiple values in Python using lists, In the next lesson, we will explore Python Tuples and Sets, where we will learn about other types of collections, We will soon be launching our YouTube channel, where we are going to post step-by-step video tutorials on Python and other tech skills, These videos will make learning easier and more visual, helping beginners follow along confidently.