Want to learn more? Take the full course at https://learn.datacamp.com/courses/da... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
The next container type we want to learn about is the tuple. Tuples are widely used internally many of the systems we depend on like databases.
Tuples are very much like lists they hold data in the order, and we can access elements inside a tuple with an index. However, the similarities end there. Tuples are easier to process and more memory efficient than lists. Tuples are immutable, which means we can't add or remove elements from them. This is powerful because we can use them to ensure that our data is not altered. We can create tuples by pairing up elements. Finally, we can use something called unpacking to expand a tuple into named variables that represent each element in the tuple. Let's explore these features...
Often, we'll have lists where we want to match up elements into pairs, and the zip function enables us to do that. Here I've got a list for the most popular cookies in the US and India, and I want to build a list of pairs by the popularity rank of the cookie in each country and I'll pass them the zip function. Then I can print the result of my zip and you can see I have what looks like a list of Tuples. It's really an iterator, but that's for a different course. Notice that the tuples use a parathesis as their object representation. Now let's look at how we can use unpacking with a tuple. Tuple unpacking, also sometimes called tuple expansion, allows us to assign the elements of a tuple to named variables for later use. This syntax allows us to create more readable and less error-prone code. Here I have a tuple containing the top-ranked cookie from both countries, and I want to store them as us_num_1 and in_num_1. So that I can print them by name. I start by putting both variables as the target of the assignment statement separated by a comma. Then assign the first tuple in our top_pairs list to them. Let's look at another great use case for tuple unpacking.
For me, the place I use tuple unpacking the most is when working with loops. We can use tuple unpacking when declaring the for loop, to separate a list of tuples into their elements as we loop over them. This sounds a bit strange, but let's take a look at it. Here I'm building a for loop that uses tuple unpacking when iterating over the top_pairs list. It splits each tuple in the list into its Indian and US cookie elements. We then use each of these variables to print the cookies in order. Another use of tuple unpacking helps us keep track of which element in the iterable or list we are currently on.
Often we want to know what the index is of an element in the iterable is. The enumerate function enabled us to do that by creating tuples were the first element of the tuple is the index of the element in the original list, then the element itself. We can use this to track rankings in our data or skip elements we are not interested in. Here I'm going to enumerate our top pairs list and split that resulting tuple into idx and item. I can also use tuple unpacking on the item to get all three components separately. This can be exceptionally powerful. Let's look at a bit of responsibility that comes with this power.
When we are creating tuples, we can make them with zip or enumerate or use parentheses as shown here. However, the real magic for creating a tuple in Python is the comma, if we accidentally end a line with a comma, we can create a tuple! This can have some very undesirable side effect further down in our code. Keep this in mind if you get a tuple where you don't expect it.
#DataCamp #PythonTutorial #DataTypesforDataScienceinPython
Auf dieser Seite können Sie das Online-Video Python Tutorial : Meet the Tuples mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer DataCamp 22 März 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 2,161 Mal angesehen und es wurde von 8 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!