6

DATATYPES: TUPLES, LISTS & SETS

by

The Education Machine

[support@theeducationmachine.com]

Tuple Datatype

Tuple is also one of the immutable datatype.
Tuples are declared using circular parenthesis ().
As these are immutable, one cannot alter tuples such as append, insert or remove.
Now onwards, start focusing on the parenthesis that are being used.


As mentioned above, a tuple is declared using circular braces().

  nums = (1,2,3)
  type(nums)


Output:
tuple

To store same value in multiple variables at one time. But the ouput is integer as tuple is a collection of elements and single element will either be a interger or string.

  tup1 = tup2 = tup3 = 1
  print(type(tup1), type(tup2), type(tup3))


Output:
<class 'int'> <class 'int'> <class 'int'>

To store same value in multiple variables at one time using commas, it will throw an error.

  tup1, tup2, tup3 = 1

reserved_keywords


To store multiple values in multiple variable at the same time.

  tup1, tup2, tup3 = 1,4,2
  print(tup1, tup2, tup3)
  print(type(tup1), type(tup2), type(tup3))


Output:
1 4 2
<class 'int'> <class 'int'> <class 'int'>

To print the datatype of variable.

  a = (1,3,4)
  print(type(a))


Output:
<class 'tuple'>

To store the same tuple in multiple variables.

  tup1 = tup2 = tup3 = (1,2,'xyz')
  print(tup1, tup2, tup3)
  print(type(tup1), type(tup2), type(tup3))


Output:
(1, 2, 'xyz') (1, 2, 'xyz') (1, 2, 'xyz')
<class 'tuple'> <class 'tuple'> <class 'tuple'>

To print the tuple and its datatype.

  tup = (1, 'XYZ', 'Delhi', 10)
  print(tup, type(tup))


Output:
(1, 'XYZ', 'Delhi', 10) <class 'tuple'>

Indexing, Slicing & Dicing on Tuples


Indexing can also be done on tuple.

  tup = (1,'XYZ','Delhi',10)
  tup[1]


Output:
'XYZ'

Slicing and Dicing on tuple.

  tup[1:3]


Output:
('XYZ', 'Delhi')

Indexing on tuple:

  tup[-1]


Output:
10

To prove that tuples are immutable.

  tup[1] = 'The Education Machine'

reserved_keywords

List Datatype

List is one of the mutable datatype.
Lists are declared using square parenthesis [ ].
As these are mutable, one can alter lists such as append, insert or remove.


Syntax to define a list:

  l = [1, 'XYZ', 'Delhi', 10]
  print(l,type(l))


Output:
[1, 'XYZ', 'Delhi', 10] <class 'list'>

Indexing on list:

  l[2]


Output:
'Delhi'

Slicing and dicing on list:

  l[-1::-1]


Output:
[10, 'Delhi', 'XYZ', 1]

Let's see what is meant by MUTABLE. Focus on ID and values.

  l=[1,20]
  print(l, id(l))
  l[1] = 30
  print(l, id(l))


Output:
[1, 20] 2041007561864
[1, 30] 2041007561864

The IDS are same after altering the list, this means that user can overwrite in list and the IDs will remain same.

  l = [10,40]
  print(l, id(l))
  l[1] = 30
  print(l, id(l))


Output:
[10, 40] 2041007556616
[10, 30] 2041007556616

If a new list is created, then IDs will be different.

  l = [10,20]
  print(l, id(l))
  l = [20,30]
  print(l, id(l))


Output:
[10, 20] 2041007771272
[20, 30] 2041007556616

Conversion of list to tuple using tuple() function.

  l = [1,'XYZ','Delhi',10]
  print(l,type(l))
  t = tuple(l)
  print(t,type(t))


Output:
[1, 'XYZ', 'Delhi', 10] <class 'list'>
(1, 'XYZ', 'Delhi', 10) <class 'tuple'>

Coversion of tuple to list using list() function.

  t = (1,'XYZ','Delhi',10)
  print(t,type(t))
  l = list(t)
  print(l,type(l))


Output:
(1, 'XYZ', 'Delhi', 10) <class 'tuple'>
[1, 'XYZ', 'Delhi', 10] <class 'list'>

Set Datatype

Sets are also one of the types of collections and very similar to Tuples and Lists, however the only difference is "It does not support duplicate values". Even if the user enter duplicate values in a set, it will store only unique values.
Curly braces { } are used for sets.

Another difference between sets and other collections are:
1.) It does not support Indexing.
2.) Since indexing is not supported, Slicing & Dicing is also not possible.
3.) It does not support item assignment.


Syntax to define a set:

  empl_ids = {5,1,9,2,56,5,2,90,5}
  print(empl_ids, type(empl_ids))


Output:
{1, 2, 5, 9, 56, 90} <class 'set'>

Datatype of the variable:

  names = {'modi', 'gandhi', 'a', 'kejriwal', 'shah'}
  print(names, type(names))


Output:
{'modi', 'a', 'shah', 'gandhi', 'kejriwal'} <class 'set'>

Lets try indexing on set. This will throw an error as indexing is not possible in sets.

  names[1]

reserved_keywords

Slicing and Dicing is also not possible on set.

  names[0:3]

reserved_keywords