Python Tuple Tutorial with Examples
Python Tuple Tutorial with Examples, Welcome to the world of Tuples in Python Tutorial. Are you the one who is looking forward to knowing the Tuples in Python? Or the one who is very keen to explore the Tuples in Python with Examples that are available? Then you’ve landed on the Right path which provides the standard information of Tuples in Python.
Here, one can learn what is Python tuple, how to create a tuple in Python, nested tuples, repeating tuples and tuple operations in python. If you are the one who wanted to become an expert in Python? Or the one who wants to explore the technology like a Pro under the certified experts with world-class training environment, then asks your Python training institute experts who offer Advanced advanced advanced Python training. Follow the below-mentioned Python strings tutorial with examples and enhance your skills to become a professional Python Developer.
What is Python Tuple?
A tuple is an immutable ordered collection of elements in python enclosed with round brackets. The difference between tuples and lists is, a tuple is immutable means we can’t change tuple like a list. Lists are created with square brackets whereas tuples are created using parentheses.
How to Create a Tuple in Python?
Creating Tuples: To create tuple we just have to put values with separated commas in round brackets. tup1 = (1,2,3,6,7,8); we can also put strings in tuple as follows: tup2 = (‘green’,’yellow’,’red’); Or we can put string with double inverted commas as : tup3 = “green”, “red”, “yellow”;
The empty tuple can be created with two parentheses having no value:
tup12 = ();
To create the tuple with the single value we have to write comma after that value although it is having no further values.
test = (15,);
Tuples start with index ‘0’. They can be manipulated by slicing, concatenating and further processes like lists.
Repeating Tuples N times:
If we want to repeat the string ‘n’ times then we can do it as follows:
It will give a continuous string as output.
If we want separate strings as output we have to write
(‘Stats’, ‘stats’, ‘stats’, ‘stats’)
Nested Tuples in Python
Nested tuples can be created as follows:
tup1 =(0, 4,8,12)
tuplnest =(tup1, tup2)
(0, 4, 8, 12,’tuple’,’python’)
Immutable tuple in Python
Tuples are immutable, unlike lists. The elements in tuple can’t be edited.
Let’s see one example:
tup =(0, 1, ‘x’, ‘y’)
TypeError: ‘tuple’ object does not support item assignment.
Slicing Tuple in Python
In tuple slicing we can select the part of tuple as output , defined by starting and ending index as follows:
# We get output in exact reverse order.
Tuple Operations in Python
Deleting an item from Tuple:
We can delete the tuple by using simply del command
tup1=( 0, 1,’a’,’b’)
For confirmation, if we again print it will give an error
NameError: name ‘tup1’ is not defined
Length of Tuple:
To find the length of tuple we use function len()
Use of count()
To find the count of a particular element in tuple this function is used.
Use of index()
It returns index of specified element in tuple.
Converting a list to a Tuple
To convert a list into tuple following method can be used
If we take a single string and we applied the same method as above, we get a tuple having all separated characters.
Ex) print(tuple(‘python tuple’)
(‘p’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’, ‘ ‘, ‘t’, ‘u’, ‘p’, ‘l’, ‘e’, ‘s’)
Note: Here a space is also considered as character.
To compare tuples
For comparison purpose cmp(), min(), max() are used.
Let’s consider following tuples
To find minimum out of these tuples we use.
To find maximum out of these tuples we use.
Checking for an element in Tuple
For this, we have to use the keyword ‘in’. It will give output in the form of True or False.
3 in test
‘x’ in test
We hope you understand Python tuples tutorial with examples concepts. Get success in your career as a Python Developer by being a part of the Prwatech, India’s leading Python training institute in Bangalore.