Introduction to Python Lists and Tuples

Tutorial 1 of 5

1. Introduction

The goal of this tutorial is to provide a comprehensive introduction to Python's list and tuple data structures. By the end of this tutorial, you will be able to create, access, modify, and manipulate lists and tuples in Python.

As prerequisites, you should have a basic understanding of Python syntax and data types.

2. Step-by-Step Guide

2.1 Lists

In Python, a list is a mutable, or changeable, ordered sequence of elements. Each element or value that is inside a list is called an item. Lists are defined by having values between square brackets [].

# Defining a list
my_list = [1, 2, 3, "python", 4.5]

You can access list items by referring to the index number, with the first index being 0.

# Accessing list items
print(my_list[0])  # Output: 1
print(my_list[3])  # Output: "python"

2.2 Tuples

A tuple is similar to a list in that it is an ordered sequence of elements. However, tuples are immutable, meaning they cannot be changed after they are created. Tuples are defined by having values between parentheses ().

# Defining a tuple
my_tuple = (1, 2, 3, "python", 4.5)

Like lists, you can access tuple items by referring to the index number.

# Accessing tuple items
print(my_tuple[0])  # Output: 1
print(my_tuple[3])  # Output: "python"

3. Code Examples

3.1 List Manipulation

# Creating a list
fruits = ["apple", "banana", "cherry"]

# Adding an item to the end of the list
fruits.append("orange")
print(fruits)  # Output: ['apple', 'banana', 'cherry', 'orange']

# Removing an item from the list
fruits.remove("banana")
print(fruits)  # Output: ['apple', 'cherry', 'orange']

3.2 Tuple Manipulation

# Creating a tuple
fruits = ("apple", "banana", "cherry")

# Attempting to change a tuple (this will cause an error)
fruits[0] = "orange"  # TypeError: 'tuple' object does not support item assignment

4. Summary

In this tutorial, we've covered the basics of lists and tuples in Python. We've learned how to create these data structures, access their elements, and manipulate lists.

For further learning, consider exploring more complex operations on lists and tuples, such as list comprehensions and tuple unpacking. Additional resources include the Python documentation on lists and tuples.

5. Practice Exercises

  1. Create a list of your favorite movies. Then, add a new movie to the list and print the updated list.

  2. Create a tuple of your favorite books. Then, try to remove a book from the tuple (you should get an error).

  3. Create a list of numbers. Write a Python program to replace the last item in the list with a new number.

Here are the solutions for the exercises:

movies = ["The Godfather", "The Dark Knight", "Pulp Fiction"]
movies.append("Inception")
print(movies)  # Output: ['The Godfather', 'The Dark Knight', 'Pulp Fiction', 'Inception']
books = ("1984", "To Kill a Mockingbird", "The Great Gatsby")
books[0] = "Moby Dick"  # TypeError: 'tuple' object does not support item assignment
numbers = [1, 2, 3, 4, 5]
numbers[-1] = 10
print(numbers)  # Output: [1, 2, 3, 4, 10]