Tuple

Tuples

- Tuples are the data types.

- Tuples are used to store multiple data in a single variable.

- Tuples are created using round brackets ().

- Tuples are unchangeable and allow duplicate values.

- In the Tuple, the first value has [0] index, and the second value has [1] index. (Index starts with 0)


Tuples with different Data Types Values:


A Tuple with String, Integer, Boolean Values:


Tuple Allow Duplicates Values:


Tuple type():

- The type() method Returns the datatype of cheems_tuple. (In the below code)

Syntax: type(variable_name)


Tuple len():

len() stands for length.

- The len() method Returns the length of cheems_tuple. (In the below code)

Syntax: len(variable_name)


Tuple Specific Single Value:

- In the Tuple, the first value has [0] index, and the second value has [1] index. (Index starts with 0)


Tuple Specific Multiple Values:

- In the Tuple, the first value has [0] index, and the second value has [1] index. (Index starts with 0)

- In the below code, [0:2] means Returns the values between 0 and 2 indexes.

[0:2]0 is included and 2 is excluded that's why python returns only and index values.

[0:3]0 is included and 3 is excluded that's why python returns only 0, 1, and 2 index values.



Change string in Tuple:

Syntax: tuple(a)

a should be another datatype variable to change into a tuple.

a is the string to change into a tuple. (In the below code)

- We can take any datatype to change into a tuple. Like- list[], set{}, dict{} etc.


Important Note:

- Tuple is unchangeable, If we want to change a specific value in a tuple or want to use these methods like- append(), insert(), remove(), pop(), clear() and delete a specific value in a tuple then we have to change the tuple in list[] or set{}

Change Tuple in list[] and set{}:

Syntax for change in list: list(variable_name)

Syntax for change in set: set(variable_name)



Tuple del:

del stands for delete.

del used to delete a tuple and return nothing.

del NOT support the delete specific value in a tuple by their position.

Delete a Tuple:

Syntax: del variable_name


Negative indexing in Tuple:

- Return the last value in a Tuple.

- In Tuple, [-1] index = Return the last value in a Tuple and [-2] index = Return the second last value in a Tuple .

Syntax: variable_name[-1]

Comments