-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay27.py
More file actions
63 lines (46 loc) · 1.4 KB
/
Day27.py
File metadata and controls
63 lines (46 loc) · 1.4 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Tuple Operations/Manipulating tuple
# Tuples are immutable, if you want to add,change tuple items,
# Then you must convert the tuple to a list.
# Then perform operations on that list and convert it back to tuple.
countries = ("Spain", "India", "USA", "England")
temp = list(countries)
temp.append("Russia")
temp.pop(3)
temp[2] = "Finland"
countries = tuple(temp)
print(countries)
# Examples of tuples
languages = ('Python', 'Swift', 'C++')
# access the first item
print(languages[0]) # Python
# access the third item
print(languages[2]) # C++
# Python Tuple Length
cars = ('BMW', 'Tesla', 'Ford', 'Toyota')
print('Total Items:', len(cars))
# Iterate Through a Tuple
fruits = ('apple', 'banana', 'orange')
# iterate through the tuple
for fruit in fruits:
print(fruit)
vegetables = ('carrot', 'Potato', 'Spinnach')
# iterate through the tuple
for vegetables in vegetables:
print(vegetables)
# Accessing Elements
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[0]) # Output: 1
print(my_tuple[1:3]) # Output: (2, 3)
# Concatenation
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
concatenated_tuple = tuple1 + tuple2
print(concatenated_tuple) # Output: (1, 2, 3, 4, 5, 6)
# Repetition
tuple1 = ('a', 'b')
repeated_tuple = tuple1 * 3
print(repeated_tuple) # Output: ('a', 'b', 'a', 'b', 'a', 'b')
# Membership Test
my_tuple = (1, 2, 3, 4, 5)
print(3 in my_tuple) # Output: True
print(6 in my_tuple) # Output: False