filmov
tv
How to Create a Tuple in Python? (Constructor, Singleton) - Python Tutorial for Beginners

Показать описание
🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ How to Create a Tuple in Python? (Constructor, Singleton)
Unlike Lists in Python, Tuples are defined by enclosing the elements in parentheses () - instead of square brackets [].
my_tuple = ("Learn", "Python", "with", "Digital Academy", 1, 2, 3)
You can create a tuple by placing a comma-separated sequence of items, in parentheses. Thus, a tuple containing zero items is called an empty tuple, and you can create one with empty parentheses. The items of a tuple do not have to be the same type. For instance, the following tuples contain integers, strings, float, or boolean values.
# Empty tuple
my_tuple = ()
# A tuple of integers
my_tuple = (1, 2, 3)
# A tuple of strings
my_tuple = ('red', 'green', 'blue')
# A tuple with mixed data types
my_tuple = (1, 'abc', 1.23, True)
Syntactically, a tuple is just a comma-separated list of values. You do not necessarily need the parentheses to create a tuple. It is the trailing commas that really define a tuple. But, using parentheses does not hurt because they help make the tuple more visible.
# A tuple without parentheses
my_tuple = 1, 'abc', 1.23, True
○ Singleton Tuple in Python
Since parentheses are also used to define operator precedence in expressions, Python evaluates the expression 7 as simply the integer 7, and creates an int object. To tell Python that you really want to define a singleton tuple, with only one value, include a trailing comma "," - just before the closing parenthesis.
# Not a tuple
my_tuple = (7)
type(my_tuple) # type 'int'
# Tuple
my_tuple = (7,)
type(my_tuple) # type 'tuple'
○ The tuple() Constructor
It is also possible to use the tuple() constructor when you want to create a tuple. And, you can convert other data types to tuple, using Python’s tuple constructor as well.
my_tuple = tuple(("apple", "banana", "cherry"))
# Convert list into tuple
my_tuple = tuple([1, 2, 3])
# my_tuple = (1, 2, 3)
# Convert string into tuple
my_tuple = tuple('abc')
# my_tuple = ('a', 'b', 'c')
Let's play this video, stick around and watch until the end of this video! 👍🏻
- Digital Academy™ 🎓
***
☞ WATCH NEXT:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
🖥️ How to Create a Tuple in Python? (Constructor, Singleton)
Unlike Lists in Python, Tuples are defined by enclosing the elements in parentheses () - instead of square brackets [].
my_tuple = ("Learn", "Python", "with", "Digital Academy", 1, 2, 3)
You can create a tuple by placing a comma-separated sequence of items, in parentheses. Thus, a tuple containing zero items is called an empty tuple, and you can create one with empty parentheses. The items of a tuple do not have to be the same type. For instance, the following tuples contain integers, strings, float, or boolean values.
# Empty tuple
my_tuple = ()
# A tuple of integers
my_tuple = (1, 2, 3)
# A tuple of strings
my_tuple = ('red', 'green', 'blue')
# A tuple with mixed data types
my_tuple = (1, 'abc', 1.23, True)
Syntactically, a tuple is just a comma-separated list of values. You do not necessarily need the parentheses to create a tuple. It is the trailing commas that really define a tuple. But, using parentheses does not hurt because they help make the tuple more visible.
# A tuple without parentheses
my_tuple = 1, 'abc', 1.23, True
○ Singleton Tuple in Python
Since parentheses are also used to define operator precedence in expressions, Python evaluates the expression 7 as simply the integer 7, and creates an int object. To tell Python that you really want to define a singleton tuple, with only one value, include a trailing comma "," - just before the closing parenthesis.
# Not a tuple
my_tuple = (7)
type(my_tuple) # type 'int'
# Tuple
my_tuple = (7,)
type(my_tuple) # type 'tuple'
○ The tuple() Constructor
It is also possible to use the tuple() constructor when you want to create a tuple. And, you can convert other data types to tuple, using Python’s tuple constructor as well.
my_tuple = tuple(("apple", "banana", "cherry"))
# Convert list into tuple
my_tuple = tuple([1, 2, 3])
# my_tuple = (1, 2, 3)
# Convert string into tuple
my_tuple = tuple('abc')
# my_tuple = ('a', 'b', 'c')
Let's play this video, stick around and watch until the end of this video! 👍🏻
- Digital Academy™ 🎓
***
☞ WATCH NEXT:
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
♡ SUPPORT US ♡
***
Комментарии