forked from chavarera/PythonScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.OperationOnTuple.py
More file actions
50 lines (32 loc) · 1.32 KB
/
2.OperationOnTuple.py
File metadata and controls
50 lines (32 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
##---------------------------------------------------------------------------
#Created By:Ravishankar Chavare
#version:python 3.7
#Date:13/03/2019
#File_des:Some Simple Operation on Tuple
##----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Adding Element into Tuple
#-----------------------------------------------------------------------------
#tuples are immutable, so you can not add new elements
#using merge of tuples with the + operator you can add an element and it will create a new tuple
tpx=(1,2,3,4,5,6,7)
#add 9
tpx=tpx+(9,)
print(tpx)
#Unlike Python lists, tuples does not have methods such as append(), remove(), extend(), insert() and pop() due to its immutable nature.
#Assigning Multiple Values
a = (1,2,3)
(one,two,three) = a
print(one)
#------------------------------------------------
#For Removing Element From Tuple
#-----------------------------------------------
#because of immutable structure we can not directly remove element from tuple here we can use list
converted_list=list(tpx)
#now we can use pop(),remove(), method to remove element
#Remove 1 from tuple
converted_list.remove(1)
#after removing element convert list to tuple
final_tuple=tuple(converted_list)
#print(tuple)
print(final_tuple)