Python Tuples
Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers.
Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This helps in understanding the Python tuples more easily.
And we define them by the use of Parenthesis without use of Parenthesis is called Tuple Packing.
Lets see an Example :
#Creating an empty Tuple
Tuple1
=
()
print
(
"Initial empty Tuple: "
)
print
(Tuple1)
#Creatting a Tuple
#with the use of string
Tuple1
=
(
'Code'
,
'For'
)
print
(
"\nTuple with the use of String: "
)
print
(Tuple1)
# Creating a Tuple with
# the use of list
list1
=
[
1
,
2
,
4
,
5
,
6
]
print
(
"\nTuple using List: "
)
print
(
tuple
(list1))
#Creating a Tuple
#with the use of built-in function
Tuple1
=
tuple
(
'Code'
)
print
(
"\nTuple with the use of function: "
)
print
(Tuple1)
output :
Initial empty Tuple:
()
Tuple with the use of String:
('Code', 'For')
Tuple using List:
(1, 2, 4, 5, 6)
Tuple with the use of function:
('C', 'o', 'd', 'e')
L
ets see how we can create a tuple with mixed data type.
Tuples can contain any number of elements and of any datatype (like strings, integers, list, etc.). Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is not sufficient, there must be a trailing ‘comma’ to make it a tuple by the definition.
Example
#Creating a Tuple
#with Mixed Datatype
Tuple1
=
(
4
,
'Welcome'
,
7
, 'Code
'
)
print
(
"\nTuple with Mixed Datatypes: "
)
print
(Tuple1)
#Creating a Tuple
#with nested tuples
Tuple1
=
(
0
,
1
,
2
,
3
)
Tuple2
=
(
'python'
,
'code'
)
Tuple3
=
(Tuple1, Tuple2)
print
(
"\nTuple with nested tuples: "
)
print
(Tuple3)
#Creating a Tuple
#with repetition
Tuple1
=
(
'Code'
,)
*
3
print
(
"\nTuple with repetition: "
)
print
(Tuple1)
#Creating a Tuple
#with the use of loop
Tuple1
=
(
'Code'
)
n
=
5
print
(
"\nTuple with a loop"
)
for
i
in
range
(
int
(n)):
Tuple1
=
(Tuple1,)
print
(Tuple1)
output :
Tuple with Mixed Datatypes:
(4, 'Welcome', 7, 'Geeks')
Tuple with nested tuples:
((0, 1, 2, 3), ('python', 'code'))
Tuple with repetition:
('Code', 'Code', 'Code')
Tuple with a loop
('Code',)
(('Code',),)
((('Code',),),)
(((('Code',),),),)
((((('Code',),),),),)
Accessing of Tuple
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). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.
NOTE : In unpacking of tuple number of variables on left hand side should be equal to number of values in given tuple a.
#Accessing Tuple
#with Indexing
Tuple1
=
tuple
(
"Code"
)
print
(
"\nFirst element of Tuple: "
)
print
(Tuple1[
1
])
#Tuple unpacking
Tuple1
=
(
"Code"
,
"For"
,
"Dudle"
)
#This line unpack
#values of Tuple1
a, b, c
=
Tuple1
print
(
"\nValues after unpacking: "
)
print
(a)
print
(b)
print
(c)
output :
First element of Tuple:
o
Values after unpacking:
Code
For
Dudle
Note : As we know that the Tuples are immutable Lets see how
When we once set the Tuple, then we cant change the elements by another elements, if we do then it will give an error
print(Tuple2)
output :
File "E:/program/tuple.py", line 2, in <module>
Tuple2[1]=8
TypeError: 'tuple' object does not support item assignment
Concatenation of Tuples
Concatenation of tuple is the process of joining of two or more Tuples. Concatenation is done by the use of ‘+’ operator and it s is done always from the end of the original tuple. Other arithmetic operations do not apply on Tuples.
Note- Only same datatypes can be combined with concatenation, an error arises if a list and a tuple are combined.
code :
# Concatenaton of tuples
Tuple1
=
(
0
,
1
,
2
,
3
)
Tuple2
=
(
'Code'
,
'For'
,
'Life'
)
Tuple3
=
Tuple1
+
Tuple2
# Printing first Tuple
print
(
"Tuple 1: "
)
print
(Tuple1)
# Printing Second Tuple
print
(
"\nTuple2: "
)
print
(Tuple2)
# Printing Final Tuple
print
(
"\nTuples after Concatenaton: "
)
print
(Tuple3)
output :
Tuple 1:
(0, 1, 2, 3)
Tuple2:
('Code', 'For', 'Life')
Tuples after Concatenaton:
(0, 1, 2, 3, 'Code', 'For', 'Life')
Slicing of Tuple
Slicing of a Tuple is done by fetching 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
# Slicing of a Tuple
# with Numbers
Tuple1
=
tuple
(
'CODEFORDUDLE'
)
# Removing First element
print
(
"Removal of First Element: "
)
print
(Tuple1[
1
:])
# Reversing the Tuple
print
(
"\nTuple after sequence of Element is reversed: "
)
print
(Tuple1[::
-
1
])
output :
Removal of First Element:
('O', 'D', 'E', 'F', 'O', 'R', 'D', 'U', 'D', 'L', 'E')
Tuple after sequence of Element is reversed:
('E', 'L', 'D', 'U', 'D', 'R', 'O', 'F', 'E', 'D', 'O', 'C')
Deleting a Tuple
Tuples are immutable and hence they do not allow deletion of a part of it. Entire tuple gets deleted by the use of del() method.
Note- Printing of Tuple after deletion results to an Error.
Lets see how
# Deleting a Tuple
Tuple1
=
(
0
,
1
,
2
,
3
,
4
)
del
Tuple1
print
(Tuple1)
Note- Printing of Tuple after deletion results to an Error.
output :
File "E:/program/tuple.py", line 4, in <module>
print(Tuple1)
NameError: name 'Tuple1' is not defined
#matplotlib
#python online
#python programming
#python list
#learn python
#python requests
#introduction to data science in python
#free python tutorial
#learn python basics
#python study
#python and ai
#python computer science
#learn python step by step
#best course to learn python
#learn python with projects
4 Comments
Informative and easy to understand..
ReplyDeleteNicely written. Very helpful for competitive programming.
ReplyDeletewill omit your great writing due to this problem.
ReplyDeleteonline training in python
online training on python
very useful content.....really liked it
ReplyDeletePython Online Course
Python Online Training in India
If you have any doubt, Please let me know.