0% found this document useful (0 votes)
2 views

Python_4

This document provides an overview of Python's data structures: lists and tuples. It explains the definitions, creation methods, key operations, and use cases for both structures, along with hands-on exercises for practice. Additional resources for further learning are also included.

Uploaded by

infinitein093
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python_4

This document provides an overview of Python's data structures: lists and tuples. It explains the definitions, creation methods, key operations, and use cases for both structures, along with hands-on exercises for practice. Additional resources for further learning are also included.

Uploaded by

infinitein093
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Day 4: Data Structures I – Lists & Tuples

Today, you'll learn about two of Python's fundamental data structures: lists and tuples. These
structures allow you to store collections of items, each with their own properties and uses. Follow
the detailed plan below, complete the examples, and work through the exercises.

Step 1: Understanding Lists

What is a List?

• Definition:
A list is an ordered, mutable (changeable) collection of items. Lists can hold items of any
data type, including numbers, strings, and even other lists.

• Creating a List:
You can create a list by placing items within square brackets [], separated by commas.

• # Example list of fruits

fruits = ["apple", "banana", "cherry"]

print(fruits)

Key Operations on Lists

1. Accessing Elements (Indexing):


Lists are zero-indexed, meaning the first element is at index 0.

print("First fruit:", fruits[0]) # Output: apple

print("Last fruit:", fruits[-1]) # Output: cherry (using negative index)

2. Slicing:
Extract a portion of the list.

print("Slice (first two items):", fruits[0:2]) # Output: ['apple', 'banana']

3. Adding Elements:
Use the .append() method to add an element at the end.

fruits.append("orange")

print("After appending:", fruits)

4. Inserting Elements:
Insert an element at a specified position with .insert(index, element).

fruits.insert(1, "mango")

print("After inserting at index 1:", fruits)

5. Removing Elements:
Remove an element by value with .remove(), or by index using del or .pop().

fruits.remove("banana") # Removes the first occurrence of "banana"


print("After removing 'banana':", fruits)

# Using pop (returns and removes the last element by default)

last_fruit = fruits.pop()

print("Popped element:", last_fruit)

print("After popping:", fruits)

6. Iterating Over a List:


Use a for loop to go through each element.

for fruit in fruits:

print("Fruit:", fruit)

Step 2: Exploring Tuples

What is a Tuple?

• Definition:
A tuple is similar to a list in that it is an ordered collection of items. However, tuples are
immutable (their elements cannot be changed after creation).

• Creating a Tuple:
Tuples are created by placing items within parentheses (), separated by commas.

• # Example tuple of colors

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

print(colors)

Key Points about Tuples

• Immutability:
Once created, you cannot modify a tuple. This is useful when you want to ensure the data
remains constant.

• Accessing Elements:
Like lists, tuples support indexing and slicing.

print("First color:", colors[0])

print("Slice (first two colors):", colors[0:2])

• When to Use Tuples:


Use tuples when you have a collection of items that should not change throughout the
program, such as coordinates or fixed configuration values.

Step 3: Hands-On Exercises


Exercise 1: List Practice

1. Task: Create a list of your favorite movies.

2. Steps:

o Create a list named movies.

o Print the list.

o Append a new movie to the list.

o Insert a movie at the second position.

o Remove one movie by its name.

o Print the final list.

Sample Code:

# Exercise: Favorite Movies List

movies = ["Inception", "The Matrix", "Interstellar"]

print("Original list:", movies)

# Append a new movie

movies.append("The Prestige")

print("After appending:", movies)

# Insert a movie at index 1

movies.insert(1, "Fight Club")

print("After inserting at index 1:", movies)

# Remove a movie by name

movies.remove("Interstellar")

print("After removing 'Interstellar':", movies)

Exercise 2: Tuple Practice

1. Task: Create a tuple of your three favorite programming languages.

2. Steps:

o Create a tuple named languages.

o Print the tuple.

o Try accessing the second element.


o Experiment with slicing the tuple.

Sample Code:

# Exercise: Favorite Programming Languages Tuple

languages = ("Python", "JavaScript", "C++")

print("Languages tuple:", languages)

# Accessing the second element

print("Second language:", languages[1])

# Slicing the tuple (first two languages)

print("First two languages:", languages[:2])

Step 4: Experiment in the Python Interactive Shell

1. Open the Shell:


In your terminal, type:

python

2. Try Out List and Tuple Commands:


Experiment with creating and manipulating lists and tuples:

# Create and manipulate a list

numbers = [1, 2, 3, 4, 5]

print("Numbers:", numbers)

numbers.append(6)

print("After appending 6:", numbers)

print("Slice [2:5]:", numbers[2:5])

# Create and inspect a tuple

coordinates = (10, 20, 30)

print("Coordinates tuple:", coordinates)

print("First coordinate:", coordinates[0])

3. Exit the Shell:


Type:

exit()
Step 5: Additional Learning Resources

• W3Schools – Python Lists:


W3Schools Python Lists

• W3Schools – Python Tuples:


W3Schools Python Tuples

You might also like