Python Tuples

JK JK 2018 
Created at
Updated at  
7,165 0 0

Tuples are used to store multiple items in a single variable.

Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage.

A tuple is a collection which is ordered and unchangeable.

Tuples are written with round brackets.

mytuple = ("apple", "banana", "cherry")

Tuple Items

Tuple items are ordered, unchangeable, and allow duplicate values.

Tuple items are indexed, the first item has index [0], the second item has index [1] etc.


Ordered

When we say that tuples are ordered, it means that the items have a defined order, and that order will not change.


Unchangeable

Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created.


Allow Duplicates

Since tuples are indexed, they can have items with the same value:

thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)

Tuple Length

To determine how many items a tuple has, use the len() function:

thistuple = ("apple", "banana", "cherry")
print(len(thistuple))

Create Tuple With One Item

To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.

thistuple = ("apple",)
print(type(thistuple))

#NOT a tuple
thistuple = ("apple")
print(type(thistuple))

Tuple Items - Data Types

Tuple items can be of any data type:

tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)

A tuple can contain different data types:

tuple1 = ("abc", 34, True, 40, "male")

type()

From Python's perspective, tuples are defined as objects with the data type 'tuple':

mytuple = ("apple", "banana", "cherry")
print(type(mytuple))

The tuple() Constructor

It is also possible to use the tuple() constructor to make a tuple.

thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)

Unpacking a Tuple

When we create a tuple, we normally assign values to it. This is called "packing" a tuple:

fruits = ("apple", "banana", "cherry")

But, in Python, we are also allowed to extract the values back into variables. This is called "unpacking":

fruits = ("apple", "banana", "cherry")

(green, yellow, red) = fruits

print(green)
print(yellow)
print(red)

Using Asterisk*

If the number of variables is less than the number of values, you can add an * to the variable name and the values will be assigned to the variable as a list:

fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")

(green, yellow, *red) = fruits

print(green)
print(yellow)
print(red)

If the asterisk is added to another variable name than the last, Python will assign values to the variable until the number of values left matches the number of variables left.

fruits = ("apple", "mango", "papaya", "pineapple", "cherry")

(green, *tropic, red) = fruits

print(green)
print(tropic)
print(red)

Loop Through a Tuple

You can loop through the tuple items by using a for loop.

thistuple = ("apple", "banana", "cherry")
for x in thistuple:
  print(x)

Loop Through the Index Numbers

You can also loop through the tuple items by referring to their index number.

Use the range() and len() functions to create a suitable iterable.

thistuple = ("apple", "banana", "cherry")
for i in range(len(thistuple)):
  print(thistuple[i])

Using a While Loop

You can loop through the tuple items by using a while loop.

Use the len() function to determine the length of the tuple, then start at 0 and loop your way through the tuple items by referring to their indexes.

Remember to increase the index by 1 after each iteration.

thistuple = ("apple", "banana", "cherry")
i = 0
while i < len(thistuple):
  print(thistuple[i])
  i = i + 1

Join Two Tuples

To join two or more tuples you can use the + operator:

tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3)

Multiply Tuples

If you want to multiply the content of a tuple a given number of times, you can use the * operator:

fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2

print(mytuple)

Tuple Methods

Python has two built-in methods that you can use on tuples.

MethodDescription
count()Returns the number of times a specified value occurs in a tuple
index()Searches the tuple for a specified value and returns the position of where it was found


 Below YouTube content is helpful for better understanding:

 

Tags Dictionary List Python Python Tuples Set Tuple Unpacking a Tuple Facebook X
Comments 0
Similar posts
  1. Python Sets
    7,090
  2. Python Dictionaries
    7,053
  3. Python Comparison Operators
    7,237
  4. Python Arithmetic Operators
    7,074
  5. Printing string n times
    7,065
  6. String concatenation by join()
    7,129
  7. Python Lists
    7,335
  8. Python String Operations
    7,406
  9. Python Conditions and If statements
    7,128
  10. Python Data Types
    7,433
  11. Python While Loops/For Loops
    7,504
  12. Python Functions
    7,062
  13. Python Variables
    7,395
  14. Python Comments
    7,533
  15. Python Syntax
    7,405
  16. Python Lambda
    7,101
  17. Python Arrays
    7,085
  18. Python Classes/Objects
    7,466
  19. Python Getting Started
    7,453
  20. Python Introduction
    7,100
  21. What is Python?
    7,723
  22. Python Inheritance
    7,185
  23. Python Iterators
    7,104
  24. Python Polymorphism
    7,086
  25. Python Scope
    7,062
  26. Python Modules
    7,097
  27. Code Chronicles - A Caffeine-Fueled Journey into Data Software Engineering
    7,361
  28. Machine Learning Types and Programming Languages
    7,230
  29. Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship
    7,888
  30. Mastering Excel Data Manipulation with Python
    7,067
  31. Challenge: One Code Problem Per Day
    1,093
  32. Clean Python Environments: The Power of venv vs. Docker
    826
  1. Python example to download webpage
    8,591
  2. Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship
    7,888
  3. Python Modules
    7,097
  4. Python Scope
    7,062
  5. Python Polymorphism
    7,086
  6. Python Iterators
    7,104
  7. Python Inheritance
    7,185
  8. Python Classes/Objects
    7,466
  9. Python Arrays
    7,085
  10. Python Lambda
    7,101
  11. Python Functions
    7,062
  12. Python While Loops/For Loops
    7,504
  13. Python Conditions and If statements
    7,128
  14. Python Dictionaries
    7,053
  15. Python Sets
    7,090
  16. Python Comparison Operators
    7,237
  17. Python Arithmetic Operators
    7,074
  18. Printing string n times
    7,065
  19. String concatenation by join()
    7,129
  20. Python Lists
    7,335
  21. Python String Operations
    7,406
  22. Python Data Types
    7,433
  23. Python Variables
    7,395
  24. Python Comments
    7,533
  25. Python Syntax
    7,405
  26. Python Getting Started
    7,453
  27. Python Introduction
    7,100
  28. What is Python?
    7,723
