tuple() function creates and returns a new tuple object. When used without an argument, it returns an empty tuple. When used with the optional
iterable argument, it initializes the new tuple with the elements in the iterable.
Read more about tuples in our full tutorial about Python Tuples.
Learn by example! Here are some examples of how to use the
tuple() built-in function:
You can create an empty tuple by skipping the argument:
>>> tuple() ()
If you pass an iterable—such as a list, another tuple, a set, or a dictionary—you obtain a new tuple object with tuple elements obtained from the iterable:
>>> tuple((1, 2, 3)) (1, 2, 3)
Note that it really creates a new tuple object that may be different from the one passed as an argument. However, this is not always the case:
>>> x = (1, 2, 3) >>> y = tuple(x) >>> x is y True >>> x == y True
The new tuple
y has the same elements as the original tuple
x. But as tuples are immutable—they cannot be changes—they both point to the same object in memory: as you can see from the check
x is y that returns
You can use the
tuple() method with or without the optional
Syntax: There are two ways of using the constructor:
tuple() -> new empty tuple, you'll probably never need that tuple(
iterable) -> new tuple initialized with elements in iterable
Interactive Shell Exercise: Understanding tuplw()
Consider the following interactive code:
Exercise: Guess the output before running the code.
But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
tuple() function creates and returns a new tuple object.
- When used without an argument, it returns an empty tuple.
- When used with the optional
iterableargument, it initializes the new tuple with the elements in the iterable.
>>> tuple() () >>> tuple((1, 2, 3)) [1, 2, 3]
I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:
Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.