tuple() Constructor in Python Last Updated : 10 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Python, the tuple() constructor is a built-in function that is used to create tuple objects. A tuple is similar to a list, but it is immutable (elements can not be changed after creating tuples). You can use the tuple() constructor to create an empty tuple, or convert an iterable (such as a list, string, or range) into a tuple.Example: Python # Example t1 = tuple() # creates an empty tuple t2 = tuple([1, 2, 3]) # converts a list into a tuple t3 = tuple("Python") # converts a string into a tuple print(type(t1), t1) print(type(t2), t2) print(type(t3), t3) Output<class 'tuple'> () <class 'tuple'> (1, 2, 3) <class 'tuple'> ('P', 'y', 't', 'h', 'o', 'n') Syntax of tuple()tuple([iterable])iterable: This is an optional argument. If provided, tuple() will convert the iterable (like a list, string, or range) into a tuple.If no iterable is passed, it creates an empty tuple ().Use of Tuple() ConstructorThe tuple() constructor is mainly used to:Create an empty tuple.Convert other iterables, such as lists, strings, ranges, etc., into tuples.Let's see some examples:Convert a Range to a TupleThe range() function creates a range object, and we can use tuple() to convert it into a tuple. Python t = tuple(range(5)) print(t) Output(0, 1, 2, 3, 4) Convert a List Comprehension to a TupleHere, we are converting a tuple from a generator expression, which squares each number in the range from 0 to 4. Python t = tuple(x * x for x in range(5)) print(t) Output(0, 1, 4, 9, 16) Creating Nested TuplesYou can also create nested tuples using the tuple() constructor. Here's an example where we create a tuple of tuples, forming a 2D structure. Python mat = tuple([tuple(range(3)) for _ in range(3)]) print(mat) Output((0, 1, 2), (0, 1, 2), (0, 1, 2)) Shallow Copy of a Tuple Using tuple()Since tuples are immutable, you cannot change their elements once created. However, you can create a shallow copy of a tuple by using the tuple() constructor. This might be useful when you want to duplicate a tuple object. Python t1 = ([1, 2], [3, 4]) t2 = tuple(t1) t2[0][0] = 10 # Modifies the list inside tuple print(t1) # Output: ([10, 2], [3, 4]) Output([10, 2], [3, 4]) Explanation: In this case, although b and b1 are separate tuples, the lists inside them are not copied, so changes to the lists inside b1 will reflect in b. Comment More infoAdvertise with us Next Article tuple() Constructor in Python P pragya22r4 Follow Improve Article Tags : Python Python Programs Practice Tags : python Similar Reads Class as Decorator in Python While function-based decorators are widely used, class-based decorators can offer more flexibility and better organization, especially when the decorator needs to maintain state or requires multiple methods to function properly. A Python class decorator is simply a class that implements the __call__ 3 min read Tuple within a Tuple in Python In the realm of Python programming, tuples offer a flexible solution for organizing data effectively. By allowing the inclusion of other tuples within them, tuples further enhance their capabilities. In this article, we will see how we can use tuple within a tuple in Python. Using Tuple within a Tup 4 min read Python Super() With __Init__() Method In object-oriented programming, inheritance plays a crucial role in creating a hierarchy of classes. Python, being an object-oriented language, provides a built-in function called super() that allows a child class to refer to its parent class. When it comes to initializing instances of classes, the 4 min read Different Forms of Assignment Statements in Python We use Python assignment statements to assign objects to names. The target of an assignment statement is written on the left side of the equal sign (=), and the object on the right can be an arbitrary expression that computes an object. There are some important properties of assignment in Python :- 3 min read Memory Management in Lists and Tuples using Python In Python, lists and tuples are common data structures used to store sequences of elements. However, they differ significantly in terms of memory management, mutability, and performance characteristics. Table of ContentMemory Allocation in ListsMemory Allocation in TuplesComparison of Lists and Tupl 3 min read Python | Repeating tuples N times Sometimes, while working with data, we might have a problem in which we need to replicate, i.e construct duplicates of tuples. This is an important application in many domains of computer programming. Let's discuss certain ways in which this task can be performed. Method #1 : Using * operator The mu 5 min read Python OOPs Exercise Questions Ready to level up your Python object-oriented programming skills? Explore our collection of Python OOP exercises, packed with over 25 engaging problems to help you master core concepts like encapsulation, inheritance, polymorphism and abstraction. Letâs turn theory into practice!Python OOPs Practice 9 min read Python for Kids - Fun Tutorial to Learn Python Programming Python for Kids - Python is an easy-to-understand and good-to-start programming language. In this Python tutorial for kids or beginners, you will learn Python and know why it is a perfect fit for kids to start. Whether the child is interested in building simple games, creating art, or solving puzzle 15+ min read Built-In Class Attributes In Python Python offers many tools and features to simplify software development. Built-in class attributes are the key features that enable developers to provide important information about classes and their models. These objects act as hidden gems in the Python language, providing insight into the structure 4 min read What Does Super().__Init__(*Args, **Kwargs) Do in Python? In Python, super().__init__(*args, **kwargs) is like asking the parent class to set itself up before adding specific details in the child class. It ensures that when creating an object of the child class, both the parent and child class attributes are initialized correctly. It's a way of saying, In 4 min read Like