7 Session Data Handling Part 1
7 Session Data Handling Part 1
DATA TYPE
It means the type of data and various associated operations to handle
the data.
Real life data is of many types so to represent them programming
languages provide ways and facilities to handle all types of data .
Data type also tells the capabilities of Python to handle specific data i.e.
Memory space it allocates to hold that data and the range of values
supported for the data type.
Built-in Core data type of PYTHON
ii. Booleans:-
It represents True or False
It is subtype of integers because Boolean False and True behaves like 0
and 1 respectively.
There are only two boolean objects in Python (True and False)
PYTHON DATA HANDLING
Examples:-
>>> bool(0)
False
>>> bool(1)
True
>>> x=True
>>> y=False
>>> x==1
True
>>> y==0
True
>>>
PYTHON DATA HANDLING
FLOATING POINT NUMEBRS(IMMUTABLE):-
•A number having fractional part is a floating point number.
•They can represent values between integers
•Floating point operations are slower than integer operations.
•Can represent more range( subjective to memory )
Two forms:
Fractional form:- (Normal decimal notation)
3.456, 3.5678. 3456,678, -23.45 ,-56.014
Exponent Form:-
3.5 can be represented as –
0.35E01
0.035e02
PYTHON DATA HANDLING
FLOATING POINT NUMEBRS:-
A number having fractional part is a floating point number.
Two forms:
Fractional form:- (Normal decimal notation)
3.456, 3.5678. 3456,678, -23.45 ,-56.014
Exponent Form:-
3.5 can be represented as –
0.35E01
0.035e02
PYTHON DATA HANDLING
COMPLEX NUMBERS:-
A complex number is made up of real and imaginary part (a+bj) , where
a will represent the real component and b will represent the imaginary
component.
Example:-
>>> x=2+5j
>>> type(x)
<class 'complex'>
>>> y=3+9j
>>> x+y
(5+14j)
>>> z=0+4j
>>>print(z)
4j
>>> x+y+z
(5+18j)
>>>
PYTHON DATA HANDLING
COMPLEX NUMBERS:-
Example:-
>>> x=3+4j
>>> print(x)
(3+4j)
>>> print(x.real)
3.0
>>> print(x.imag)
4.0
Note:- Same as floating point numbers because both real and imaginary parts are
of type float
PYTHON DATA HANDLING
STRING:-
• A string data type lets you hold string data i.e. any number of valid characters
into a set of quotation marks.
• Each character stored in the string is a unicode character.
• Unicode is a system to represent every character from every language.
-6 -5 -4 -3 -2 -1
PYTHON DATA HANDLING
STRING:-
Strings are Immutable(can’t change at place)
So individual characters (elements/item) of strings can’t be changed by using =
operator.
S1=‘ADTTYA’
S1*2+=‘I’ # will give error , ITEM ASSIGNMENT NOT ALLOWED
-6 -5 -4 -3 -2 -1
A D I T Y A
PYTHON DATA HANDLING
LIST:-
List in Python represents a list(sequence) of comma separated values of any
data type between square brackets.[]
List is Mutable (can be changed / modified at place)
List can grow and shrink( add and remove items of a list)
List items are ordered (indexed forward and backward both)
len() function is used to find the length of the list.
Forward indexing (0 to len(LIST)-1)
Backward indexing(-1 to –len(LIST))
Example:-
L1= [1 , 2 , 3 , 4 , 5 ]
L2=*‘a’ , ’e’ , ’i’ , ’o’ , ’u’ +
L3=*9413, “Amit Verma” , 98 , 97 , 95, 95.5, 94.5 +
print(len(L1))
print(len(L2))
print(len(L3))
print(L2[3])
print(L3*1+, “Scored:-”, L3*2)+L3*3++L3*4++L3*5++L3*6+, “ in Annual examination”)
PYTHON DATA HANDLING
LIST:-
List are mutable, so a value can be assigned to any element/ item of the list using index
no. (both forward or backward)
Example:-
L1=*1 , 2 , 3 , ’apple’ , 35.5+
L1[0]=100
L1*2+=“Mango”
L1[-1]=80.55 # calculation is [-1+len(L1)] = [-1+5] = 4
print(L1)
OUTPUT
*100, 2, ’Mango’ , ’apple’ , 80.55+
# ERROR STATEMENT
L1[5]=200
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
l1[5]=100
IndexError: list assignment index out of range
>>>
PYTHON DATA HANDLING
TUPLES:-
Tuple is a comma separated list /sequence of values of any data types within parenthesis. ()
Tuple is a sequence that can not changed i.e. can’t be modified. They are immutable.
Tuple can’t grow and shrink( can’t add and remove items of a tuple)
Tuple items are ordered. ( indexed -forward and backward both)
Example:-
>>> T1=(10 , 20 , 30.5 , 'MVN', 44, False, None)
>>> print(len(T1))
7
>>> print(T1[4])
44
>>> print(T1[-2])
False
>>> print(T1[-1])
None
>>>
PYTHON DATA HANDLING
TUPLES:-
Example:-
>>> T1=(10 , 20 , 30.5 , 'MVN', 44, False, None)
>>> print(T1[7]) # ERROR
>>>D1={1:450,2:440,3:430,5:460,4:487}
>>>D2={'aman':466,'sachin':480,'deepak':455}
>>> print(D1[5]) # access the value using key
460
>>> print(D2['deepak'])
455
>>> D1[4]=490 # modifying value by accessing it using key
>>> print(D1)
{1: 450, 2: 440, 3: 430, 5: 460, 4: 490}
PYTHON DATA HANDLING
SET:-
A set is a comma separated collection which is unordered and unindexed. In Python
sets are written with curly brackets.
Sets are unordered, so you cannot be sure in which order the items will appear.
You cannot access items in a set by referring to an index, since sets are unordered
the items has no index.(using LOOP you can access)
Once a set is created, you cannot change its items, but you can add new items.
It does not store duplicate items.( it is mutable)
Example:-
>>> set1={1,2,3,4}
>>> set2={10,20,30,20,40,50,30}
>>> print(set1)
{1, 2, 3, 4}
>>> print(set2)
{40, 10, 50, 20, 30}
>>> 50 in set1
False
>>> 50 in set2
True
PYTHON DATA HANDLING
SET:-
s={1,2,3}
>>> s.add(4)
>>> print(s)
{1, 2, 3, 4}
>>> s.update([4,5,6,7])
>>> s
{1, 2, 3, 4, 5, 6, 7}
>>> s.remove(6)
>>> s
{1, 2, 3, 4, 5, 7}
>>> s.discard(5)
>>> s
{1, 2, 3, 4, 7}
>>> s.remove(9) # remove method gives an error if element is not present
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
s.remove(9)
KeyError: 9
>>> s.discard(9) # discard does not give error even if the element is not present
>>> s
{1, 2, 3, 4, 7}
PYTHON DATA HANDLING
MUTABLE & IMMUTABLE
Python data objects can be broadly classified into two categories:-
• Mutable (changeable or modifiable)
• Immutable ( non modifiable)
Mutable means that in the same memory address, new values can be stored as
and when you want.
The data type that do not support this are called Immutable
Immutable data types:-
Integer
Float
Boolean
String
Tuple
Mutable data types:-
List
Dictionary
Set
PYTHON DATA HANDLING
MUTABLE & IMMUTABLE
Immutable data types (int):-
>>> x=10
>>> y=x
>>> z=10
>>> print(x,y,z)
10 10 10
>>> print(id(x) , id(y) , id(z))
140703590350176 140703590350176 140703590350176
>>> x=100
>>> print(id(x)) # value changed id changed int are immutable
140703590353056
>>> print(id(y), id(z))
140703590350176 140703590350176
PYTHON DATA HANDLING
MUTABLE & IMMUTABLE
Immutable data types(string):-
S1='apple'
>>> print(S1)
apple
>>> print(id(S1))
2033991503152
>>> S1='apples'
>>> print(S1)
apples
>>> print(id(S1)) # value changes id changes string is (immutable)
2033994044912
>>>
PYTHON DATA HANDLING
MUTABLE & IMMUTABLE
Immutable data types(float):-
>>> p=100.123
>>> print(p)
100.123
>>> print(id(p))
2033988870064
>>> p+=10
>>> print(p)
110.123
>>> print(id(p)) # value changed id changed float is immutable
2033993186704
PYTHON DATA HANDLING
MUTABLE & IMMUTABLE
Immutable data types(boolean):-
>>> x=False
>>> print(x)
False
>>> print(type(x))
<class 'bool'>
>>> print(id(x))
140703589847600
>>> x=True
>>> print(id(x))
140703589847568
>>> y=True
>>> print(id(y))
140703589847568
>>>
PYTHON DATA HANDLING
MUTABLE & IMMUTABLE
Immutable data types(tuple):-
>>> t1=(1,2,3,4,10,3,20,5)
>>> print(t1)
(1, 2, 3, 4, 10, 3, 20, 5)
>>> print(id(t1))
1821714372312
>>> t1=(1,2,3,4,300,3,20,5)
>>> print(t1)
(1, 2, 3, 4, 300, 3, 20, 5)
>>> print(id(t1))
1821714371752
>>>
PYTHON DATA HANDLING
MUTABLE & IMMUTABLE
Mutable data type(list):-
>>> L=[10,20,30,60,20,100]
>>> print(L)
[10, 20, 30, 60, 20, 100]
>>> print(id(L))
1821714657032
>>> L[2]=5000 # item assignment allowed as its is mutable
>>> print(L)
[10, 20, 5000, 60, 20, 100]
>>> print(id(L)) # id remains same even after change in list (mutable)
1821714657032
>>> L[-2]='IBM‘ # item assignment allowed as its is mutable
>>> print(L)
[10, 20, 5000, 60, 'IBM', 100]
>>> print(id(L))
1821714657032
>>>
PYTHON DATA HANDLING
MUTABLE & IMMUTABLE
Mutable data type(dictionary):-
Example:-
>>>x=40
>>>print(x)
40
>>>x=5.5
>>>print(x)
5.5
PYTHON DATA HANDLING
VARIABLE INTERNALS
ID
The id of the object is where the object is referring to. (location)
In most implementations it return the address.
The built in function id() returns a unique id for the specified object.
All objects in Python has its own unique id.
The id is assigned to the object when it is created.
The id is the object's memory address
Example:-
>>> x=5
>>> print(id(5))
140703590350016