Sequence data types include list, tuple, and range (see Sequence Types — list, tuple, range).
Other sequence data types may be added in the future because Python is a developing language.
Tuples
A tuple is a collection of ordered objects with the property of being unmodifiable once created. It's for this reason that they're referred to as "immutable lists".
There are two types of Python objects: changeable and immutable.
Immutable objects, as their name implies, can't be changed after they've been created.
Because the members of a tuple are surrounded between parentheses rather than square brackets, you can quickly distinguish it from a list:
A tuple is a collection of values separated by commas, such as:
One can also nest tuples.
One way to do this is listed below.
Tuples are immutable, this can be seen from the below example :
You will get this error :
TypeError: 'tuple' does not support item assignment on line 10 in main.py
But you can store mutable types :
Variable=([1,2,3,4,5],[6,7,8,9,10])
It is not feasible to assign to individual items in a tuple, however tuples containing mutable objects, such as lists, can be created.
Tuples are immutable, and they often contain a heterogeneous sequence of components that can be accessed using unpacking or indexing.
Lists are mutable, and their elements are usually homogeneous, and they can be accessed by iterating across them.
As demonstrated tuples are always wrapped in parenthesis. Here the output to ensure that nested tuples are correctly read.
Empty tuples
An empty pair of parentheses constructs an empty tuple.
Here only a tuple with one item is produced by following a value with a comma (it is efficient to enclose a single value in parentheses).
Tuple packing
Below example demonstrates tuple packing, here these values are packed together in a tuple. whilst the use of x,y,z variable demonstrates the case of unpacking.
The variables on the right side cause sequence unpacking.
When unpacking a sequence, the number of variables on the left side of the equals sign must equal the number of elements in the series.
Multiple assignment is nothing more than a combination of tuple packing and sequence unpacking.
Adding elements to tuple:
you'll get the below error:
if we use append or pop function on tuples:
AttributeError: 'tuple' object has no attribute 'append' on line 8 in main.py
AttributeError: 'tuple' object has no attribute 'pop' on line 8 in main.py
benefits of tuples are :
Tuples can be used to construct safer code since the data we don't want to modify is kept "write-protected" in an immutable tuple.
Tuples can be used instead of lists since they take up less memory, which can be important when using large datasets.
Operations using tuples are faster than operations involving lists.
Indexing
We can access any element in the sequences using an index that starts at zero since the elements are ordered.
We're looking at the first element of seqdata with the first index (0). The second index ('V') of the first element ('MRVLLVALALLA') refers to the 6th element ('V').
Slice notation can be used to choose a piece of a sequence. Slicing is
the process of cutting food into thin slices.
a colon separates two indexes (:). These indexes represent a location
between the elements in the existing space. The word "Python" can be
written as.
Sets
A set is a mathematical model for a collection of diverse things.
It contains elements or members, which can be any mathematical object: numbers, symbols, points in space, lines, other geometrical structures, variables, or even other sets.
Membership testing, duplication elimination, and the application of mathematical operations such as intersections, unions, differences, and symmetrical differences are some of the typical uses of sets.
It is also possible to create an empty set and then add the elements as needed:
As in list comprehension, we can also define a set using comprehension.
Union
Figure 10.1. Union in python.
The operator union (as shown in above Figure) is the union of two (or more) sets, and its abbreviated form is |:
Intersection
Figure 10.2. Intersection in python.
Difference
Figure 10.3. Difference in python.
Symmetric difference
Figure 10.4. Symmetric difference in python.
The parts that are not a part of the intersection are referred to as symmetric differences (see as mentioned in the above figure); the operator is symmetric difference, and it is abbreviated as ^ :
---- Summary ----
As of now you know how tuples work.
Create a simple tuple.
Create empty tuples.
Tuple packing.
Adding values to Tuple.
Working with sets
Sets union,intersection
Sets difference..etcs
Copyright © 2022-2023. Anoop Johny. All Rights Reserved.