Python Tutorial : Meet the Tuples
Key Takeaways
The video tutorial covers the basics of tuples in Python, including their creation, indexing, immutability, and tuple unpacking, as well as their use with the zip and enumerate functions.
Full Transcript
the next container top we want to learn about is the tuple tuples are widely used internally and many of the systems that we depend upon like databases tuples are very much like lists they hold data in the order we supply it and we can access the elements inside a tuple with an index however the similarities and their tuples are easier to process and more memory efficient than lists tuples are also immutable which means that 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 tuple unpacking to expand a tuple into named variables that represent each element of 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 I'll pass them to 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 the parentheses 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 name variables for later use this syntax allows us to create more readable 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 num1 and i in num1 so that i can print them by the name I start by putting both variables as the target of an assignment statement separated by a comma then I assign the first tuple and I'll talk pairs lists to them let's look at another great use case for tuple unpacking for me the place I use to pool unpacking the most is when working with loops we can use two pond packing 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 tip on packing helps us keep track of which element in the iterable or lists we are currently working on often we want to know what the index is of an element in the interval the enumerate function enables us to do that by creating tuples where the first element of the tuple is the index of the element in the original list and then the element itself is the second we can use this to track rankings in our data or skip elements were not interested in here I'm going to enumerate our top payers lists and split that into the resulting tuple into index and item I can also use tuple unpacking on the atom 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 a zip or enumerate or use the parentheses as shown here however the real magic for creating a two point Python is the comma if we accidentally in an assignment line with a comma we can create a tuple and this can have some very undesirable side effects further down in our code so keep this in mind if you get a tuple where you don't expect it
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/data-types-for-data-science-in-python 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
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: Python for Data
View skill →Related Reads
📰
📰
📰
📰
Step-by-Step HTML to DOCX Conversion Tutorial in Node.JS
Dev.to · Muhammad Mustafa
USING SQLITE IN GO AND IT'S IMPORTANCE
Dev.to · wairewaire
The Dependency Trap: The hidden cost of npm install
Medium · JavaScript
Iterators and Generators in JavaScript: Symbol.iterator, for…of, yield, and Lazy Evaluation
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI