Table of Contents
The tuple() builtin can be used to create tuples in Python.
In Python, a tuple is an immutable sequence type. One of the ways of creating tuple is by using the tuple()
construct.
The syntax of tuple()
is:
tuple(iterable)
1. tuple() Parameters
- iterable (optional) – an iterable (list, range, etc.) or an iterator object
If the iterable is not passed to tuple()
, the function returns an empty tuple.
2. Example: Create tuples using tuple()
t1 = tuple() print('t1 =', t1) # creating a tuple from a list t2 = tuple([1, 4, 6]) print('t2 =', t2) # creating a tuple from a string t1 = tuple('Python') print('t1 =',t1) # creating a tuple from a dictionary t1 = tuple({1: 'one', 2: 'two'}) print('t1 =',t1)
Output
t1 = () t2 = (1, 4, 6) t1 = ('P', 'y', 't', 'h', 'o', 'n') t1 = (1, 2)
Recommended reading: Python Tuples
Related posts:
Python @property decorator
Python Multiple Inheritance
Python Program to Find the Sum of Natural Numbers
Python Program to Check Whether a String is Palindrome or Not
Python String ljust()
Python File I/O Operation
Python Program to Check If Two Strings are Anagram
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Deep Learning in Python - LazyProgrammer
Python chr()
Python Errors and Built-in Exceptions
Python Program to Differentiate Between del, remove, and pop on a List
Python Dictionary copy()
Python String partition()
Python reversed()
Python Program to Check if a Number is Odd or Even
Python Set union()
Python List count()
Python Program to Create a Countdown Timer
Python Program to Get the File Name From the File Path
Python abs()
Python Program to Catch Multiple Exceptions in One Line
Python Program to Display Fibonacci Sequence Using Recursion
Python Keywords and Identifiers
Python isinstance()
Python open()
Python Input, Output and Import
Python Program to Calculate the Area of a Triangle
Python while Loop
Python Program to Print Colored Text to the Terminal
Python Directory and Files Management
Python String lstrip()