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

Introduction-to-Tuples-in-Python

Uploaded by

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

Introduction-to-Tuples-in-Python

Uploaded by

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

Introduction to

Tuples in Python
Welcome to our exploration of tuples in Python! In this
presentation, we'll cover the key features of tuples, compare
them with lists, and explore essential commands for working
with them.
Tuples and Lists
Tuples and lists are both used to store collections of data.
Tuples are immutable, meaning they cannot be changed,
while lists are mutable, allowing for modifications.

Tuples Lists
Order matters, cannot be Order matters, can be
changed. changed.
Comparison: Tuples vs.
Lists
Understanding the differences between tuples and lists is key to
choosing the right data structure for your needs.

Feature Tuple List

Mutability Immutable Mutable

Syntax Parentheses () Square brackets []

Performance Generally faster Slightly slower

Use Cases Representing Storing dynamic


constant data, like data, like
dates, coordinates shopping cart
items
Tuple Commands
A variety of commands can be used to work with tuples.

1 min() 2 max() 3 sum()


Finds the smallest element Finds the largest element Calculates the sum of
in a tuple. in a tuple. elements in a tuple.

4 sorted() 5 count()
Returns a sorted list from a tuple. Counts the occurrences of a specific element
in a tuple.
Code Example
Let's put our knowledge of tuple commands into practice with a code example.

my_tuple = (1, 2, 3, 4, 3)
print(min(my_tuple)) # Output: 1
print(max(my_tuple)) # Output: 4
print(sum(my_tuple)) # Output: 13
print(sorted(my_tuple)) # Output: [1, 2, 3, 3, 4]
print(my_tuple.count(3)) # Output: 2
Quiz Time!
Let's test your understanding of tuples with some multiple-choice questions!

What is the output of the following code?

tuple1 = (4, 2, 8, 6) print(min(tuple1))

• (A) 2
• (B) 4
• (C) 8
• (D) 6
Answer: A
Quiz Time!
Let's test your understanding of tuples with some multiple-choice questions!

Which of the following commands can modify a tuple?


• (A) append()
• (B) remove()
• (C) count()
• (D) None
Answer: D
Quiz Time!
Let's test your understanding of tuples with some multiple-choice questions!

What will sorted((3, 1, 2)) return?


• (A) (3, 1, 2)
• (B) [1, 2, 3]
• (C) [3, 2, 1]
• (D) (1, 2, 3)

Answer: B
Quiz Time!
Let's test your understanding of tuples with some multiple-choice questions!

Tuples are preferred over lists when:


• (A) Data needs to be mutable
• (B) Data should remain constant
• (C) We need to sort data
• (D) We need to count elements
Answer: B
Quiz Time!
Let's test your understanding of tuples with some multiple-choice questions!

What is the result of sum((1, 2, 3))?


• (A) 1
• (B) 3
• (C) 6
• (D) Error
Answer: C
Thank You
Thank you for joining us in this journey through the world of
Python tuples!

You might also like