fbpx

What is tuple in python, Understanding Tuples easily

First, of all what is a tuple in python? so in order to answer this question, we have first to know how many data types are there in python? Good these questions well help us very well. Know that there are 5 types of data, each type has subtypes. meaning that A tuple is a sequence.  we can say it’s similar to a list that contains a sequence of elements of any data type. while strings are sequences of characters.

How much Data types are there in python?

One thing you have to know is that when we want to write a tuple. we write it using parenthesis () instead of square brackets [].

example: chars = (‘a’, ‘b’, ‘c’)

Why we need tuple in python?

Use tuple when you want an element to be unchangeable. Know that tuple is immutable meaning it’s unlike lists. one good examples when we use tuple in python.

Go and build a function in python. def sum_div(number):  Then create to variables. twice = number*2
… div = number/2 .  return twice, div.  Now:   sum(10) should be(20, 5.0). the tuple here is the output which is the value of sum_div function.

Great Now check if the output is tuple or No. use type() function to do it. first, create a variable result = sum_div(10). Here you give result final value of the function. Use type function to check what data type is the result is. write type(result) then you will see <class ‘tuple’>.

Storing names and name addresses of people is one of the common uses of a tuple in python.

 

 

Scroll to Top