In continuation to our previous blog on python list we are going to deal with python set and tuple what is the usage and advantage of these in python programming.
You may refer to previous tutorial to get the basic knowledge on python list implementation because tuple is similar to that of list.
Python Set and Tuple are two built-in data structures in Python with different characteristics and use cases.
A Set is an unordered collection of unique elements. Sets are commonly used when we want to store unique values and perform operations like union, intersection, difference, etc. The syntax for creating a set is to enclose a comma-separated sequence of elements within curly braces {}.
A Tuple is an ordered collection of elements that can be of different types. Tuples are immutable, which means that once they are created, their values cannot be modified. Tuples are commonly used when we want to store a sequence of values that should not be changed, such as coordinates or database records. The syntax for creating a tuple is to enclose a comma-separated sequence of elements within parentheses ().
Python set and tuple video tutorial :
Go through the below tutorial for detailed implementation of set and tuple in python programming.
Tuple ( ):
Tuple is a immutable so that you cannot add data into it rather you can make use of the data added through the index position and fetch them.
Just as like list tuples are also used to store multiple items into a single variable but then the difference is it’s immutable.
And also the advantage of tuple is it’s faster than the list, because as they are immutable.
Tuple can be created by curved brackets ( ) providing the elements in between them by comma separated .
example :
tuple = ( 20, 13, 78, 56 )
now specify
‘ tuple ‘ output is printed below.
( 20, 13, 78, 56 )
and now making use of indexing position as shown below
tuple[ 3 ]
56
tuple [ 1 ]
13
Now try to find count in tuple as shown below
tuple.count( 78 )
1
Here 78 is present only once in list so 1 is printed.
let’s try to add repeated values and find count
tuple = ( 2, 4, 3, 2, 1 )
tuple.count( 2 )
2
Set { }:
Set is also a list kind of representation but it has it’s own rules such that values cannot be repeated that is uniqueness is maintained.
set = { 5, 3, 2, 4, 7, 4 }
set
{ 2, 3, 4, 5, 7 }
The list may be appearing as sorted but no it’s not sorted it changes every time so the concpet of indexing is not available.
Set follows hashing mechanism for faster processing of values.
In set data is un-ordered, no index and duplicate values. You may use set according to requirement because of these features.
Python Set and Tuple Usage :
Sets are used when we want to store unique elements and perform set operations, while Tuples are used when we want to store a sequence of values that should not be changed.
If you have any query’s in this tutorial on python set and tuple do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates..