0% found this document useful (0 votes)
2 views30 pages

7 Session Data Handling Part 1

The document provides an overview of data types in Python, including built-in core types such as numbers, strings, lists, tuples, dictionaries, and sets. It explains the concepts of mutable and immutable data types, detailing how integers, floats, booleans, strings, and tuples are immutable, while lists, dictionaries, and sets are mutable. Additionally, it covers the properties and behaviors of these data types with examples to illustrate their usage.

Uploaded by

lupinstark7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views30 pages

7 Session Data Handling Part 1

The document provides an overview of data types in Python, including built-in core types such as numbers, strings, lists, tuples, dictionaries, and sets. It explains the concepts of mutable and immutable data types, detailing how integers, floats, booleans, strings, and tuples are immutable, while lists, dictionaries, and sets are mutable. Additionally, it covers the properties and behaviors of these data types with examples to illustrate their usage.

Uploaded by

lupinstark7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

PYTHON DATA HANDLING

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

•NUMBERS(int, float, complex)


•String
•List
•Tuple
•Dictionary
•Set
PYTHON DATA HANDLING
INTEGERS(IMMUTABLE)
There are two types of Python integers:-
i. Integers (signed):-
 It is normal integer representation of whole numbers.
 Can store any integer(big or small) only limited to the memory
available.
 Integers can be negative or positive.

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.

• A Python string is a sequence of characters and each character can be


individually accessed using an index
• Strings in Python are stored as individual characters in contiguous locations .
• Strings in Python are given two way indexing :
• Forward Indexing- (0 to len(string)-1)

• Backward Indexing- (-1 to –len(string))


(forward indexing( 0 to 5)
Example:- 0 1 2 3 4 5
S1=‘PYTHON’ P Y T H O N
len(S1) is 6
-6 -5 -4 -3 -2 -1
(backward indexing ( -1 to -6)
PYTHON DATA HANDLING
STRING:-
Accessing individual characters from the string.
Syntax:-
stringobject[indexno]

S1[4] #will give ‘O’


S1[2] # will give ‘Y’
S1[-1] # will give ‘N’
S1[-6] # will give ‘P’
print(S1[7]) # will produce error( string out of range)
Length of the String can be determined by built in function len()
Example:-
print(len(S1)) 0 1 2 3 4 5
P Y T H O N

-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

If you want to change it then assign again the complete string


S1=‘ADITYA’
0 1 2 3 4 5
S1 A D T T Y A

-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

Traceback (most recent call last):


File "<pyshell#9>", line 1, in <module>
print(T1[7])
IndexError: tuple index out of range

>>> T1[2]=6000 #ERROR

Traceback (most recent call last):


File "<pyshell#10>", line 1, in <module>
T1[2]=6000
TypeError: 'tuple' object does not support item assignment
PYTHON DATA HANDLING
DICTIONARY:-
Dictionary is an unordered set of comma separated items where each item is a
key:value pair within curly brackets{}
Dictionary is mutable. i.e vale for a particular key can be changed.
New key: value pair can be added or removed.(grow and shrink)
Its not ordered so there is no index no . We refer to dictionary items by keys.
Keys are unique.
Example:-

>>>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):-

>>> d={'aman singh':100000,'anita verma':150000,'sunil dev':200000}


>>> print(d)
{'aman singh': 100000, 'anita verma': 150000, 'sunil dev': 200000}
>>> print(id(d))
1476169241528
>>> d['aman singh']=120000 # item assignment allowed as its is mutable
>>> print(d)
{'aman singh': 120000, 'anita verma': 150000, 'sunil dev': 200000}
>>> print(id(d))
1476169241528
PYTHON DATA HANDLING
VARIABLE INTERNALS
Object:-
Any entity that stores and value or any type of data is an object in Python
An object has certain properties and exhibits certain type of behaviour.
(All data and value are referred as objects in Python)
Variable is an object that refers to a value.
Example:-

Interger values are objects –


Properties:- they hold whole numbers only and have infinite precision.
Behaviour- All arithmetic operations can be performed on them.
List is an object:-
Properties:- can hold any type of values and any number of values.
Behaviour:- Mutable (add, delete, update)
PYTHON DATA HANDLING
VARIABLE INTERNALS
Every Python object has three key attributes:-
• type
• value
• id
TYPE
The type of an object determines the type of operations that can be performed
on the object.
The built in function type() tells the type of the object.
Example:-
>>> x=5000
>>> print(type(x))
<class 'int'>
>>> y=5.5
>>> print(type(y))
<class 'float'>
>>> >>> s1='Microsoft'
>>> print(type(s1))
<class 'str'>
PYTHON DATA HANDLING
VARIABLE INTERNALS
VALUE
It means the data item contained in the object, a literal is also an object.
Built in function print() will display the value where object is referring to
currently.

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

You might also like