Python Tuples

JK1184 
Created at Jun 15, 2023 09:13:38
Updated at May 09, 2024 16:08:31 
79   0   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 Share on Facebook Share on X

◀ PREVIOUS
Python Comparison Operators
▶ NEXT
Python Sets
  Comments 0
Login for comment
SIMILAR POSTS

Python Sets (updated at May 09, 2024)

Python Dictionaries (updated at May 09, 2024)

Python Comparison Operators (updated at May 10, 2024)

Python Arithmetic Operators (created at Jun 14, 2023)

Python Lists (updated at May 10, 2024)

Python String Operations (updated at May 10, 2024)

Python Conditions and If statements (updated at May 09, 2024)

Python Data Types (updated at May 10, 2024)

Python While Loops/For Loops (updated at May 09, 2024)

Python Functions (updated at May 09, 2024)

Python Variables (updated at May 10, 2024)

Python Comments (updated at May 10, 2024)

Python Syntax (updated at May 10, 2024)

Python Lambda (updated at May 09, 2024)

Python Arrays (updated at May 09, 2024)

Python Classes/Objects (updated at May 09, 2024)

Python Getting Started (updated at May 10, 2024)

Python Introduction (created at Jun 12, 2023)

What is Python? (updated at Jan 04, 2024)

Python Inheritance (updated at May 09, 2024)

Python Iterators (updated at May 09, 2024)

Python Polymorphism (updated at May 09, 2024)

Python Scope (updated at May 09, 2024)

Python Modules (updated at May 09, 2024)

Code Chronicles - A Caffeine-Fueled Journey into Data Software Engineering (updated at May 12, 2024)

Machine Learning Types and Programming Languages (updated at Nov 29, 2023)

Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship (updated at May 10, 2024)

Mastering Excel Data Manipulation with Python (updated at Apr 26, 2024)

OTHER POSTS IN THE SAME CATEGORY

Python example to download webpage (updated at May 15, 2024)

Python Tutorials for AP Computer Science Principles, Data Projects and High School Internship (updated at May 10, 2024)

Python Modules (updated at May 09, 2024)

Python Scope (updated at May 09, 2024)

Python Polymorphism (updated at May 09, 2024)

Python Iterators (updated at May 09, 2024)

Python Inheritance (updated at May 09, 2024)

Python Classes/Objects (updated at May 09, 2024)

Python Arrays (updated at May 09, 2024)

Python Lambda (updated at May 09, 2024)

Python Functions (updated at May 09, 2024)

Python While Loops/For Loops (updated at May 09, 2024)

Python Conditions and If statements (updated at May 09, 2024)

Python Dictionaries (updated at May 09, 2024)

Python Sets (updated at May 09, 2024)

Python Comparison Operators (updated at May 10, 2024)

Python Arithmetic Operators (created at Jun 14, 2023)

Python Lists (updated at May 10, 2024)

Python String Operations (updated at May 10, 2024)

Python Data Types (updated at May 10, 2024)

Python Variables (updated at May 10, 2024)

Python Comments (updated at May 10, 2024)

Python Syntax (updated at May 10, 2024)

Python Getting Started (updated at May 10, 2024)

Python Introduction (created at Jun 12, 2023)

What is Python? (updated at Jan 04, 2024)

UPDATES

Aiming for Congressional Award Gold? Your Registration Guide! (created at May 19, 2024)

Digital SAT what you should know (updated at May 17, 2024)

Reflections on a Year of Volunteering at Torrance Wilson Park: Season in 2023-2024 (updated at May 16, 2024)

ChatGPT Reset command and Ignore the Previous Response feature to have a Solid Result (updated at May 16, 2024)

Python example to download webpage (updated at May 15, 2024)

Impact in the theird party cookie on web browser (updated at May 15, 2024)

Why Nike Card Wallets Are a Smart Buy for the Stylishly Practical (updated at May 15, 2024)

How to Rip a Leg that hundreds of people succeeded in (updated at May 15, 2024)

Little Johnny Kang's Taekwondo Journey at the Kukkiwon in 2016 (created at May 15, 2024)

IU's Life Snapshots (updated at May 15, 2024)

Java Tutorials associated with AP Computer Science A (updated at May 15, 2024)

Java Getting Started (updated at May 15, 2024)

Java Syntax (updated at May 15, 2024)

Java Comments (updated at May 15, 2024)

Java Variables (updated at May 15, 2024)

Java Math (updated at May 15, 2024)

Java If ... Else (updated at May 15, 2024)

Java Short Hand If...Else (Ternary Operator) (updated at May 15, 2024)

Java Switch Statements (updated at May 15, 2024)

Loading XML Data with JavaScript (created at May 15, 2024)

Finding Comfort in the Chaos: My LOL Journey on US Servers (updated at May 14, 2024)

Java While Loop/Do While Loop/For Loop/For-Each Loop/Break/Continue (updated at May 13, 2024)

Java Arrays (updated at May 13, 2024)

Meet NewJeans Danielle: The Rising Star with Killer Dance Moves! (updated at May 13, 2024)

IU's 5.16MHz with UAENA (updated at May 12, 2024)

Microsoft Windows commands frquently used (updated at May 12, 2024)

Key Features of the Apache License (updated at May 12, 2024)

Naver Papago - multilingual machine translation service (updated at May 12, 2024)

What is Sitemap? Why do we need it? (updated at May 12, 2024)

What is a smart TV? (updated at May 12, 2024)

Semantic Network - a method of expressing knowledge based on a mesh structure (updated at May 12, 2024)

What is the role of README.md at GitHub/GitLab Repository? (updated at May 12, 2024)

Google Map Link Logic (updated at May 12, 2024)

Flying side kick - Run → Jump → Kick (updated at May 12, 2024)

Las Vegas KÀ by Cirque du Soleil that showed us an exciting fantasy (updated at May 12, 2024)

Physics Areas and Formulas (updated at May 12, 2024)

Statistics Formulas (updated at May 12, 2024)

PreCalculus Formulas for Trigonometry and Math Analysis (updated at May 12, 2024)

Differential/Integral Calculus Formulas (updated at May 12, 2024)

Mean(Averange) and Median based on Global Wealth per Person (updated at May 12, 2024)

Sneaker Style: Perfect Shoes to Wear with Jeans (updated at May 12, 2024)

YouTube Multiview Video Games Dataset from UC Irvine Machine Learning Repository (updated at May 12, 2024)

How to tie your Taekwondo Belt? (updated at May 12, 2024)

Taekwondo Tornado Kick Tutorial - one of my favorite kicks (updated at May 12, 2024)

Exploring the Heartfelt History and Traditions of Valentine's Day (updated at May 12, 2024)

Resell Price $9,400 - Apple Vision Pro Goes Off Right After Launch (updated at May 12, 2024)

Clack Attack - Embracing the Joyful Chaos of Mechanical Keyboards (updated at May 12, 2024)

Indulging in Delight: Waffles and Long Drinks in Seoul, Korea (updated at May 12, 2024)

A House Tour Adventure in the California, US (updated at May 12, 2024)

Base camp for house hunting in Anaheim near Disney Land (updated at May 12, 2024)