Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Python-tuple

python

Uploaded by

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

Python-tuple

python

Uploaded by

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

Python Tuple

Tuples in Python
 Tuples are used to store multiple items in a single variable.
 A tuple is a collection which is ordered and unchangeable.
 Tuples are written with round brackets.
 One cannot add items to a tuple once it is created.
 Tuples cannot be appended or extended.
 We cannot remove items from a tuple once it is created.
 Create Tuple With One Item

To create a tuple with only one item, you have to add a comma
after the item, otherwise Python will not recognize it as a tuple
Indexing in Python Tuple

 In order to access the tuple items refer to the index number.


 Use the index operator [ ] to access an item in a tuple. The index
must be an integer.
var = tuple((1,2,3,4))
print(var)
 Example:-
Tuple methods in Python

Method Description
count() Returns the number of times a specified value occurs in
a tuple
index() Searches the tuple for a specified value and returns the
position of where it was found
Len() of Tuple

To determine how many items a tuple has, use the len() function

 Example:-

 my_tuple = (“Hello", 10, True,90.8)


 print(len(my_tuple))
Tuple methods in Python

 Syntax of count function:-


my_tuple = (“Hello", 10, True,90.8)
print(my_tuple.count(10))

 Syntax of index function :-


my_tuple = (“Hello", 10, True,90.8)
print(my_tuple.index(90.8))
Operations in Tuples

 Concatenation Operation:-To join two or more tuples, + operator is used.


 Example:-
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
 Nesting of Python Tuples:-A nested tuple in Python means a tuple inside
another tuple.
 Example:-
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = (tuple1 , tuple2)
print(tuple3)
Operations in Tuples

 Repetition Python Tuples:-We can create a tuple of multiple


same elements from a single element in that tuple.
 Example:-
tuple1 = (“hello”,)*3
print(tuple1)
 Slicing Tuples in Python:-Slicing a Python tuple means
dividing a tuple into small tuples using the indexing method.
 Example:-
tuple1 = (1, 2, 3)
print(tuple1[:])
 print(tuple1[:1])

You might also like