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 del Statement
Python String find()
Node.js vs Python for Backend Development
Python Set issubset()
Python Program to Append to a File
Python Program to Check if a Number is Odd or Even
Python Objects and Classes
Python Matrices and NumPy Arrays
Python Dictionary items()
Python type()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program Read a File Line by Line Into a List
Python Tuple index()
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Dictionary get()
Python Data Types
Python String rsplit()
Python Program to Print Colored Text to the Terminal
How to Get Started With Python?
Python Program to Solve Quadratic Equation
Python timestamp to datetime and vice-versa
Python if...else Statement
Python String splitlines()
Python String islower()
Python enumerate()
Python Function Arguments
Python Type Conversion and Type Casting
Python Program to Count the Number of Digits Present In a Number
Python String title()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python Dictionary popitem()
Python List extend()