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

Veröffentlicht am: 29 März 2021
auf dem Kanal: Digital Academy
695
14

🎓 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 -    • DATA TYPES in Python (Numbers, Strings, Li...  
○ Operators in Python -    • OPERATORS in Python (Arithmetic, Assignmen...  
○ IF Statements in Python -    • CONDITIONAL Statements in Python (IF, ELIF...  
○ FOR Loops in Python -    • FOR Loop in Python (Syntax, Break, Continu...  

📖 Blog: http://digital.academy.free.fr/blog

📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners:    • 🐍 Python 101: Learn Python Basics for Abso...  

📖 [PLAYLIST] Complete Python Development Course for Beginners: http://digital.academy.free.fr/playli...

🧑‍🎓 [COURSE] http://digital.academy.free.fr/courses

📘 [BOOK] Python for Absolute Beginners: https://amzn.to/3NvyOWV

🛒 Shopping and Discounts: http://digital.academy.free.fr/store

💌 Weekly Newsletter for Developers: https://www.getrevue.co/profile/digit...

#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/
✧   / digitalacademyy  
✧   / digitalacademyfr  
✧   / digital_academy_fr  
✧    / digitalacademyonline  

♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/store
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧   / digital_academy  
✧ https://www.buymeacoffee.com/digital_...

***


Auf dieser Seite können Sie das Online-Video How to Create a Tuple in Python? (Constructor, Singleton) - Python Tutorial for Beginners mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Digital Academy 29 März 2021 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 695 Mal angesehen und es wurde von 14 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!