Computer Science, asked by upendra4069, 5 months ago

Explain types of tuples and Setsand their functions and methods in python?

Answers

Answered by saralaraib74
2

Explanation:

. Python Tuple

This Python Data Structure is like a, like a list in Python, is a heterogeneous container for items. But the major difference between the two (tuple and list) is that a list is mutable, but a tuple is immutable. This means that while you can reassign or delete an entire tuple, you cannot do the same to a single item or a slice.

To declare a tuple, we use parentheses.

>>> colors=('Red','Green','Blue')

a. Python Tuple Packing

Python Tuple packing is the term for packing a sequence of values into a tuple without using parentheses.

>>> mytuple=1,2,3, #Or it could have been mytuple=1,2,3

>>> mytuple

(1, 2, 3)

b. Python Tuple Unpacking

The opposite of tuple packing, unpacking allots the values from a tuple into a sequence of variables.

>>> a,b,c=mytuple

>>> print(a,b,c)

1 2 3

c. Creating a tuple with a single item

Let’s do this once again. Create a tuple and assign a 1 to it.

>>> a=(1)

Now, let’s call the type() function on it.

>>> type(a)

<class ‘int’>

As you can see, this declared an integer, not a tuple.

To get around this, you need to append a comma to the end of the first item 1. This

Similar questions