Recently updated
  1. Watch This Dazzling Dancer’s Mind-Blowing Moves
    49
  2. Michael Jackson's Billie Jean
    131
  3. Bootstrap vs. Tailwind CSS: Origins, Features, Pros & Cons, and How to Choose the Right Framework
    139
  4. The Complete Guide to Golang: History, Features, Real-World Uses, and Code Examples
    418
  5. Telemetry vs. Analytics: Understanding the Difference and Why It Matters
    236
  6. The Evolution and Production Reality of Agentic AI
    214
  7. How to Activate or Waive Your UIUC Student Health Insurance
    297
  8. Complete Guide to Building a Machine Learning Model
    325
  9. My life cuts at Las Vegas during Thanksgiving day holiday
    7,407
  10. The Cybercab Transformation: From Autonomous Taxi to Mobile Base Station
    354
  11. Harness vs. OpenClaw: Two Very Different "Agents"
    979
  12. Clean Python Environments: The Power of venv vs. Docker
    826
  13. What is Docker? Why is Docker also useful in a development environment?
    621
  14. UIUC 2026-2027 Academic Calendar
    1,597
  15. How to Build Llama 3 AI Apps with Python: Setup & User Prompts
    810
  16. Open-Source LLMs: The AI Revolution
    761
  17. Resume 2.0: Leveling Up for My First Software Gig
    2,229
  18. Not everyone will understand what this man just did
    1,888
  19. UIUC Dorm Guide: Find Your Perfect Fit !!
    1,596
  20. Unpacking IU's Shopper
    769
  21. Jackie Chan's Police Story: The Action Masterpiece
    664
  22. The IVE Story: Identity, 'I AM' Charts, and Influence
    968
  23. Tech Visionaries who graduated at UIUC - You are the Next Turn
    1,187
  24. Open Databases for Sex Crime Occurrences in the U.S.
    718
  25. Automatically copy text to the clipboard when dragging the mouse in the Cursor
    2,653
  26. My First Day at University of Illinois-Urvana Champaign
    1,197
  27. Sand, Sea, and a Splash of Fun at Newport Beach: A Family Adventure
    8,134
  28. Sun, Rocks, and Adventure: A Day at Joshua Tree National Park
    8,216
  29. Sipping the Stars: My Starbucks Adventure
    9,659
  30. Exciting explore at Sequoia National Park
    7,664
  31. My Life Shot at Death Valley
    1,713
  32. Ip Man fights with Muay Thai Master
    944
  33. Mad Clown - Don't Die
    1,027
  34. How to get Student Enrollment and Degree Verification at UIUC
    4,906
  35. LAX Thanksgiving Rush: A Joyful Reunion
    925
  36. ZO ZAZZ(조째즈) - Don`t you know (모르시나요) (PROD.ROCOBERRY)
    1,131
  37. FISHINGIRLS Unleashes Energetic EP 'Funiverse' Featuring Signature Track 'Fishing King'
    987
  38. 10CM - To Reach You (너에게 닿기를)
    1,165
  39. Feeling weak? Transform yourself at the UIUC ARC!
    1,573
  40. BOYNEXTDOOR - If I Say I Love You
    1,188
  41. The Future of Software Engineer - AI Engineering
    932
  42. G Dragon x Taeyang (Eyes Nose Lips, Power, Home Sweet Home, GOOD BOY) - LE GALA PIÈCES JAUNES 2025
    919
  43. Lie - Legend song by BIGBANG
    7,820
  44. Why ROLLBACK is useful when you work with Google Gemini CLI?
    843
  45. Reimbursement after Vaccination at McKinley Health Center
    1,023
  46. Gemini CLI makes a Magic! Time to speed up your app development with Google Gemini CLI!
    968
  47. Common Questions from UIUC school life in terms of CS Program
    1,125
  48. UIUC Immunization Compliance
    1,213
  49. LEE CHANHYUK's songs really resonate with my soul - Time Stop! Vivid LaLa Love, Eve, Endangered Love ...
    1,099
  50. LEE CHANHYUK - Endangered Love (멸종위기사랑)
    1,096