What is Tuple in python | tuple | Python tutorial in urdu Hindi | python tutorial | learn Python

Publicado el: 25 septiembre 2023
en el canal de: Property Consultant
21
0

A tuple in Python is a data structure that is used to store an ordered collection of elements. Unlike lists, which are also used to store collections of elements, tuples are immutable, which means their elements cannot be changed after creation. Tuples are typically used to group related pieces of data together.

Here's a step-by-step explanation of tuples in Python:

Creating a Tuple:
You can create a tuple by enclosing a comma-separated sequence of values within parentheses. For example:

python
Copy code
my_tuple = (1, 2, 3)
You can also create a tuple without parentheses by simply separating the values with commas:

python
Copy code
my_tuple = 1, 2, 3
Both of these examples create a tuple with three elements, 1, 2, and 3.

Accessing Elements:
You can access individual elements of a tuple using indexing, just like with lists. The index starts from 0 for the first element. For example:

python
Copy code
first_element = my_tuple[0] # Accesses the first element (1)
Similarly, you can access other elements by changing the index.

Slicing:
You can also slice a tuple to extract a portion of it. Slicing allows you to create a new tuple that contains a subset of the elements. For example:

python
Copy code
sub_tuple = my_tuple[1:3] # Creates a new tuple with elements 2 and 3
Slicing returns a new tuple, so the original tuple remains unchanged.

Tuple Packing and Unpacking:
You can pack multiple values into a tuple in a single statement. For example:

python
Copy code
person = ("John", 30, "Engineer")
Here, we've packed the name, age, and occupation of a person into a tuple.

You can also unpack a tuple into individual variables:

python
Copy code
name, age, occupation = person
This assigns the values from the person tuple to the name, age, and occupation variables.

Tuple Immutability:
Tuples are immutable, which means you cannot change their elements after creation. For example, the following code would result in an error:

python
Copy code
my_tuple[0] = 4 # This will raise a TypeError
To modify a tuple, you need to create a new tuple with the desired changes.

Tuple Methods:
Tuples have a few built-in methods, such as count and index. count returns the number of times a value appears in the tuple, and index returns the index of the first occurrence of a value.

python
Copy code
my_tuple = (1, 2, 2, 3, 4, 2)
count_2 = my_tuple.count(2) # Returns 3
index_3 = my_tuple.index(3) # Returns 3


in this video i am going to explain tuples data structure in python
#python
#pythontutorial


En esta página del sitio puede ver el video en línea What is Tuple in python | tuple | Python tutorial in urdu Hindi | python tutorial | learn Python de Duración hora minuto segunda en buena calidad , que subió el usuario Property Consultant 25 septiembre 2023, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 21 veces y le gustó 0 a los espectadores. Disfruta viendo!