Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Learn About Python Tuples Function [With Examples]

Updated on 19 November, 2024

7.35K+ views
6 min read

Tuples are sequences or a collection of objects separated by commas. They are similar to lists in many ways, except that the elements cannot be changed after they are created. And unlike lists, tuples in Python are immutable objects. Also, they use parentheses and not square brackets. 

Creating a tuple is as simple as placing values separated by commas, sometimes between parentheses. Here are some examples:

  • tup1 = ( ‘English’, ‘Hindi’, 1998, 2016)
  • tup2 = “c”, “d”, “e”, “f”
  • tup3 = (5, 6, 7, 8, 9)

As you can see, a tuple may have any number of elements, and they may be of different types – an integer, a list, a string, and so on. Using parentheses is optional, but considered a good practice to follow. Now, let us delve into the specifics. 

If you are a beginner and interested to learn more about data science, check out our data science certification from top universities.

Read about: Operators in Python

Tuples in Python

1. Creating a tuple

An empty tuple comprises two parentheses with nothing inside, i.e., (). Here’s how you create it:

empty _tup = ()

print (empty_tup)

#Output

()

Now, let’s see how we can create non-empty tuples. Creating tuples without parentheses is called tuple packing. 

tup=‘mouse’, ‘keyboard’

print(tup)

#Output

(‘mouse’, ‘keyboard’)

Alternatively, you can use parentheses for the same output. 

tup= (‘mouse’, ‘keyboard’)

print(tup)

#Output

(‘mouse’, keyboard’)

For a single-element tuple, merely putting the one constituent within parentheses would not work. You will have to include a trailing comma to indicate that it is a tuple. Consider the following example. 

tup=(50,)

2. Concatenation, Nesting, and Repetition

To concatenate two tuples in python, you can write the following code:

my_tup=(0,1,2,3,4,5)

your_tup=(‘hello’, ‘bye’)

print(my_tup + your_tup)

#Output

(0,1,2,3,4,5, ‘hello’, ‘bye’)

Below is the code for creating nested tuples:

tup1=(0,1,2)

tup2=(‘python’, ‘learn’)

tup3=(tup1, tup2)

print(tup3)

#Output

((0,1,2),(‘python’,’learn’))

To create a tuple with repetition, follow the steps given below:

new_tup=(‘hi’,)*4

print(new_tup)

#Output 

(‘hi’, ‘hi’, ‘hi’, ‘hi’)

On writing the above code without commas, you will get a string, hihihihi, as output for new_tup.

Read: Top 5 Python Modules

3. Accessing Tuples

To access values in tuple, you use square brackets with the index. Take the code below to test slicing. 

tuple=(0,1,2,3)

print(tuple[1:])

print(tuple[::-1])

print(tuple[2:4])

#Output

(1,2,3)

(3,2,1,0)

(2,3)

4. Dealing with immutability 

It is not possible to update or change the values of elements, but you can create new tuples by taking portions of existing tuples, as demonstrated in the example below.

tuple1=(‘ab’, ‘xy’)

tuple2=(13,14)

#action invalid for tuples

#tuple1[0]=50

#Creating a new tuple

tuple3=tuple1+tuple2

print tuple3

#Output

(‘ab’, ‘xy’, 13, 14)

Similarly, you cannot remove individual elements in tuples since they are immutable. However, you can put together another tuple to discard the undesired constituents. And you can remove the entire tuple by using the del statement explicitly.

tuple1=(‘January’, February’)

del tuple1

Also read: Python Developer Salary in India

upGrad’s Exclusive Data Science Webinar for you –

ODE Thought Leadership Presentation

Basic Tuple Operations 

There are various built-in tuple functions in python, such as len(), cmp(), max(), min(), and tuple(seq). Let us demonstrate their use one by one.

  • Finding length of a tuple

my_tuple = (‘upgrad’, ‘python’)

print(len(my_tuple))

#Output

2

  • Comparing elements

