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
- Integers:
- Strings:
- Heterogeneous data:
( consisting of strings, integers, and floats)
- Boolean:
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 🙂
The Data Monk Interview Books – Don’t Miss
Now we are also available on our website where you can directly download the PDF of the topic you are interested in. At Amazon, each book costs ~299, on our website we have put it at a 60-80% discount. There are ~4000 solved interview questions prepared for you.
10 e-book bundle with 1400 interview questions spread across SQL, Python, Statistics, Case Studies, and Machine Learning Algorithms – Ideal for 0-3 years experienced candidates
23 E-book with ~2000 interview questions spread across AWS, SQL, Python, 10+ ML algorithms, MS Excel, and Case Studies – Complete Package for someone between 0 to 8 years of experience (The above 10 e-book bundle has a completely different set of e-books)
12 E-books for 12 Machine Learning algorithms with 1000+ interview questions – For those candidates who want to include any Machine Learning Algorithm in their resume and to learn/revise the important concepts. These 12 e-books are a part of the 23 e-book package
Individual 50+ e-books on separate topics
Important Resources to crack interviews (Mostly Free)
There are a few things which might be very useful for your preparation
The Data Monk Youtube channel – Here you will get only those videos that are asked in interviews for Data Analysts, Data Scientists, Machine Learning Engineers, Business Intelligence Engineers, Analytics managers, etc.
Go through the watchlist which makes you uncomfortable:-
All the list of 200 videos
Complete Python Playlist for Data Science
Company-wise Data Science Interview Questions – Must Watch
All important Machine Learning Algorithm with code in Python
Complete Python Numpy Playlist
Complete Python Pandas Playlist
SQL Complete Playlist
Case Study and Guesstimates Complete Playlist
Complete Playlist of Statistics