0% found this document useful (0 votes)
47 views8 pages

Classification of Datatype and Mutability, Immutability

This document provides information about different data types in computer science including integers, floating point numbers, complex numbers, strings, lists, tuples, sets, and dictionaries. It discusses the functions type() and id() to find the data type and memory address of a variable. It also describes implicit and explicit type conversion. Finally, it discusses mutable and immutable objects, providing examples of objects that can and cannot change their values or memory addresses.

Uploaded by

mohamed Aasis
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)
47 views8 pages

Classification of Datatype and Mutability, Immutability

This document provides information about different data types in computer science including integers, floating point numbers, complex numbers, strings, lists, tuples, sets, and dictionaries. It discusses the functions type() and id() to find the data type and memory address of a variable. It also describes implicit and explicit type conversion. Finally, it discusses mutable and immutable objects, providing examples of objects that can and cannot change their values or memory addresses.

Uploaded by

mohamed Aasis
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/ 8

ORISON ACADEMY SENIOR SECONDARY SCHOOL

GRADE 11COMPUTER SCIENCE MATERIAL


8.DATA HANDLING
Data Type:
Data type is a kind of data that a variable hold in a programming
language.
To find the Data Type used in the variable , the function used is :
type( variable name/ value)
To find the Memory address of variable ,the function used is :
id( variable name/ value)
To convert from one datatype to another:
Two methods:implicit and explicit conversion
 Implicit conversion: Datatype change brought about
the compiler.
 Example of implicit: 9.0+2=11.0 (one is int another is
float data type so the result is converted in to float)
 Explicit conversion: Datatype change brought about
the user.
 Example of explicit:a=9
float(a)
(result is 9.0 , since user has
written the code as float(a))
Classification of Data Type:
1.Integers:
All the whole numbers belongs to the integer datatype
It can be signed or unsigned which means that it can be
negative or positive values
Integers further classified into decimal, Boolean , octal and
hexadecimal
Decimal numbers will be from 0 to 9
Boolean numbers are 0 and 1
Octal numbers are from 0 to 7 and it will be prefixed with 0o
Hexadecimal numbers are from 0 to 9 and A to F(10 to 15)
and it will be prefixed with 0x

2.Floating point numbers/ real :


All the numbers with decimal point are said to be real
numbers
It can be represented in 3 ways: 7.8 (or) 7.8X10power1 (or)
7.8 E 1 (or) 7.8 e 1
E or E represents EXPONENT; number before E or e is called
mantissa part ; number after E or e is called as exponent
part
Single precision float can hold 7 digits after decimal point
Double Precision float can hold15 digits after decimal point
Comma and space should not be used while representing
int and float numbers

3.Complex Numbers :
The numbers with real and imaginary *art 5s ter0 t6 be the
complex numbers
It is represented in the form of real + imaginary
Example: k=4+5j where j=√-1
To print only the real part give the code as k.real and for
imaginary give the code as k.imag
If the input is 0+any imaginary part then in the result only
the imaginary part will get displayed
4.Strings:
Strings can be a single character enclosed with single
quotes/ group of characters with single or double quotes /
group f lines with triple or double quotes.
Strings supports indexing. It can be forward or backward
indexing
Forward indexing starts from 0 to n.
Backward indexing from -1
Example: s=”all the best”
T=’j’
W=’logical thinking’
S[1] will give the result as l

5.Lists:
Group of elements are declared in a single variable is
termed as list.
The list is represented using [ ]
List is an ordered list
List allows indexing
Example: L=[8,2,3]
L[2] will give the result as 3

6.Tuples:
Group of elements are declared in a single variable is
termed as tuple.
The tuple is represented using ( )
tuple is an ordered way of representing
tuple allows indexing
Example: T=(8,2,3)
T[1] will give the result as 2

7.Sets:
Group of elements are declared in a single variable is
termed as Set.
Sets are represented using { }
Sets are unordered list
Sets doesn’t support indexing
Sets is the only datatype which doesnot allow duplicate
values. EX: S={5,5,5} while printing the result only {5} will be
the result
While updating the Sets, only the immutable objects can be
updated with the existing set.

8.Dictionary:
Group of elements are declared with key and values in a
single variable is termed as dictionary.
Dictionaries are represented using { key:”value”}
Example: D={1:”apple”,2:”banana”,63:”icecream”}
Dictionary indexing is based on key.
D[63] will give the result as icecream
Key values can be updated/ edited.

-------------------------------------------------------------------------------------------------
MUTABLE AND IMMUTABLE OBJECTS

S.NO MUTABILITY IMMUTABILITY


1 Mutability means that in the Immutability means that new
same memory address , new value cannot be stored in the
value can be stored as and same memory address as and
whenever needed. whenever needed.
2 Mutable objects: Immutable Objects:
List, sets, dictionaries Int, float, string,Boolean ,
*Note: sets doesn’t support tuples
the same mutable objects to
get added
3 Example: Example:
L=[8,9,5] Sample=”nams”
L[1]=3 S[3]=”e”
Now L value will get Here the data cannot be
changed to L=[8,3,5] but the changed.
memory address remains the Excemption case:
same I=8
I=5
Now the value of I will change
to 5 from 5, at the same time
memory address will also get
changed,since it’s immutable.
1. INTEGERS
>>> i=3
>>> id(i)
507107312
>>> i=4
>>> id(i)
507107344
>>> # so i is immutable
2. FLOAT
>>> f=9.8
>>> id(f)
30672240
>>> f=6.8
>>> id(f)
30674112
>>> # so float is immutable
3. BOOLEAN
>>> b=0
>>> id(b)
507107216
>>> b=1
>>> id(b)
507107248
>>> #so bool is immutable
4. COMPLEX
>>> c=9+2j
>>> id(c)
43051696
>>> c=0+2j
>>> id(c)
50966032
>>> #so complex is immutable
5. STRINGS
>>> S="you"
>>> S[0]
'y'
>>> S[0]='E'
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
S[0]='E'
TypeError: 'str' object does not support item assignment
>>>#so strings are immutable
6. LISTS
>>> L=[3,4,5]
>>> L
[3, 4, 5]
>>> id(L)
52318856
>>> L[0]
3
>>> L[0]=1
>>> L
[1, 4, 5]
>>> id(L)
52318856
>>> #So lists are mutable
7. DICTIONARY
>>> D={1:"J",2:"K"}
>>> D
{1: 'J', 2: 'K'}
>>> id(D)
52944392
>>> D[2]
'K'
>>> D[2]='L'
>>> D
{1: 'J', 2: 'L'}
>>> id(D)
52944392
>>>#So dictionaries are mutable
8. SETS
>>> S={5,6}
>>> id(S)
52267496
>>> S={5,6,(7)}
>>> S
{5, 6, 7}
>>> id(S)
52268392
>>> S={5,6,7,[8]}
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
S={5,6,7,[8]}
TypeError: unhashable type: 'list'
>>> #So sets are mutable, we can or remove elements . but we
cannot add mutable objects in to the set.
>>> #also set can have another set inside it, which is called as
subset

You might also like