
To create a Python Tuple in Python Programming, firstly we assign values to this tuple. In other words, we do Python Tuple Packing. In this lesson, we will do the reverse of this, Python Tuple Unpacking. With Tuple Unpacking, we will extract the values back into variables.
So, let’s firstly check do Python Tuple Packing. Here, we will add values to the tuple, we will create tuple. Out tuple example will include some of the characters of the lord of the rings.
You can also check Python Tuple Acccess
The output of this Python Tuple Packing code will be like below:
Now, let’s do Python Tuple Unpacking. We will do this with the below example.
The output of this code will be like below:
You can also wtach the video version of this lesson!
Here, the number of the variables and the number of the memebrs in tuple must be same. If these numbers are not same, then we can use asterisk (*). If we use this sign, then the remaining values are assigned same. Let’s do an example for this.
In the output of this Python Tuple Unpacking example code will be like below:
As you can see here, because of the fact that we use asterisk (*) with “elf”, the tuple members after it all defined as elf.
Like Python Tuple Packing, you can use Tuple Unpacking for different purposes. You can extract the values into variables and use them in your code.
Gokhan Kosem is a Network Engineer, Instructor and the Founder of IPCisco.com with 15+ years of experience in Cisco, Nokia, Huawei, Juniper, Linux, Service Provider Networks, Routing and Switching technologies.
He has worked on the backbone networks of major service providers and network vendors including Nortel, Alcatel-Lucent (Nokia) and has extensive hands-on experience with Cisco, Huawei, Juniper and Nokia networking technologies.
He has trained thousands of networking students worldwide through IPCisco.com, Udemy, books, labs, quizzes, and educational content across multiple social media platforms.
IPCisco.com | Best Route to Your Dreams
Leave a Reply