3. Topic 3(COLON) Data Structures and Programming Paradigms

Lesson 3.1: Lists And Tuples

#### Lesson focus #### Learning outcomes Students should be able to:.

Lesson 3.1: Lists and Tuples

Welcome to Lesson 3.1 of Foundation Computing! In this lesson, we will explore two fundamental data structures in Python: lists and tuples. By the end of this lesson, you'll have a solid understanding of how to use these structures effectively in your programming.

Learning Objectives

By the end of this lesson, you should be able to:

  • Understand lists as ordered, mutable collections; create, index, slice, and modify them.
  • Perform common list operations and methods such as append, insert, remove, and sort, and iterate over a list.
  • Recognize tuples as ordered, immutable collections and understand when immutability is useful.
  • Utilize list comprehensions as a concise construction technique.
  • Create and manipulate lists, including adding, removing, and updating elements.

Introduction to Lists

Lists in Python are powerful data structures that allow you to store an ordered collection of items. Let's break down the concept of lists together:

What are Lists?

A list is defined with square brackets [], and it can hold various data types—numbers, strings, and even other lists! For example:

my_list = [1, 2, 3, 'apple', 'banana']

Characteristics of Lists

  • Ordered: The order in which you insert items is preserved.
  • Mutable: You can change items in a list after its creation.

Basic List Operations

  1. Creating a List: You can create a list by simply defining it:
   fruits = ['apple', 'banana', 'cherry']
  1. Accessing Elements: Use indexing to access elements. Remember that indexing starts at 0!
   print(fruits[0])  # Outputs 'apple'
  1. Slicing: You can slice lists to access a subset of items:
   print(fruits[1:3])  # Outputs ['banana', 'cherry']
  1. Modifying a List: You can change an item by specifying its index:
   fruits[1] = 'blueberry'
   print(fruits)  # Outputs ['apple', 'blueberry', 'cherry']

Common List Methods

  • Append: Add an item to the end of the list:
  fruits.append('kiwi')
  • Insert: Add an item at a specific position:
  fruits.insert(1, 'orange')  # Adds 'orange' at index 1
  • Remove: Remove a specific item:
  fruits.remove('apple')
  • Sort: Sort the list in ascending order:
  fruits.sort()

Iterating Over a List

To perform operations on all items in a list, you can use a for loop:

for fruit in fruits:
    print(fruit)

This code will print each fruit in the fruits list!

Tuples: An Introduction

While lists are mutable, tuples are immutable. This means once you create a tuple, you cannot change its content. Let's explore tuples!

What are Tuples?

A tuple is defined with parentheses (). Here's an example:

my_tuple = (1, 2, 3, 'apple', 'banana')

Characteristics of Tuples

  • Ordered: Just like lists, the order of elements is preserved.
  • Immutable: Once created, the elements of a tuple cannot be changed.

When to Use Tuples

Tuples are useful when you want to ensure that the data does not change. For example, you may use a tuple to store a set of coordinates that should remain constant:

coordinates = (10.0, 20.0)

List Comprehensions

List comprehensions provide a succinct way to create lists. Here’s how it works. Suppose you want a new list containing the squares of numbers from 0 to 9:

squared_numbers = [x**2 for x in range(10)]

This compact line of code does the same job as a longer loop, but it’s more readable!

Example of List Comprehension

Let’s say you have a list of fruits, and you want to create a new list containing only those fruits that start with the letter 'b':

fruits = ['apple', 'banana', 'cherry', 'blueberry']
result = [fruit for fruit in fruits if fruit.startswith('b')]
# result will be ['banana', 'blueberry']

Conclusion

As we wrap up this lesson, remember that lists are ordered and mutable collections perfect for holding and managing data dynamically. Tuples, on the other hand, are great for fixed collections of items where the integrity of data is crucial. Mastering these structures will significantly enhance your programming skills in Python!

Study Notes

  • Lists are mutable; you can modify them.
  • Use square brackets [] to define a list.
  • Common list methods: append, insert, remove, sort.
  • Tuples are immutable; you cannot change them once created.
  • Use parentheses () to define a tuple.
  • List comprehensions provide a concise way to create lists based on existing lists.

Practice Quiz

5 questions to test your understanding

Lesson 3.1: Lists And Tuples — Computing | A-Warded