-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTuple.py
57 lines (39 loc) · 1.16 KB
/
Tuple.py
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
48
49
50
51
52
53
54
55
56
57
'''
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.
'''
my_tuple = ("mango",10,"apple",50)
print(my_tuple)
print(len(my_tuple))
print(type(my_tuple)) #<class 'tuple'>
my_tuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(my_tuple[2:5]) #('cherry', 'orange', 'kiwi')
my_tuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(my_tuple[-4:-1]) #('orange', 'kiwi', 'melon')
'''
----Change Tuple Values----
Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.
You can convert the tuple into a list, change the list, and convert the list back into a tuple
'''
my_tuple = ("mango","apple","banana")
x = list(my_tuple)
print(type(x)) #<class 'list'>
x.insert(1,"Jackfruit")
x = tuple(my_tuple)
print(my_tuple) #('mango', 'apple', 'banana')
print(type(my_tuple)) #<class 'tuple'>
'''
----Methods---
append()
extend()
clear()
insert()
remove()
pop()
copy()
index()
count()
sort()
reverse()
'''