Ad Code

Responsive Advertisement

Basic Python Program - 30

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]
  1. new_tuple: This is the new tuple that will contain the copied elements.
  2. old_tuple: This is the existing tuple from which you want to copy elements.
  3. start_index: The index of the first element you want to copy (inclusive).
  4. 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:

Post a Comment

0 Comments

Ad Code

Responsive Advertisement