Tuesday, 10 January 2023

Python Tuple

 

A Tuple in Python is a collection of ordered and immutable elements. These elements can be of any data type, including numbers, strings, and other objects.

Fruits = (“apple”,”grapes”,”mango”)

Marks = (67,89,34,56)

 

The most notable feature of tuples is that they are immutable, meaning that their elements cannot be modified once they have been assigned.

One of the main advantages of using tuples is that they are memory efficient and faster than lists. This makes them a great choice for storing large amounts of data, such as a list of coordinate points or a list of words.

Despite their immutability, tuples can still be manipulated with various built-in functions and methods.

 For example, the "len" function can be used to determine the number of elements in a tuple.

 marks = (34,56,78,23,90,34)

print(len(marks))

output: 6

The  "count" method can be used to count the number of occurrences of a specific element.

 print(marks.count(34))

output: 2

 To access an element of tuple ,we can use index , where index starts from 0.

marks[0]

output: 34

marks[3]

output :23

 

A tuple with 6 elements will have indices ranging from 0 to 5.An indexError will be raised if we access the index beyond the range.

 

To access the range of elements we can use slicing operator(:)

marks(1:3)

output: (56,34)


Amelioration

This article was researched and written with the help of ChatGPT, a language model developed by OpenAI."

 "Special thanks to ChatGPT for providing valuable information and examples used in this article."

 

No comments:

Post a Comment