What is Tuple Operations in Python | EP-11 Tuple Operations in Python | Basic Tuple Operations
#PythonTupleOperations
"""
1. Accessing of Python Tuples
2. Concatenation of Tuples
3. Slicing of Tuple
4. Deleting a Tuple"""
1. Accessing of Tuples
""" In Python Programming, Tuples are immutable, and usually, they contain a
sequence of heterogeneous elements that are accessed via unpacking or indexing
(or even by attribute in the case of named tuples).
Note:
In unpacking of tuple number of variables on the left-hand side should be equal
to a number of values in given tuple a. """
Tuple unpackingcon_1 = (1,2,3)
con_2 =("a","b")
company = ("cybrosys","technologies")
a,b =company
print("a",a)
print("b",b)
print("company",company)
2. Concatenation of Tuples
"""Concatenation of tuple is the process of joining two or more Tuples.
Concatenation is done by the use of ‘+’ operator. Concatenation of tuples is
done always from the end of the original tuple. Other arithmetic operations do
not apply on Tuples.
Note- Only the same datatypes can be combined with concatenation, an error
arises if a list and a tuple are combined."""
Concatenation of tuples
con1 =(1,2,3)
con2 = ("A","b")
result = con1 + con2
print(result)
con3 =tuple([7,8])
r = con2 +con3
3. Slicing of Tuple
"""
Slicing of a Tuple is done to fetch a specific range or slice of sub-elements
from a Tuple. Slicing can also be done to lists and arrays. Indexing in a list
results to fetching a single element whereas Slicing allows to fetch a set of
elements.
Note- Negative Increment values can also be used to reverse the sequence of
Tuples.
"""
Slicing of a Tuple
company1 =("cybrosys")
Removing First element
print("Removal of First Element: ")
print(company1[1:])
Reversing the Tuple
print("\nTuple after sequence of Element is reversed: ")
print(company1[::-1])
Printing elements of a Range
com = ("cybrosys")
print("\nPrinting elements between Range 4-6: ")
print(com[4:6])
4. Deleting a Tuple
"""Tuples are immutable and hence they do not allow deletion of a part of it.
The entire tuple gets deleted by the use of del() method.
Note- Printing of Tuple after deletion results in an Error.
syntax:del object"""
tuple1 = (1,2,3)
del tuple1
print(tuple1)
#Python #Programming #PythonProgramming #Tutorial #BeginnerPython #LearnPython #Tuples #PythonOperations #DataStructures #TupleOperations #Coding #PythonTutorial #PythonBasics #AdvancedPython #PythonCode
Connect With Us:
—————————————
➡️ Website: https://www.cybrosys.com/
➡️ Email: info@cybrosys.com
➡️ Twitter: / cybrosys
➡️ LinkedIn: / cybrosys
➡️ Facebook: / cybrosystechnologies
➡️ Instagram: / cybrosystech
➡️ Pinterest: / cybrosys