What is a Python tuple?
A tuple is a built-in data type in python that is used to store multiple items under a single variable. Tuples are very similar to lists since they are ordered, and allow duplicate values however unlike lists they are immutable which means that they cannot be changed.
Creating a Python tuple
So, to create a tuple, we need a variable and we’re going to call it ‘t’ and we’re going to do equals and then now we need some values of which we are going to separate with a comma. Now, the first way that we can do this is just by typing in some values separated by a comma. So we’ll do 1, 2 and 3. Now, if I save this and hover over the variable the code editor tells us that this is a tuple. We can also check the type of the variable as shown below.
However, it’s worth noting that in most cases you will want to use brackets around the values to determine that it’s a tuple. So the best practice is to have parentheses enclosing those values contained in a tuple. As illustrated below.
Now, a tuple can have multiple values that are the same. But it could also have integers and strings put together. We can go ahead and change the value 2 the word ‘lion’.
We can also go ahead and write another tuple underneath. So we’ll call this one x is equal to and again with the brackets and we’ll just do 4, 6 and then ‘tiger’.
Now we have two tuples on the screen. If we want to print one of them out, we can just print(t) for instance. We’ll see down here that we get that back.
Indexing and slicing Python tuples
We said at the beginning of the video that tuples are very similar to lists, which means we can actually slice and index them. We are going to use the square brackets to denote what we want to index. So we want a specific item from this tuple. So if we do t[0], this should give us the first item from the tuple of t. And we can see that we get one back and if we could change this to 1 or 2 as shown below.
Adding two Python tuples
We can actually add these two tuples together to create one large tuple. So if we do print(t + x), we’re going to get all the values together. In one tuple here.
Nesting Python tuples
We can nest tuples together as well. And to do so we need to just write the two tuples side by side separated by a comma and then wrap the whole thing in brackets. We can print the whole tuple and we can also index the tuples as shown here.
So why would you use a tuple and not a list? Well, generally speaking, if you want some data that isn’t changeable or doesn’t need to have its order changed, you could use a tuple. And when we talk about larger amounts of data, python actually executes tuples much quicker.
If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
Summary
If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
