Data Types: Data Types Are Always Represent What Type of Data To Be
Data Types: Data Types Are Always Represent What Type of Data To Be
1.Basic Types
a.Number Types [integer | float | complex ]
b.bool type
c.None Type
Number Types :
Integer type :
1.If any Variable is initialized with a number without decimal , then they
are treated as integer type
2.Predefined class for int type is <class „int‟>
3.It may be +ve integer or –ve Integer
y=-123
type(y) #<class „int‟>
>>>
z=12345678912345678912345678912345678912345678923468237468
2634876283476827346826384762834682734872364876283476827346
>>> type(z)
<class 'int'>
2.Float
1.If any variable is assigned with a number with decimal , then they
considered by python as float
2.Predefine class for Float <class „float‟>
1|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Eg: x=3.14 [in java or c-language x=3.14; treated as double
In java or c-Language x=3.14f; ]
y=-12.232334;
type(y) #<class „float‟>
>>> x=1.2e2
>>> type(x)
<class 'float'>
>>> print(x)
120.0
>>> y=1.2E3
>>> type(y)
<class 'float'>
>>> print(y)
1200.0
3.Complex
* Complex types are mostly used for mathematics and scientific related
projects
* In math‟s complex numbers are represented by (a+bi)
* Here a is real part, b is imaginary part
* i represent under square root of -1
* But in python we have to represent by using (a+bj)
* Here real and imag part can be either of int or float , but internally it will
represents in the form of float
>>> x=10+20j
>>> type(x)
<class 'complex'>
>>> print(x)
2|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
(10+20j)
To get real and imag values from the Complex object, then we have to
use “real” and “imag” attributes
>>> print(x.real)
10.0
>>> print(x.imag)
20.0
>>> type(x.real)
<class 'float'>
>>> type(x.imag)
<class 'float'>
Bool type:
1.If any variable is assigned with either True or False , Then they are
considered by python as bool type
2. Predefined Class for bool type <class „bool‟>
3. In C-Language 0-False and 1-True But in java true or false
Eg: x=True
type(x) #<class „bool‟>
y=False
type(y) #<class „bool‟>
Eg 2:
x=1
type(x) #<class „int‟>
3|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Eg 3:
X=true #NameError
None Type:
If you want declare a variable without any value then , you have
to declare the variable is None.
In C-Language :
int x; //x will be initialized with Garbage .Value
In C and Java :
int x=NULL;
in Java
String s=null;
In Python:
x=None
type(x) #<class „NoneType‟>
>>> x=None
>>> type(x)
<class 'NoneType'>
>>> print(x)
None
X=10
X=None
4|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Eg: x=10
A reference variable can‟t refer more than object
Eg: x=”Shashi” , x=30
An Object can be referred by more than one reference
o Eg: x=10
o y=x #ref.copy
o z=y #ref.copy
o id(x)
o id(y)
o id(z)
del vs None
1.if you don‟t want variable in the python memory then we have delete
variable | object using del keyword
2. Whenever you want variable but I want erase the value of variable then
we have to use None “Data type”
>>> x=10
>>> print(x)
10
>>> x=None
>>> print(x)
None
>>> del x
>>> print(x)
5|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
print(x)
NameError: name 'x' is not defined
String Collections:
- In C and Java Language, we have character data type [char] and
char[ ] String
- In Python there is no “char” data type
- In Python we have string data type, but it can store a char or group of
chars [ A|N ]
- String is Collection i.e. it will store the data in the form Array
- The Data from string object can be accessed by using indexing
system
- String is immutable
- In Python String literals can be represented using by „shashi‟ or
“shashi” or „‟‟shashi‟‟‟ or “””shashi”””
- Predefined class for String is <class „str‟>
Eg:
>>> s1='sai'
>>> type(s1)
<class 'str'>
6|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
>>> print(s1)
sai
>>> s2="baba"
>>> type(s2)
<class 'str'>
>>> print(s2)
baba
>>> s3='''Ramesh'''
>>> type(s3)
<class 'str'>
>>> print(s3)
Ramesh
>>> s4="""Shashi"""
>>> type(s4)
<class 'str'>
>>> print(s4)
Shashi
>>> s="welcome"
>>> print(s[0]) #w
>>> print(s[3]) #c
>>> print(s[-2]) #m
7|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
>>> print(s[-1]) #e
2.Using Slicing
print(s[0:3:1]) #wel
print(s[0:3:]) #wel
print(s[:3:1]) #wel
print(s[:3:]) #wel
print(s[:3]) #wel
Eg 2:
>>> s="welCOME"
>>> print(s[3:6:1])
8|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
COM
>>> print(s[3:7:1])
COME
>>> print(s[3:7:])
COME
>>> print(s[3:7])
COME
>>> print(s[3::])
COME
>>> print(s[3:])
COME
>>> print(s[3]) #index
C
Even Position
print(s[0:7:2]) #wloe
print(s[0::2])
print(s[::2])
Odd Position
print(s[1:7:2]) #ecm
print(s[1::2]) #ecm
>>> s="WELcOME"
>>> s[3]='C'
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
s[3]='C'
TypeError: 'str' object does not support item assignment
List Collection:
Predefined class for list <class „list‟>
9|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
It will allow heterogeneous mixer of Objects
List can be indexed
List can be sliced
It is mutable
Insertion order is maintained
All the Object in the list must be taken in between [ ] and Each object
is separated by a [,]
Duplicate objects are also allowed
>>> lst=[10,3.14,10,None,"Ramesh"]
>>> type(lst)
<class 'list'>
>>> print(lst)
[10, 3.14, 10, None, 'Ramesh']
>>> print(lst[0])
10
>>> print(lst[-1])
Ramesh
>>> print(lst[0:4:1])
[10, 3.14, 10, None]
>>> lst[3]="Shashi"
>>> print(lst) #[10, 3.14, 10, 'Shashi', 'Ramesh']
Tuple:
Predefined class for tuple is <class „tuple‟>
It will allow heterogeneous mixer of Objects
Tuple can be indexed
tuple can be sliced
Insertion order is maintained
10 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
All the Object in the tuple must be taken in between ( ) and Each
object is separated by a [,]
Duplicate objects are also allowed
It is immutable
>>> t=(10,3.14,10,None,"Ramesh")
>>> type(t)
<class 'tuple'>
>>> print(t)
(10, 3.14, 10, None, 'Ramesh')
>>> print(t[1])
3.14
>>> print(t[-2])
None
>>> print(t[2:5:1])
(10, None, 'Ramesh')
>>> t[-2]="Amma"
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
t[-2]="Amma"
TypeError: 'tuple' object does not support item assignment
SET Collection:
Predefined class for set is <class „set‟>
It will allow heterogeneous mixer of Objects
Insertion order is not maintained
11 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
All the Objects in the set must be taken with in the { } and each
object should be sep by ,
Duplicate objects are not allowed
Set Object doesn‟t support indexing and slicing
Set in mutable
>>> s={10,3.14,10,None,"Ramesh"}
>>> type(s)
<class 'set'>
>>> print(s)
{None, 10, 3.14, 'Ramesh'}
>>> print(s[0])
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
print(s[0])
TypeError: 'set' object is not subscriptable
>>> print(s[0:3:1])
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
print(s[0:3:1])
TypeError: 'set' object is not subscriptable
>>> print(s)
{None, 10, 3.14, 'Ramesh'}
>>> s.add("SssiT") #it is mutable
>>> print(s)
{3.14, None, 10, 'SssiT', 'Ramesh'}
Dictionary:
12 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
If you want store group of objects in the form of key and value pairs,
then we have to use dictionary collection
Key and value should be separated by :
Key and value pair is called an item
Each item should be separated by ,
All items must be taken with in the { }
Keys must be an immutable but values can be any thing
>>> stu={"sno":101,"age":34,"name":"sudha"}
>>> type(stu)
<class 'dict'>
>>> print(stu)
{'sno': 101, 'age': 34, 'name': 'sudha'}
>>> print(stu['name'])
sudha
>>> print(stu['age'])
34
>>> print(stu['scity'])
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
print(stu['scity'])
KeyError: 'scity'
>>> print(stu)
{'sno': 101, 'age': 34, 'name': 'sudha'}
>>> stu['name']='Radha'
>>>
>>> print(stu)
{'sno': 101, 'age': 34, 'name': 'Radha'}
13 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
>>> s={}
>>> type(s)
<class 'dict'>
14 | P a g e