Skip to main content

DATATYPE – TUPLES AND DICTIONARIES



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 is 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 consists of only integers, it gives maximum or largest integer present. When the Tuple consists 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.
Ø min(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 represented 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 (,). These 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
This 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 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 the example.






Comments

Popular posts from this blog

COMPARISON, LOGICAL, ASSIGNMENT AND BITWISE OPERATORS

COMPARISON, LOGICAL, ASSIGNMENT AND BITWISE OPERATORS In this blog we will see about Arithmetic operators and understand how to use it with Python IDLE to perform certain calculations. PYTHON COMPARISON OPERATORS OPERATOR DESCRIPTION EXAMPLE        == Checks whether LHS is equal to RHS, then the condition becomes true 10==10 is true        != Checks whether LHS is not equal to RHS, then the condition becomes true 2!=3 is true        > Checks whether LHS is greater than RHS, then the condition becomes true 6>3 is true        < Checks whether LHS is less than RHS, then the condition becomes true 6<10 is true        >= Checks whether LHS is gre...

BUILT INS FUNCTIONS IN PYTHON

BUILT INS FUNCTIONS IN PYTHON Every programming languages have certain pre-defined functions. Similarly, for certain tasks python has built in functions which you can access and understand as explained in the video.   dir(__builtins__): This module will return all the built in function and identifiers directly available to the programmer.   help(“function name”): This is the most important function in python. It gives the complete detail regarding the function which you mentioned inside help().

WHILE LOOP IN PYTHON

WHILE LOOP IN PYTHON Loops are used to execute a piece of code or set of statements multiple times in a program as long as the condition is true. While loop is the basic loop structure in Python. Syntax: While (expression):             Statement(s) Here, condition may be any logical expression and statement(s) can be single statement or multiple statements. The loop will iterate or execute until the condition is true. When the condition becomes false, program comes out of the loop and remaining statements are executed. In Python language, during any of the decision making, control statements and looping indentation is very important because, it specifies the set of statements to be a part of single code or block. Since no brackets are used to specify the complete code with in, indentation is used. Flowchart: In While loop, the main important thing is the loop may never be executed if the condition is...