Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Python Unit - 3( c ) Tuple

The document provides an introduction to tuples in Python, highlighting their characteristics such as being ordered, immutable, and allowing duplicate values. It covers tuple syntax, methods, and use cases, emphasizing their utility in storing fixed data and as dictionary keys. The conclusion reiterates the importance of tuples for efficient data handling in programming.

Uploaded by

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

Python Unit - 3( c ) Tuple

The document provides an introduction to tuples in Python, highlighting their characteristics such as being ordered, immutable, and allowing duplicate values. It covers tuple syntax, methods, and use cases, emphasizing their utility in storing fixed data and as dictionary keys. The conclusion reiterates the importance of tuples for efficient data handling in programming.

Uploaded by

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

CSA 6007T

INTRODUCTION TO PYTHON
PROGRAMMING
BCA – II YEAR
IV SEMESTER

By:-
Vinita Thanvi
Assistant Professor
Lucky Institute of Professional Studies

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS
Python Complex Data Types:
Tuple

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Tuple :

• A tuple is an ordered, immutable collection of elements in


Python.
• Once a tuple is created, its elements cannot be changed,
added, or removed.
• Tuples are used when you want to ensure that the data
remains unchanged throughout the program.
• Tuples are used to store multiple items in a single variable.

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Tuple Items
Tuple items are ordered, unchangeable, and allow duplicate values.

Tuple items are indexed, the first item has index [0], the second item has index [1]
etc.

Ordered
When we say that tuples are ordered, it means that the items have a defined order,
and that order will not change.

Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove items
after the tuple has been created.

Allow Duplicates
Since tuples are indexed, they can have items with the same value:

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Tuple Syntax:

• The syntax for creating a tuple:


tuple_name = (element1, element2, element3, ...)
• Parentheses () are used to define a tuple.
• The elements inside the tuple are separated by commas ,.
• You can create a tuple with any type of element: integers, strings, lists, etc.
• Creating a Tuple:
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple) # Output: (1, 2, 3, 4, 5)

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Single element tuple:

When you create a tuple with only one element, you need a trailing
comma to distinguish it from a regular parenthesis.
single_element_tuple = (5,)
print(single_element_tuple) # Output: (5)

Tuple without parentheses (using commas):

You can create a tuple without parentheses by using just commas, but
this is usually less readable.
tuple_without_parentheses = 1, 2, 3
print(tuple_without_parentheses) # Output: (1, 2, 3)

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Empty Tuple:

empty_tuple = ()
print(empty_tuple) # Output: ()

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Accessing Elements in a Tuple:

• Tuples are ordered, so you can access elements by their index


(starting from 0).
• Negative indexing is also supported.
my_tuple = (10, 20, 30, 40, 50)
# Accessing elements by index
print(my_tuple[0]) # Output: 10
print(my_tuple[3]) # Output: 40
# Negative indexing
print(my_tuple[-1]) # Output: 50
(last element)
print(my_tuple[-2]) # Output: 40
(second last element)

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Slicing a Tuple:You can slice a tuple just like a list to get a
subtuple.pythonCopymy_tuple = (10, 20, 30, 40, 50)# Slicing
the tuplesub_tuple = my_tuple[1:4] # From index 1 to 3 (not
including 4)print(sub_tuple) # Output: (20, 30, 40)

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Immutability of Tuples:Once a tuple is created, you cannot
change its elements, add new elements, or remove elements. If
you try to modify the tuple, you'll get an
error.pythonCopymy_tuple = (10, 20, 30)# Attempt to change
an element (will raise an error)# my_tuple[1] = 25 #
Uncommenting this will raise a TypeError# You can, however,
create a new tuple with modified valuesnew_tuple =
my_tuple[:1] + (25,) + my_tuple[2:]print(new_tuple) # Output:
(10, 25, 30)

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Tuple Methods:Although tuples are immutable, they come with
a few useful methods:count(): Returns the number of
occurrences of an element in the tuple.index(): Returns the
index of the first occurrence of an element in the
tuple.Example:pythonCopymy_tuple = (1, 2, 3, 4, 2, 5)#
count()print(my_tuple.count(2)) # Output: 2 (2 appears twice)#
index()print(my_tuple.index(3)) # Output: 2 (3 is at index 2)

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Concatenating and Repeating Tuples:You can concatenate or
repeat tuples using + and *
operators.Concatenation:pythonCopytuple1 = (1, 2, 3)tuple2 =
(4, 5, 6)# Concatenate two tuplesnew_tuple = tuple1 +
tuple2print(new_tuple) # Output: (1, 2, 3, 4, 5,
6)Repeating:pythonCopytuple1 = (1, 2, 3)# Repeat a tuple
multiple timesnew_tuple = tuple1 * 3print(new_tuple) #
Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Nested Tuples:Tuples can contain other tuples (nested tuples),
which can hold a variety of data types.pythonCopynested_tuple
= ((1, 2), (3, 4), (5, 6))print(nested_tuple) # Output: ((1, 2), (3,
4), (5, 6))You can also access elements in the nested
tuples.pythonCopyprint(nested_tuple[1][1]) # Output: 4
(second tuple, second element)

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Tuple Unpacking:You can unpack the elements of a tuple into
variables in a very convenient way.pythonCopymy_tuple = (10,
20, 30)# Unpacking tuple into variablesa, b, c =
my_tupleprint(a) # Output: 10print(b) # Output: 20print(c) #
Output: 30You can also use an asterisk * to capture multiple
elements.pythonCopymy_tuple = (10, 20, 30, 40)# Unpacking
with asteriska, *b, c = my_tupleprint(a) # Output: 10print(b) #
Output: [20, 30]print(c) # Output: 40

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Use Cases of Tuples:Immutability:Tuples are ideal for use cases
where the data shouldn't be changed after creation. For
example, storing coordinates (latitude, longitude), RGB color
values, etc.As dictionary keys:Since tuples are immutable, they
can be used as keys in a dictionary, unlike
lists.pythonCopymy_dict = { (1, 2): "Point A", (3, 4): "Point
B" }print(my_dict[(1, 2)]) # Output: Point AReturn multiple
values from a function:Tuples are often used to return multiple
values from a function.pythonCopydef get_coordinates():
return (10.0, 20.0)coordinates =
get_coordinates()print(coordinates) # Output: (10.0, 20.0)
Iteration:Tuples are often used in iteration over sequences
where the order and immutability are important.

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Summary of Tuple Characteristics:Ordered: Elements are in a
specific order and can be accessed by index.Immutable: Once
created, the elements cannot be modified.Allow duplicate
elements: Tuples can contain duplicate values.Indexing and
slicing: Tuples support both positive and negative indexing as
well as slicing.Heterogeneous: Can store elements of different
data types.

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


Conclusion:Tuples are useful in Python when you want to store
a fixed set of values, which should not be changed, and they
provide efficient ways of accessing, slicing, and iterating over
data. They are often used for storing coordinate pairs, multiple
return values, and as dictionary keys.

By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS


By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS
By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS
By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS
By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS
By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS
By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS
By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS
By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS
By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS
Thank You
By: Mrs. Vinita Thanvi Assistant Professor, IT Department, LIPS

You might also like