🎓 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:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho
☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw
📖 Blog: http://digital.academy.free.fr/blog/
📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners
🛒 Shopping and Discounts: http://digital.academy.free.fr/store/
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
✧ http://digital.academy.free.fr/
✧ https://twitter.com/DigitalAcademyy
✧ https://www.facebook.com/digitalacademyfr
✧ https://www.instagram.com/digital_academy_fr/
✧ https://www.youtube.com/c/DigitalAcademyOnline
♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧ https://www.patreon.com/digital_academy
✧ https://www.buymeacoffee.com/digital_academy
***