tup1 = (‘upgrad’,’python’)

tup2 = (‘coder’, 1)

if (cmp(tup1, tup2) != 0):

     # cmp() returns 0 if matched, 1 when not tup1 

    # is longer and -1 when tup1 is shorter

    print(‘Not the same’)

else:

    print(‘Same’)

#Output

Not the same 

  • Maximum and minimum values

print (‘Maximum element in tuples 1,2: ‘ + str(max(tup1)) + ‘,’ + str(max(tup2)))

print (‘Minimum element in tuples 1,2: ‘ + str(min(tup1)) + ‘,’ + str(min(tup2)))

#Output

Maximum element in tuples 1,2: upgrad,coder

Minimum element in tuples 1,2: python,1

You will observe that the max() and min() checks are based on ASCII values. In case of two strings in a tuple, python checks the first different characters in the strings.

  • Converting lists and strings into tuples

list1 = [0, 1, 2,3]

print(tuple(list1))

print(tuple(‘upgrad’)) # string ‘upgrad’

#Output

(0,1,2,3)

(‘u’, ‘p’, ‘g’, ‘r’, ‘a’, ‘d’)

Here, a single parameter, such as a list, string, set, dictionary key, is taken and converted into a tuple.

How to create a tuple in a loop

Now, let’s move on to creating tuples in a loop. You can follow the following python code to do it. 

tuple=(’python’,)

n=3 #Number of time the loop runs

or i in range (int(n)):

 tuple=(tuple,)

 Print tuple

#Output

((‘python’,),)

(((‘python’,),),)

((((‘python’,),),),)

As you can see, there are different ways of creating a tuple and iterating over it.

Advantages over lists

Lists and tuples in Python are typically used in similar situations. But tuples are preferred over lists due to a variety of reasons. Some of them are listed below.

  • Tuples are used for heterogeneous types of data. On the other hand, lists are more suitable for homogenous data types.
  • Tuples offer a performance boost as iterating through them is faster as compared to lists. This is attributable to their immutable nature.
  • You can go for tuple implementation to keep your data write-protected.
  • Immutable elements can be used as a dictionary key.

Conclusion

In this article, we understood all about tuples in Python, from what they are and how to create them to their different operations and benefits. This information will surely come handy as you move forward in your Python learning journey! 

If you are reading this article, most likely you have ambitions towards becoming a Python developer. If you’re interested to learn python & want to get your hands dirty on various tools and libraries, check out Executive PG Programme in Data Science.

Frequently Asked Questions (FAQs)

1. What are the characteristics of tuples in Python?

In Python, a tuple is an ordered collection of objects that cannot be changed. Here, the objects in Python could be anything like integers, tuples, lists, strings, etc. The insertion order is preserved as the display of output will be dependent upon the order in which elements are inserted. Tuples are immutable, and you cannot modify the objects once they are added to the tuple. Tuples can store both the same as well as different types of objects. Indexing plays a major role in tuples. You can store duplicates in tuples. You can use both positive and negative indexes in tuples, where positive index refers to forward direction and negative index refers to backward direction. You need to use a comma as a separator for separating objects in a tuple.

2. Are there built-in methods in tuples?

Yes, there are two built-in methods available for use in tuples. Tuples are immutable, and you are not allowed to change the objects in a tuple after adding them. A tuple object can call the two available built-in methods, which are count() and index(). count() will return the number of times any specific value occurs in the entire tuple. index() will search the entire tuple for the specified value and also return the position where that value had been found.

3. Why are tuples faster than lists in Python?

There are mutable and immutable objects in Python, where lists come under mutable ones, and tuples come under immutable ones. Tuples are stored in a single block of memory, and there is no requirement for extra space for storing new objects.
On the other hand, lists are allocated in two blocks, where one block stores the object information and the other block has a variable size for adding new data. This is the main reason behind tuples being faster than lists in Python. This is another reason why indexing is faster in tuples as compared to lists.