Python Data Types and Collections
- Typically, in every high level programming language, data is stored in variables. In the python variables store data of various data types.
- The variable name must start from underscore ( _ ), digit (0 - 9), character (a - z and/or A - Z) nothing else i.e no special character is allowed for variable name.
- Python majorly supports data of int, float, string and complex number data types.
- As I told earlier, data of above data types can be stored in the variables. However, when large amount of data have to be saved in variables then it becomes tedious. To make process easy there are collection mechanism provide for it and these are known as containers. Majorly, List, Tuple, Dictionary and Set are the containers.
- Before moving further, I would like to make it very clear that out of these four containers, first two containers i.e. a) list and b) tuple, supports data of various data types i.e same list may have data of multiple data types simultaneously. Similarly for tuples too. Wow!! its great. Further, all these containers can contain any finite number of items (not infinite). 😁
- Now you would be thinking that what is the difference between list and tuple. Yeah I know...The difference is this...List is mutable but tuple is not. Means, once the container is created as tuple means tuple container is declared, it can't be modified further but but if list container has been created it can be modified any number of times. Very Simple..!!
- Now the question comes that how to declare a list. In list all elements are declared with in [ ] and multiple items are seperated by comma (,). Strings are writtern within double quotes (" ") or single quotes ( ' ' ). Suppose you want to declare a list of integer items :
L = [10, 4, 2, 7, 5]
if you want to declare list of strings or float items, do this:
L = ['Brij', 'Krishna', 'Radha', 'Meera']
L = ["Brij", "Krishna", "Radha", "Meera"]
L = ['Brij', "Krishna", 'Radha', 'Meera']
If you want to declare list of items of mix data types, do this:
L = ["Brij", "30", 20, 10.5]
In all above cases output will be list with all its elements.
- Similarly, if we want to declare a tuple then please note that all elements are declared with in ( ) and all other things are very similar to that of tuple i.e. multiple items are seperated by comma ( , ). Strings are writtern within double quotes (" ") or single quotes ( ' ' ). Suppose you want to declare a tuple of integer items :
T = (10, 4, 2, 7, 5)
if you want to declare tuple of strings or float items, do this:
T = ('Brij', 'Krishna', 'Radha', 'Meera')
T = ("Brij", "Krishna", "Radha", "Meera")
T = ('Brij', "Krishna", 'Radha', 'Meera')
If you want to declare tuple of items of mix data types, do this:
T = ("Brij", "30", 20, 10.5)
In all above cases output will be tuple with all its elements.
- Now little bit about third container i.e Dictionary. Dictionary is actually works on the principle of key-value pair. i.e. each and every item is actually key-value pair and these items will be written with in curly braces { }.
- Further all the items (key-value pair) will be seperated by comma and with each element key and values are seperated by colen ( : ).
- The beauty of keys is; each and every key is unique with the dictionary and all these values may or may not be unique (generally not).
- The key and values may of any data type, or even may be container too. Wow!! Are you getting what I am saying...See these examples.
D={1:4, 3:7, 2: 9}
D={1:4, 3:7, 2: [9, 4]}
D={1:4, 3:(3,5), 2: [9, 4]}
D={1:{5:8, 3:2, 7:1}, 3:(3,5), 2: [9, 4]}
D={'c':6, 'a':1, 'b':3}
In all above cases, output will be dictionary with all its elements in the acsending order of keys.
- All above are valid formats.
- In next lecture we will discuss about operations on elements of these containers.
No comments:
Post a Comment