In this video, I have explained the remaining data types in Python. In my previous videos, I have covered Numbers, string, List and List slicing. In this video, have explained the fundamentals of tuples and dictionary with examples and difference between List and tuples. To know or learn watch the video.

DATATYPE – TUPLES AND DICTIONARIES

PYTHON TUPLES

In Python programming language, tuple is similar to that of list. The main difference between tuple and list is, elements of tuple cannot be changed once it is assigned and in list the element can be changed. This is called as immutable. Tuple can store sequence of data within it, which are separated by a comma and enclosed inside round brackets.

Example:

Tuple1 = (“A”, 21, 78, “Red”, 50, “Apple”)

In this way a tuple is created, items inside the tuple are separated by a comma and string is always stored into double quote as explained earlier.

Usually when there are multiple data stored inside an array, list, tuple, etc. the indexing of it starts from zero. For example Tuple1[0] = ‘A’ and it ends on n-1. There are many other ways of accessing items inside a tuple similar to that of list as explained in my videos. You can perform various arithmetic operations on tuple. Let see some more about Tuples in detail.

Some advantages of Tuple over List are:

  • As tuple are immutable, iteration using tuples are faster as compared to lists.
  • When data is to be unchanged, tuple provides guarantee and keeps the data write-protected.
  • Tuples are generally used to store different datatypes (heterogeneous) whereas, lists are used to store similar datatypes (homogeneous).

Some functions to be used with Tuples are:

  • len (TupleName): This function will give the total number of items present inside your Tuple.
  • max(TupleName): This function gives the maximum value inside the Tuple. When the Tuple is of only integers, it gives maximum or largest integer present. When the Tuple is of only strings, it gives the longest string which has the most numbers of characters. It does not work when the Tuple are having items of mixed data types.
  • mix(TupleName): This function gives the minimum value inside the Tuple. Same concept applies here for integer, string and mixed data types as in max().
  • del(TupleName): This function deletes the entire Tuple created and when accessed again, it will show an error saying it doesn’t exist anymore.
  • tuple(ListName): This function is used to convert List into tuple.
  • sorted(TupleName): This function is used to sort the tuple. If the items are alphabet or string, sorting will be done in alphabetical order. If the items are numeric, sorting will be done in ascending order.
  • sum(TupleName): This function is used to sum all the items or elements in your tuple. This is used for numeric values.

PYTHON DICTIONARIES

In Python programming language, dictionary is an unordered collection of data or items. Dictionary is represent in a pair format, where the indexing of elements is mapped using key. Each pair is separated using a colon (:) and the value are separated using a comma (,). This complete set of pairs can be of any datatype and the key are immutable objects (can be string, numbers, and tuple). In general, dictionary maps a set of objects (keys) to another set of objects (pair). All the pairs or data are enclosed inside a curly brackets.

Example:

Dict1 = {“Name”: “Jack”, “Age” : 20 , “Income”: 20000}

In this way a dictionary is created, pair inside the dictionary are separated using colon and the complete item is separated using comma which is enclosed inside a curly brackets.

Some of the functions used in Dictionaries are:

  • To access the elements, you can use square brackets followed by the name of the key to display the corresponding value. For example,

Dict1[“Name”]

When the above statement is executed, it will give the following output as shown below:

Jack

  • How to add an element into an existing dictionary. For example,

Dict1[“Siblings”] = 2

These will add the pair into your existing dictionary as shown below:

  • How to delete a particular element from your dictionary. For example,

del Dict1[‘Age’]

This will delete the ‘Age’ key with is corresponding value as shown below:

So in this way you can create a Python Tuples and Dictionaries, access it, delete it, add new values, remove the existing values, get the length or total count of items inside a tuple and dictionary, get the minimum and maximum value in your tuples and dictionaries. Dictionaries are generally used to maintain the diary as shown above in an example.