🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ How to Pack a Tuple and Unpack a Tuple in Python?
○ Tuple Packing in Python
When a tuple is created, the items in the tuple are packed together into an object. In this example, the values ‘red’, ‘green’, ‘blue’ and ‘yellow’ are packed all together, into a tuple.
my_tuple = ('red', 'green', 'blue', 'yellow')
my_tuple = ('red', 'green', 'blue', 'yellow')
○ Tuple Unpacking in Python
When a packed tuple is then unpacked, the individual items are unpacked and assigned to the items you defined. In the below example, the tuple "my_tuple" is unpacked into the variables 'red', 'green', 'blue' and 'yellow' so you can access each one of them individually.
my_tuple = ('red', 'green', 'blue', 'yellow')
(red, green, blue, yellow) = my_tuple
print(red) # red
print(green) # green
print(blue) # blue
print(yellow) # yellow
Nota: When unpacking, the number of variables on the left MUST match the number of items in the tuple.
○ Common Errors while Unpacking a Tuple in Python
Here are some very common errors during tuple unpacking - too many arguments, or not enough values to unpack!
my_tuple = ('red', 'green', 'blue', 'yellow')
(red, green) = my_tuple
ValueError: too many values to unpack
my_tuple = ('red', 'green', 'blue')
(red, green, blue, extra) = my_tuple
ValueError: not enough values to unpack (expected 4, got 3)
○ Unpack Tuple without Storing its Values (_)
Eventually, you may also want to unpack only some values from your tuple, but do not store all of its items inside each variable. Fortunately, Python offers this feature, using "_". This way, each item is mapped into a new (placeholder) variable when it has one affected, and store its value so you can access it later.
Do not store the value yellow
my_tuple = ('red', 'green', 'blue', 'yellow')
red, green, blue, _ = my_tuple
---
Awesome! But, you do not have any idea when using packing and unpacking methods might be useful for you? Here are a few examples in which you may want to pack and unpack a tuple.
Tuple unpacking comes handy when you want to swap values of two variables, without using a temporary variable.
a = 1
b = 99
Swap values
a, b = b, a
print(a) # 99
print(b) # 1
While unpacking a tuple, the right side can be any kind of sequence: tuple, string or list. Typically, you may also apply any method on your variable that will return this kind of sequence, then proceed with unpacking. For instance, this example will split a string representing an email address into its user and domain, so you can access them.
Split an email address
addr = 'digital.academy@free.fr'
user, domain = addr.split('@')
user = digital.academy
domain = free.fr
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, String...
○ Operators in Python - • OPERATORS in Python (Arithmetic, Assi...
○ IF Statements in Python - • CONDITIONAL Statements in Python (IF,...
○ FOR Loops in Python - • FOR Loop in Python (Syntax, Break, Co...
📖 Blog: http://digital.academy.free.fr/blog
📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners: • 🐍 Python 101: Learn Python Basics for...
📖 [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_...
***
In questa pagina del sito puoi guardare il video online How to Pack and Unpack a Tuple in Python? (+ ValueError) - Python Tutorial for Beginners della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Digital Academy 30 marzo 2021, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 3,113 volte e gli è piaciuto 60 spettatori. Buona visione!