Python Lists & Tuples with simple examples

Introduction

Welcome to our article on Lists and Tuples in Python! In this lesson, we’ll explore what lists and tuples are, why they are useful, and how you can use them in your Python programs. Both lists and tuples are ways to store multiple items in a single variable, making it easier to manage and manipulate data.

What are Lists?

A list is a collection of items that are ordered and changeable (modifiable). Lists are written with square brackets [], and items are separated by commas.

Example:

Let’s create a list of fruits:

fruits = ["apple", "banana", "cherry"]
print(fruits)

This code will output:

['apple', 'banana', 'cherry']
Accessing List Items

You can access any item in a list by referring to its index (position) inside square brackets. Remember, Python indexing starts at 0.

print(fruits[0])  # Output: apple
print(fruits[1]) # Output: banana
print(fruits[2]) # Output: cherry
Modifying List Items

You can change the value of a specific item in the list by accessing its index and assigning a new value.

fruits[1] = "blueberry"
print(fruits) # Output: ['apple', 'blueberry', 'cherry']
Adding Items to a List

You can add items to a list using the append() method.

fruits.append("orange")
print(fruits) # Output: ['apple', 'blueberry', 'cherry', 'orange']
Removing Items from a List

You can remove items from a list using the remove() method.

fruits.remove("blueberry")
print(fruits) # Output: ['apple', 'cherry', 'orange']

What are Tuples?

A tuple is similar to a list, but it is immutable, meaning that you cannot change, add, or remove items after the tuple is created. Tuples are written with parentheses () and items are separated by commas.

Example:

Let’s create a tuple of colors:

colors = ("red", "green", "blue")
print(colors)

This code will output:

('red', 'green', 'blue')
Accessing Tuple Items

Just like lists, you can access any item in a tuple by referring to its index.

print(colors[0])  # Output: red
print(colors[1]) # Output: green
print(colors[2]) # Output: blue
Why Use Tuples?

Tuples are useful when you want to ensure that the data cannot be modified. This can help protect data integrity.

Real-Time Examples

Example 1: Student Grades

Let’s say we want to store grades for three subjects for a student.

Using a list (because grades might change):

grades = [85, 90, 88]
print("Initial Grades:", grades)

# Updating a grade
grades[2] = 92
print("Updated Grades:", grades)

Using a tuple (for fixed grades):

fixed_grades = (85, 90, 88)
print("Fixed Grades:", fixed_grades)
Example 2: Days of the Week

Days of the week don’t change, so a tuple is a good choice here:

days_of_week = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
print(days_of_week)

Conclusion

In this article, we learned about lists and tuples in Python. Lists are great for collections of items that can change, while tuples are used for collections that should remain constant. Understanding these concepts will help you manage and manipulate data effectively in your Python programs.

Practice Exercise

  1. Create a list of your favorite movies and print it.
  2. Add a new movie to the list and print the updated list.
  3. Remove the second movie from the list and print the updated list.
  4. Create a tuple of your favorite sports and print it.

Happy coding!

Leave a Reply