Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tuple Operation and Tuple Assignment in Python.pdf
1. Tuple Operation and Tuple
Assignment in Python
Tuple Operation and Tuple
Assignment in Python
2. WHAT ARE TUPLES
TUPLE
TUPLE
A Tuple is an ordered collection
of elements in Python.
1.
2. Tuples in Python are both immutable
(cannot be changed) and ordered (element
sequence is preserved).
3. Tuples are used for storing related
data, returning multiple values from
functions, and as keys in dictionaries.
3. Tuples are created using
parentheses () and separating
values with commas.
creating tuple
This code snippet defines a tuple named my_tuple with three
elements: an integer, a string, and a floating-point number. Tuples
can contain a mix of different data types, and they preserve the order
of elements, making them a versatile and useful data structure in
Python.
4. You can acces elements in tuple using indexing by specifying the position of
the element within square brackets.the indexing starts at 0 for the first
element in the tuple
Tuples in python are zero indexed, meaning the first element is at index 0,the
second element is at index 1 and so on
Accessing Tuple
Elements
5. Concatenation: Concatenation is the process of combining
two or more tuples to create a new tuple. You can achieve
this by using the + operator.
Tuple Operations -
Concatenation
6. Tuple repetition allows you to create a new tuple by repeating an existing tuple
multiple times. You can achieve this using the * operator followed by the number
of times you want to repeat the tuple.
Here's how to use the * operator for tuple repetition:
In this example, original_tuple is repeated three times to create
repeated_tuple, which contains the elements from the original tuple
repeated in the specified order and number of times.
Tuple Operations -
Repetition
7. Tuple slicing is a technique in Python that allows
you to extract a portion of a tuple, creating a new
tuple containing those elements. Slicing is done
using the colon (:) notation within square
brackets and consists of two indices: the starting
index (inclusive) and the ending index (exclusive).
Here's how to use the slicing
notation for tuple slicing:
Tuple Operations -
Slicing
8. Tuple Assignment
Tuple Assignment
Tuple assignment is a way to
assign multiple values to
multiple variables at once
using tuples, enabling concise
assignments and value
swapping.
Assigning multiple values to variables:
Swapping variable values:
9. Swapping Values
with Tuples
Swapping in a tuple means exchanging the
positions or values of elements within the
tuple, typically using tuple assignment to
achieve this interchange.
10. Tuples are immutable, ordered sequences in Python. They
offer two methods: count() for counting occurrences of a
specific value in the tuple and index() for finding the index
of the first occurrence of a specified value. Tuples provide
data integrity and ordered storage.
Tuple Methods
11. CONCLUSION
"In conclusion, we've explored the world of tuples in Python, and we've learned that they are like
sealed envelopes, holding valuable information that can't be altered once sealed. We've seen how to
create tuples, access their elements, and use various operations to manipulate them in creative ways.
Tuples might seem simple, but they can be incredibly powerful and versatile tools in Python, helping
you organize and work with data effectively. So, I encourage you to experiment and have fun with
tuples in your Python projects. They can simplify your code and add a touch of creativity to your
programming journey