Author - StudySection Post Views - 291 views
datatype

Tuple datatype in python

A tuple is a built-in datatype in python and it is used to store multiple items in a single variable. It is one of the data types in python which is used to store collections of data.

A tuple is a collection that is ordered and unchangeable means we can not change items after declaring them in the tuple. Tuples are written with round brackets.

example:- fruit=(“apple”,” banana”,” mango”) this is a tuple.

Tuple items are ordered, unchangeable, and also allow duplicate values.
Tuple items are indexed like the first item has index [0], the second item has index [1], etc.
In tuple, we can not change the order of items once the tuple is created. To find the length of a tuple we can use the length function with syntax len(tuple name).

example:-
fruit= (“apple”, “banana”,”mango”)
print(len(fruit))
Output:- 3

If we want to make a tuple with a single item we can do like this
fruit=(“apple”,)

* Comma, after the item is, must in single item tuple otherwise it will make it as a string.
In tuple, we can store any data type (integer, string, boolean)

fruit=(“apple”, “banana”,”mango”) #string
num=(1,2,3,4,5,6) #integer

We can also make tuple with different data types in a single variable
mix=(“apple”, 23, name, ”age”)

We can also do the unpacking of tuple tuples but here we need to have n number of variables for n number of data.
Eg: x,y=(1,2)
Where x=1 and y=2
#run loop into tuple
Suppose
x=(1,2,3,4,5,6)
For i in x:
print(i)

#count how many times a particular variable appears in the tuple

x=(1,2,7,34,,3,1,23,45,1)
print(x.count(1))

## answer would be -> 3

People having good knowledge of Financial accounting can get an Accounting Certification Exam from StudySection to increase their chances of getting a job in this field. You can get a foundation level certification if you are new to Financial accounting or you can go for advanced level certification if you have expert level skills in Financial accounting.

Leave a Reply

Your email address will not be published.