1 of 14

Free Python

Class For Girls

Week 12

Mary Xie

Michelle Xie

COLORS

BLACK

SLIDESMANIA.COM

2 of 14

Reflect

  • What is one thing you remember from last week's class?
  • What do you remember about data structures?

COLORS

BLACK

SLIDESMANIA.COM

3 of 14

Tuples

COLORS

BLACK

SLIDESMANIA.COM

4 of 14

What are tuples?

A tuple is an ordered and immutable collection of elements. Tuples are used to group together related data, and they are similar to lists in many ways, but with a key difference: once a tuple is created, its elements cannot be modified, added, or removed.

COLORS

BLACK

SLIDESMANIA.COM

5 of 14

How are tuples used?

Tuples are defined using parentheses.

For example, my_tuple() = (1, “apple”, 3.14)

COLORS

BLACK

SLIDESMANIA.COM

6 of 14

How are tuples similar to lists and sets?

Tuples are similar to lists in that they can store multiple items. Like sets, tuples are also ordered collections, but unlike sets, they can contain duplicate elements.

COLORS

BLACK

SLIDESMANIA.COM

7 of 14

Characteristics of tuples

Ordered

Immutable

Heterogenous

Accessed by index

tuples maintain the order of elements, meaning the order in which elements are added to the tuple is preserved.

once a tuple is created, you cannot modify its elements. You cannot add, remove, or change the order of elements within a tuple.

tuples can contain elements of different data types, allowing you to create a collection of diverse values.

elements in a tuple can be accessed using their index(position) within the tuple. Indexing starts at 0.2005

COLORS

SLIDESMANIA.COM

8 of 14

Example

In this example, “my_tuple” contains three elements: an integer (‘1’), a string (‘ “apple” ‘), and a floating point number (‘3.14’)

my_tuple() = (1, “apple”, 3.14)

COLORS

BLACK

SLIDESMANIA.COM

9 of 14

Example

In this example, we created a tuple to store contact information (name, phone, email)

contact_info = ("Alice Smith", "555-1234", "alice.smith@example.com")

print(f"Name: {contact_info[0]}")

print(f"Phone: {contact_info[1]}")

print(f"Email: {contact_info[2]}")

COLORS

BLACK

SLIDESMANIA.COM

10 of 14

Example

We created a tuple to store the daily temperatures for a week. This program calculates and prints the average temperature for the week.

weekly_temperatures = (70, 72, 68, 71, 75, 73, 69)

average_temperature = sum(weekly_temperatures) / len(weekly_temperatures)

print(f"The average temperature for the week is: {average_temperature:.2f}°F")

COLORS

BLACK

SLIDESMANIA.COM

11 of 14

Example

This is a more complex program. Basically, we created a tuple to store information about a movie, including its title, director, year of release, and rating. Write a script to print each piece of information and determine if the movie is rated above a certain threshold (e.g., 7.0).

# Create a tuple to store movie information (title, director, year, rating)

movie_info = ("Inception", "Christopher Nolan", 2010, 8.8)

# Print the movie information

print(f"Title: {movie_info[0]}")

print(f"Director: {movie_info[1]}")

print(f"Year of Release: {movie_info[2]}")

print(f"Rating: {movie_info[3]}")

# Define the rating threshold

rating_threshold = 7.0

# Check if the movie is rated above the threshold

if movie_info[3] > rating_threshold:

print(f"The movie '{movie_info[0]}' is rated above {rating_threshold}.")

else:

print(f"The movie '{movie_info[0]}' is rated below {rating_threshold}.")

COLORS

BLACK

SLIDESMANIA.COM

12 of 14

Let’s practice!

Create a tuple with a set of 4 numbers and 2 words.

Tuple() = (-2, 0, 999, “Hello!”, 143, “Love”

COLORS

BLACK

SLIDESMANIA.COM

13 of 14

Let’s practice!

Using the tuple we created previously, print out the 3rd element of the tuple.

Tuple() = (-2, 0, 999, “Hello!”, 143, “Love”

third_element() = Tuple[2]

We use 2 because the index of the first element is 0. Therefore the third element’s index would be 2

COLORS

BLACK

SLIDESMANIA.COM

14 of 14

Thank You!

COLORS

BLACK

SLIDESMANIA.COM