Explain four buit-in tuple functions python with example.


Question: Explain four buit-in tuple functions python with example.

Certainly! Here are four built-in tuple functions in Python along with examples:


1. `len()`: This function returns the number of elements in a tuple.


```python

my_tuple = (1, 2, 3, 4, 5)

length = len(my_tuple)

print(length)  # Output: 5

```


2. `count()`: This function returns the number of occurrences of a specified value in a tuple.


```python

my_tuple = (1, 2, 2, 3, 4, 2)

count = my_tuple.count(2)

print(count)  # Output: 3

```


3. `index()`: This function returns the index of the first occurrence of a specified value in a tuple.


```python

my_tuple = (1, 2, 3, 4, 5)

index = my_tuple.index(3)

print(index)  # Output: 2

```


4. `sorted()`: This function returns a new tuple with the elements of the original tuple sorted in ascending order.


```python

my_tuple = (5, 3, 1, 4, 2)

sorted_tuple = tuple(sorted(my_tuple))

print(sorted_tuple)  # Output: (1, 2, 3, 4, 5)

```


These functions provide useful operations for working with tuples in Python, allowing you to determine the length, count occurrences, find indices, and sort elements.


Disclaimer

All information provided on this site is generated by artificial intelligence. If you find any content objectionable or have concerns about the information provided, please feel free to comment or contact us directly.

Rjwala Rjwala is your freely Ai Social Learning Platform. here our team solve your academic problems daily.

Related Posts

0 Komentar

Post a Comment

let's start discussion

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Latest Post