Question 30: Write a Python program to find the repeated items of a tuple. Download hole Program / Project code, by clicking following link: Write syntax to copy specific elements existing tuple into new tuple ? To copy specific elements from an existing tuple into a new tuple in Python, you can use slicing to create a new tuple that contains the desired elements. Here's the syntax:
new_tuple = old_tuple[start_index:end_index] - new_tuple: This is the new tuple that will contain the copied elements.
- old_tuple: This is the existing tuple from which you want to copy elements.
- start_index: The index of the first element you want to copy (inclusive).
- end_index: The index of the element just after the last element you want to copy (exclusive).
Here's an example:
old_tuple = (1, 2, 3, 4, 5)
new_tuple = old_tuple[1:4]
print(new_tuple)
Output:
(2, 3, 4)
In this example, we copied elements from old_tuple starting from index 1 (inclusive) to index 4 (exclusive) and created a new tuple new_tuple containing those elements. Programming Code: Following code write in: BP_P30.py # Find repeated items of a Tuple
# create tuple
tuple1 = (1, 3, 4, 6, 5, 4, 2, 7, 9, 10, 7, 4, 4)
print("Tuple is: ", tuple1)
count1 = tuple1.count(7) # count() function used to count a number repeated no.
print("7 repeated: ", count1)
count2 = tuple1.count(4)
print("4 repeated: ", count2)
# Thanks for Reading.
Output:
Write syntax to copy specific elements existing tuple into new tuple ?
To copy specific elements from an existing tuple into a new tuple in Python, you can use slicing to create a new tuple that contains the desired elements. Here's the syntax:
new_tuple = old_tuple[start_index:end_index]
new_tuple = old_tuple[start_index:end_index]
- new_tuple: This is the new tuple that will contain the copied elements.
- old_tuple: This is the existing tuple from which you want to copy elements.
- start_index: The index of the first element you want to copy (inclusive).
- end_index: The index of the element just after the last element you want to copy (exclusive).
Here's an example:
old_tuple = (1, 2, 3, 4, 5)
new_tuple = old_tuple[1:4]
print(new_tuple)
Output:
(2, 3, 4)
In this example, we copied elements from old_tuple starting from index 1 (inclusive) to index 4 (exclusive) and created a new tuple new_tuple containing those elements.
old_tuple = (1, 2, 3, 4, 5)
new_tuple = old_tuple[1:4]
print(new_tuple)
Output:
(2, 3, 4)
In this example, we copied elements from old_tuple starting from index 1 (inclusive) to index 4 (exclusive) and created a new tuple new_tuple containing those elements.
# Find repeated items of a Tuple
# create tuple
tuple1 = (1, 3, 4, 6, 5, 4, 2, 7, 9, 10, 7, 4, 4)
print("Tuple is: ", tuple1)
count1 = tuple1.count(7) # count() function used to count a number repeated no.
print("7 repeated: ", count1)
count2 = tuple1.count(4)
print("4 repeated: ", count2)
# Thanks for Reading.
Output:
0 Comments