Tuple in Python | Python for Data Science | Day 3
Hello, Python fam!
Let’s get started with an essential concept in Python i.e. Tuples. Like all programming languages, Python uses buckets i.e. data structures to hold data for further computation. Tuple is Python’s exclusive data structure that is immutable and supportive of comma-separated heterogeneous data.
Don’t get dazed by the image above. Tuples are pretty easy to comprehend. Stay with us for the rest of the article and you will find out. Firstly, the basic syntax of tuple is:
name_of_the_tuple= ( val1, val2, val3, ………)
The clouds of doubt fall with code. So, without further ado, let’s head straight to see some instances of tuple.
A tuple can consist of
- Heterogeneous data:
( consisting of strings, integers, and floats)
Can we store a single object in a tuple?
The answer is yes. But there’s a little trick here.
The type function is used to find the type of a variable. So, when we declare a tuple with only one element, why is the type displayed as string and not a tuple?
Type in the line of code below and you will get to know.
Here the type is tuple because of the comma that follows the first element. With this example, you now know that whenever you need to declare a tuple with a single element, the element should be followed by a comma.
Let’s try another line of code.
We can access elements of a tuple by referring them through index numbers, starting from 0.
But can we alter the tuple elements by using their index numbers? Let’s find out.
As you must have observed above, tuple objects do not support item assignment.
Now, let’s try adding two tuples together. Try this-
You can also visit https://www.w3schools.com/python/python_tuples.asp for some more examples.
Now, that we have learned a few things about tuples, it’s time to end this tutorial. But before that, let’s delete the tuple.
The error suggests that the tuple is deleted successfully.
If you tried the above examples of tuples, bravo! You must have got a good taste of our very first data structure in this series. If you haven’t yet, open your favorite IDE, and try them now!
When you are done, try these questions to test your knowledge.
We have also answered the questions for your convenience. If you are stuck do not refrain from checking out the solutions. Happy learning 🙂
We have covered 40+ complete Data Science company interviews from the candidates who cracked these interviews.
Data Science Companies interview questions
We also have 30+ e-books on Amazon, Insta Mojo and books which can be delivered directly on your email address
Complete Set of e-books from The Data Monk
Understand some of the very complex topics in Analytics which are asked in most of the interviews
The Data Monk Top Articles
How to become a Data Scientist? Complete study material, free resources and websites to practice
Become a Data Scientist
Make your profile on our website and practice at least 5-7 questions per day. Be a part of ~2000 Analytics expert
Intern | The Data Monk