0% found this document useful (0 votes)
14 views

Tuple 1

Python

Uploaded by

vaishnaviware005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Tuple 1

Python

Uploaded by

vaishnaviware005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

TUPLES

Tuples are used to store multiple items in a single variable.

Tuple is one of four-built-in data types in pythons use to store collection of data , the other three are list,set & dictionery.

ALL which different quality & usage.

Tuple is a collection which is ordered and unchangleble.

Tuples are written with round brackets ().

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.

Double-click (or enter) to edit

list create [a,b] access [1],[1:2] operation [insert,append,extend,remove("banana",pop[1],pop[],


list (Mutable-changeble,allow duplicate value, ordered) access (index,slice)

tuple (unmutable-unchangable,ordered,allow duplicate value)


typle create(a,b) access index[1],slice [1:2] operation (count(a),index(a))

set create access operation

dictionary create access add delete operation

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

Access Tuples Items

You can access tuple items by refering to the index number , inside square bracket.

NOTE: First item index is 0.

#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

Negative indexing means start from the end.

-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')

Display empty tuple

# 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")

print(animals[:2]) # By default start value is zero


('dog', 'cat')

# -3 -2 -1
animals=("dog","cat","giraffe","lion","tiger")

print(animals[-3:]) #start from -3 # by default end is the last value


('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

tuples are unchangble

# 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()

It returns the number of times , specifyed value occures in the tuple.

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"))

ValueError: tuple.index(x): x not in tuple

animals=("dog","cat","giraffe","lion","tiger","dog","tiger")

print(len(animals))
7

You might also like