learn kro favicon img

Python TUPLE

In Python, a tuple is an immutable sequence data type that can contain elements of different types. Tuples are similar to lists, but they are defined using parentheses instead of square brackets.

Here is an example of defining a tuple in Python:

You can access the elements of a tuple using indices, just like with a list. For example:

You can also use the len function to get the length of a tuple. For example:

To pack variables into a tuple, you can use the tuple function. For example:

To unpack a tuple into variables, you can use the assignment operator. For example:

You can compare tuples using the comparison operators. For example:

You can slice a tuple using the slice notation. For example:

To delete a tuple, you can use the del statement. For example:

It’s important to note that once a tuple is deleted, it cannot be accessed or used in any way.

In Python, you can use tuples as keys in dictionaries, as long as the elements of the tuple are immutable. For example:

Leave a Comment

Your email address will not be published. Required fields are marked *