Tuples

Tuples

  • Like List tuples are also ordered collection of data
  • Unchangeable
  • Itreable
  • Can be heterogeneous

In Python tuples are written with round brackets.

Creation of Tuple:

t = tuple()
print(type(t))

t1 = ()
print(type(t1))

t1 = (1,2,3,4,"Rushikesh",2.4)
print(type(t1))

#Return 
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>        

Note : Tuple with only one element

t2 = (2)
print(type(t2))

#Return -
<class 'int'>

********** We need to add , After first eleement *******
t2 = (2,)
print(type(t2))

Now it will return 
<class 'tuple'>        

Tuples Support Indexing ,slicing As List :

t1 = (1,2,3,4,"Rushikesh",2.4)
print(type(t1))
print(t1[1])
print(t1[1:4])

#Return - 
2
(2, 3, 4)        

When we try to change tuples element it will throw an error (Immutable/Unchangeable)

t1 = (1,2,3,4,"Rushikesh",2.4)
t1[4] = "Rahul"
print(t1)

#Return -
Traceback (most recent call last):
  File "C:\Users\Rushi Reddy\OneDrive\文档\Desktop\Python\Practice\DataStructure\tuples.py", line 5, in <module>
    t1[4] = "Rahul"
    ~~^^^
TypeError: 'tuple' object does not support item assignment        

Mutablility In Tuples:

Note :Tuples are immutable but list inside tuples are mutable we can change the list element

t1 = ([1,2,3,4],"Rushikesh")
print(t1)         #Return - ([1,2,3,4],"Rushikesh")
print(type(t1))   #Return <class 'tuple'>
print(type(t1[0]))#Rerurn <class 'list'>
## t1[0][1] is list element so we can the list item 
t1[0][1] = 3
print(t1)         #Return - ([1, 3, 3, 4], 'Rushikesh')        

Unpacking Tuples:

When we create a tuple, we normally assign values to it. This is called "packing" a tuple

But Python, we are also allowed to extract the values back into variables. This is called "unpacking".

t1 = ("Rushikesh","sanjay","jethure")
print(t1)
firstname,father_name,surname = t1
print(firstname)
print(father_name)
print(surname)

#Return - 
('Rushikesh', 'sanjay', 'jethure')
Rushikesh
sanjay
jethure        

Tuples Method :

1)count() : Returns the number of times a specified value occurs in a tuple

tup1 = (1,1,1,3,4,5,6,7)
print(tup1.count(1))
print(tup1.count(9))

#Return -
3
# 9 Is not in tuple so it will return -- 0        

2)index() : Searches the tuple for a specified value and returns the position of where it was found

tup1 = (1,1,1,3,4,5,6,7)
print(tup1.index(1,2,5))

#Return -
2        

Iteration:

tup1 = (1,1,1,3,4,5,6,7)
for i in tup1:
    print(i)

#Return -
1
1
1
3
4
5
6
7        

Concatenation:(+,*)

tup1 = (1,2,3,4,5)
tup2 = (6,7,8)
tup3 = tup1 + tup2
print(tup3)
print(tup3 * 2 )

# Return -
(1, 2, 3, 4, 5, 6, 7, 8)
(1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8)        

Convert Tuples into list and list into tuples:

If We have to change the tuples element we can convert tuple into list and we can chnage the element of that converted list and then we can again covert it into tuple

tup1 = (1,2,3,4,5)
print(type(tup1))
lst = list(tup1)
print(type(lst))
print(lst[2])
lst[2] = 100
print(lst)
tup2 = tuple(lst)
print(tup2)
print(type(tup2))

#Return -
<class 'tuple'>
<class 'list'>
3
[1, 2, 100, 4, 5]
(1, 2, 100, 4, 5)
<class 'tuple'>        

要查看或添加评论,请登录

Rushikesh J.的更多文章

  • Linux Operating System

    Linux Operating System

    Linux Operating System ======================= => Linux is a community based OS => Linux is free & Open Source => Linux…

  • Infrastructure

    Infrastructure

    ================= Infrastructure ================= Servers (Machines) Databases Storage Network Security Backup =>…

  • Application Architectural Patterns

    Application Architectural Patterns

    =================================== 1) Monolithic Architecture (Outdated) 2) Microservices Architecture (Trending)…

  • DevOps Introduction

    DevOps Introduction

    =============== What is DevOps =============== DevOps = Development + Operations => DevOps is a culture/process in IT…

    2 条评论
  • Try and Exception in Python

    Try and Exception in Python

    Exception is the error and is an event, which occur when program statements are executing. It disrupts the flow of…

  • Python Array With Examples

    Python Array With Examples

    Array in Python Array in Python Array is the collection of items having same data type with contiguous memory location.…

  • Python Date Object

    Python Date Object

    We can work with time and date with Python. There are two module in Python time and datetime that used to work with…

  • String Formatting WITH Problems and Solution

    String Formatting WITH Problems and Solution

    What is String Formatting? It is the way to insert custom string or variable in a text (string). Using string…

  • SET Different Methods With Examples

    SET Different Methods With Examples

    SET Method : add() Working: This method is used to add element to set. Syntax: set.

  • Python SET Comprehension With Examples

    Python SET Comprehension With Examples

    What is Comprehension? ?We create a sequence from a given sequence is called comprehension. ?Sequence means list…

社区洞察

其他会员也浏览了