Tuple 1
Tuple 1
Tuple is one of four-built-in data types in pythons use to store collection of data , the other three are list,set & dictionery.
Tuple Items
Tuple items are ordered, unchangleble and allow duplicate values. Tuple items are indexed, the first item has indexed(0), second item has
indexed(1) etc.
When we say that tuples are ordered, it means that the items have defined order, and that order will not changed.
Tuples are unchangeble, meaning that we can not change , add or removed items after the tuple has been created.
Since tuples are indexed, they can have items with the same values.
type(())
tuple
# 0 1 2 3 4
animals=("dog","cat","cat","giraffe","lion")
report=("india",35,3,90,98.35)
print(type(animals))
print(type(report))
print(animals)
print(animals[3])
print(len(animals))
<class 'tuple'>
<class 'tuple'>
('dog', 'cat', 'cat', 'giraffe', 'lion')
giraffe
5
You can access tuple items by refering to the index number , inside square bracket.
#positive indexing
# 0 1 2 3
animals=("dog","cat","giraffe","lion")
print(animals[0])
print(animals[1])
print(animals[2])
dog
cat
giraffe
# 0 1 2 3 4 5 6
Tuple_one=("lion","cat","dog","tiger","34","56","34")
print("Animal list:",Tuple_one)
print(type(Tuple_one))
print(Tuple_one[4])
Animal list: ('lion', 'cat', 'dog', 'tiger', '34', '56', '34')
<class 'tuple'>
34
Negative indexing
-1 Refer to the last item -2 Refer to the second last item etc.
# -4 -3 -2 -1
animals=("dog","cat","giraffe","lion")
# 0 1 2 3
print(animals[-2])
print(animals[0:2]) #slice[start:stop:step] 0-1 stop value is excluded or not included
print(animals[:]) #slice[start:stop] #full tuple,entire tuple,all values in tuple
giraffe
('dog', 'cat')
('dog', 'cat', 'giraffe', 'lion')
# 0 1 2 3
animals=("dog","cat","giraffe","lion")
print(animals[:0]) #[start:stop] 0 means first endex position , start and stop are same
()
# 0 1 2 3 4
animals=("dog","cat","giraffe","lion","tiger")
# -3 -2 -1
animals=("dog","cat","giraffe","lion","tiger")
animals=("dog","cat","giraffe","lion")
print(animals[3])
print(animals[2])
print(animals[0])
lion
giraffe
dog
# 0 1 2 3 4 5
Quiz=("india","america","thailand","japan","china","austerlia")
# -6 -5 -4 -3 -2 -1
# 1 2 1 2 1 #1:6:2
# 1 2 3 1 2 #1:6:3
#Rule 1 : bydefault start 0
#Rule 2 : Bydefault end or stop --last item
#Rule 3: end is not included , its excluded
#Rule 4 : negative index startr from last -1
#Rule 5: start and stop is same then empty
#Rule 6 : Start empty and end 0 then empty
#Rule 7: by default step is 1
#print(Quiz[1:3]) # "america","thailand"
#print(Quiz[1:6]) # "america","thailand","japan","china","austerlia"
#print(Quiz[1:6:2]) #"america","japan","austerlia"
#print(Quiz[1:6:3]) #"america","china"
#print(Quiz[:]) #"india","america","thailand","japan","china","austerlia"
#print(Quiz[:0]) # ()
#print(Quiz[-4:]) # "thailand","japan","china","austerlia"
#print(Quiz[-5:-2]) #"america","thailand","japan"
#print(Quiz[-3]) # "japan"
print(Quiz[4]) #"china"
china
# 0 1 2 3 4 5
animal_list=("lion","cat","dog","tiger","34","56")
print(animal_list)
print(animal_list[0:4])
print(animal_list)
y=animal_list[0:4]
print(y)
('lion', 'cat', 'dog', 'tiger', '34', '56')
('lion', 'cat', 'dog', 'tiger')
('lion', 'cat', 'dog', 'tiger', '34', '56')
('lion', 'cat', 'dog', 'tiger')
id(animal_list)
138432083162208
id(y)
138432299963536
Methods
Python has two build in methods that you can use on tuple
count()
index()
Searches the tuple for a specified value and returns the position of where it was found.
animals=("dog","cat","giraffe","lion","tiger","dog","dog")
print(animals.count("dog"))
3
animal_list=("lion","cat","dog","lion","tiger","34","56")
print(animal_list.count("lion"))
2
index()
# 0 1 2 3 4 5
animals=("dog1","cat","giraffe","lion","tiger","dog")
print(animals.index("tiger"))
4
animals=("dog","cat","giraffe","lion","tiger","dog")
print(animals.count("dog"))
2
animal_list=("lion","cat","dog","tiger","34","56","cat","cat")
print(animal_list.count("cat"))
print(animal_list.index("cat"))
3
1
animals=("dog","cat","giraffe","lion","tiger","dog")
print(animals.count("cat"))
1
animals=("dog","cat","giraffe","lion","tiger","dog")
print(animals.count("lion"))
1
animals=("dog","cat","giraffe","lion","tiger","dog")
print(animals.index("lion"))
print(animals.index("camel"))
3
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-13-ae466703f076> in <cell line: 5>()
3 print(animals.index("lion"))
4
----> 5 print(animals.index("camel"))
animals=("dog","cat","giraffe","lion","tiger","dog","tiger")
print(len(animals))
7