MUTABILITY & IMMUTABILITY
The values of all the datatypes cannot be changed in python.
Those datatypes whose members can be modified are called mutable datatypes whereas those other datatypes whose members cannot be modified are called immutable datatypes
Datatypes in python
Python supports very many types of complex data types which can be even referred to as data structures. They are:
- integer
- strings
- lists
- tuples
- dictionaries
- sets
Integer
Barely here means the same old integer in all other languages. Operation defined on Integers are also the same.
>>>a=5
>>>b=3
>>>a+b
8
>>>a*b
15
>>>a**b
125
------>long
Python can handle fairly long values. There is no need of a special data type call long, for example
>>> a=100
>>> b=100
>>> a**b
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L
>>> b=100
>>> a**b
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L
Here the L signifies the integer is a " long " integer.
Strings
Strings are just an array of characters and mean the same as in C language.
Strings are immutable.
That is operations like appending a character or changing the character using the index etc are not possible in here.
This is how u declare strings in python
>>> a='hello'
>>> b="worldd"
>>> a+b
'helloworldd'
>>> a*2
'hellohello'
>>> b*3
'worlddworlddworldd'
>>> b="worldd"
>>> a+b
'helloworldd'
>>> a*2
'hellohello'
>>> b*3
'worlddworlddworldd'
Lists
Lists are a special type of datatype that resembles an array but a lot more complex than a C array.
A list can contain a fair set of arbitary elements and are Mutable.
A list can be made as follows
>>> a=[1,2,"hello",3,5]
Lists may contain further lists within it.
>>>b=[1,2,a,7,8,"hai"]
>>>b
[1,2,[1,2,"hello",3,5],7,8,"hai"]
The [] operator is used to access individual elements of a list.
The [] operator is used to access individual elements of a list.
>>> x = [1, 2, 3]
>>> x[1]
2
>>> x[1] = 4
>>> x[1]
4
Tuples
Tuples are almost like lists but differ a little. Tuples are generally Immutable and have a different syntax.
This is how you declare tuples in Python
>>>a=(1,2,3,"hello")
Dictionaries
Dictionaries here are a key value mapping idea. That is it a set of keys mapped to their corresponding values.
We can have any Python data type as a value but while selecting the keys care should be taken that it is IMMUTABLE.
Therefore a list cannot be used as a key in dictionaries... It generates an ERROR!!
A dictionary can be declared as follows
>>>a={1:"hai","hello":[1,2],(1,2,3):"hehe"}
Sets
Sets are just the same as what we call "sets" in mathematics with some exemptions. Sets can have not only nos as its elements but also other datatypes like characters and strings. Sets cannot have other sets as its element.This is how you declare a set in Python
>>>a={1,2,3,4}
Or you can do it by calling set() also
>>>a=set([1,2,3])
NOTE
When you do something like this
>>>a={}
It means a dictionary and is not considered as a set!!






0 comments:
Post a Comment