Learn Core Python

Python Datatypes

Python Datatypes:-

What are Datatypes?

è Datatypes are used to allocate sufficient memory to inputs.

è Datatypes are used represent the type of data present in variable.

In Python, Every Datatype is a predefined class. So it creates objects in the memory There is no primitive concept in Python.

è In Python, No need to specify datatype, based on the right side value of variable python

will assign datatype.

è In Primitive Datatype sizes are fixed. In Object memory created based on value.

 

In Primitive once we declare variable with type we cannot change different type and also we cannot assign different type of value.

 

In Python, if you try to assign different value

è Python is a Dynamically typed language. 

           

Types of Datatypes:-

In Python we have different datatypes. They are:

1. Simple Datatypes -- It stores only one value at a time

   a) int

   b) float

   c) complex

   d) bool

   e) str

2. bytes

3. bytearray -- multiple values of same type.

4. range -- Returns range values.

5. Complex or Collection Datatypes -- It stores multiple values in a variable

   a) list

   b) tuple

   c) set

   d) frozenset

   e) dict

5. None -- means nothing  

 

In Python we have several inbuilt functions.

1. type() -- It checks the type of variable and returns that variable type.

E.g:-

a  = 10
print(a)  
print(type(a))
b = 10.5
print(b)  
print(type(b))
c = 'cfamily'
print(c)  
print(type(c))
d = True
print(d)  
print(type(d))

 

O/P:-

10

<class 'int'>

10.5

<class 'float'>

cfamily

<class 'str'>

True

<class 'bool'>

 

2. id() -- It returns address of object.

a  = 10
print(a)  
print(id(a))

O/P:-

10

140719610538048

 

3. print() -- It executes expression and displays output on a console.

 

 

Simple Datatypes:-

Simple datatypes allows only one value at a time.

a = 10

....

....

a = 20

....

....

a = 'cfamily'

 

int datatype:-

-->It is used to store/represent numeric values or whole numbers or integer values.

E.g:-

10
20
 
a = 10
print(type(a))

O/P:- <class 'int'>

 

Upto Python2, we have long datatype also but from Python3 we have only 'int' datatype.

 

We can represent int values in different ways. They are:

1. Decimal Form (0 to 9)

2. Binary Form (0,1)

3. Octal Form(0 to 7)

4. Hexa Decimal Form (0 to 9,a to f)

 

decimal form:-

è Decimal values are 10, they are from 0 to 10.

è The base of decimal is 10

E.g:-

a = 10
print(a) ==> 10
1     0
10^1  10^0
1*10+ 0*1
10+0==>10

 

binary form:-

è binary means 2

è binary values 0,1

è The base of binary is 2

è Every binary value prefix with '0b' or '0B'

E.g:-

a = 0B1011
1   0   1   1
2^3 2^2           2^1 2^0
1*8+0*4+1*2+1*1
8+0+2+1==>11
 
a = 0b111111
print(a)

O/p:-63

 

octal form:-

è octal values are 8. They are 0 to 7

è The base of octal is '8'

è Every octal value prefix with '0O' or '0o'

E.g1:-

a = 0o12
print(a) ==> 10

 

1   2

8^1 8^0

1*8+2*1

8+2==> 10

 

E.g2:-

a = 0O18
print(a)

O/P:-

SyntaxError: invalid digit '8' in octal literal

 

hexa decimal form:-

hexa decimal values are 16. they are

    0 to 9 and a to f

            a -- 10

            b -- 11

            c -- 12

            d -- 13

            e -- 14

            f -- 15

à The base of hexa decimal is '16'.

à  Every hexa decimal value prefix with '0x' or '0X'.

E.g1:-

a = 0x1a
print(a)
 
1    a
16^1 16^0      
1*16+10*1
16+10==>26

 

E.g2:-

a = face
print(a)

O/P:-

NameError: name 'face' is not defined

 

E.g3:-

a = 0Xface
print(a)==>64206

 

E.g4:-

a = 0xbeer
print(a)           

SyntaxError: invalid syntax

there is no 'r' in hexa decimal

 

base conversion functions:-

à  Python provided in-built functions for base conversion.

1. bin()

2. oct()

3. hex()

 

1. bin():-

è By using this function we can convert any base(decimal) to binary.

E.g:-

bin(10)
bin(0O17)
bin(0xface)

 

2. oct():-

à  By using this function we can convert any base(decimal) to octal.

E.g:-

oct(12)
oct(0B1010)
oct(oxface)

 

3. hex():-

à  By using this function we can convert any base(decimal) to hexa decimal.

E.g:-

hex(10)
hex(0O12)
hex(0B1010)

 

Note:-

à  Python is a Case-Sensitive. In Some cases Python ignores Case-Sensitive

E.g:-

ox or oX -- are same -- to represent hexa decimal values
ob or 0B -- are same -- to represent binary values
0o or 0O -- are same -- to represent octal values.

 

float datatype:-

à  We can use float datatype to represent floating point values or real number or decimal

    values with fractional part.

E.g:-

10.5
5.4      

 

E.g:-

a = 10.5
print(type(a))

O/P:- <class 'float'>

 

à  We can also represent floating point values in the form of exponential form(scientific

    notation).

E.g:-

a = 1.2e3
print(a)
print(type(a))

 

Here 'e' means exponential -- here also we can use 'e' or 'E' both are same.

 

à  The main advantage of exponential form is it occupies less memory.

 

à  floating values we can represent only in decimal form.

E.g:-

a = ob1010.001

SyntaxError: invalid syntax

 

complex datatype:-

à  Used only in scientific applications.

à  This datatype has two parts.

    a) real part

            b) imaginary part.

à  In real part or imaginary part we can take either inter or floating or combination.  

à  real part and imaginary part separated with '+' symbol and imaginary part suffix with 'j'

 

E.g:-

a = 10+10j
print(a)
print(type(a))

 

E.g:-

a = 10.5+10.5j
print(a)
print(type(a))

 

E.g:-

a = 10+5.5j
print(a)
print(type(a))

 

E.g:-

a = 10.5+5j
print(a)
print(type(a))

 

à  We can take real part or imaginary part as int value means int values we can take

    in the form of decimal, binary, octal, hexa decimal.

E.g:-

a = 0B1010+10.5j
print(a)
print(type(a)) 

 

E.g:-

a = 0O17+5.5j
print(a)
print(type(a))

 

E.g:-

a = 0xface+5.5j
print(a)
print(type(a))

 

bool datatype:-

à  For boolean represent python reserved a keyword called "bool".

à  In Python there are two values reserved to represent boolean values. They are

    1. True

            2. False

Other than these two values if store any value we will get error.

           

à  In some languages

       '0' means false, 1 means True.

               t -- True

               f -- False

                       

E.g:-

a = true

print(a)

print(type(a))

 

NameError: name 'true' is not defined

 

E.g2:-

a = True

print(a)

print(type(a))

O/P:-

True

<class 'bool'>

 

à  Conditional statements expects boolean values True or False.

    if 10>5:

              ...

            ....

   

à  relational operators:>,<,>=,<= returns boolean values.

            print(10<20)

           

à  Logical operators returns boolean values.

            print((10>5) and (10<20)) -- True

            print((10>5) and (10>20)) -- False

           

str datatype:-

à  'str' is a keyword to represent string in python.

à  Every String in python enclosed either single quote or double quote. 

E.g:-

a = 'cfamily'
name = "akkem sreenivasulu"
print(a)
print(type(a))
print(name)
print(type(name))

O/P:-

cfamily

<class 'str'>

akkem sreenivasulu

<class 'str'>

 

à  A String is a collection of characters. There is no character datatype in Python.

    Everything is treated as string.

           

bytes datatype:-

à  bytes data type represents a group of byte numbers just like array.

E.g:-   

a = [10,20,30,40,50]
b = bytes(a)
print(b)
print(type(b))
print(b[0])
print(b[1])
print(b[2])
print(b[3])
print(b[4])

O/P:-

b'\n\x14\x1e(2'

<class 'bytes'>

10

20

30

40

50

à  To access each element is bytes datatype we use index, index starts from '0' and ends with

    n-1, where 'n' is size of bytes.

 

Note1:-          

à  The only allowed values for byte type are 0 to 256. Other than these values if try to

    store any values in bytes datatype we will get error.

E.g:-

a = [100,200,300,400,500]
b = bytes(a)
print(type(b))

ValueError: bytes must be in range(0, 256)

 

Note2:-

à  Once we create bytes datatype value, we cannot change its values, otherwise ww will get

    error.

E.g:-

a = [10,20,30,40,50]
b = bytes(a)
print(b[0])
b[0]=100
print(b)

TypeError: 'bytes' object does not support item assignment           

 

bytearray datatype:-

à  It is exactly same as bytes datatype except that is elements can be changeable or modified.

E.g:-

a = [10,20,30,40,50]
b = bytearray(a)
print(type(b))
print(b[0])
b[0]=100
print(b[0])

O/P:-

<class 'bytearray'>

10

100

 

E.g:-

a = [100,200,300,400,500]
b = bytearray(a)
print(type(b))

ValueError: byte must be in range(0, 256)

 

Collection Datatypes

To store group of values in a single entity.

We have different collection datatypes. They are:

1. tuple

2. list

3. set

4. frozenset

5. dict

 

tuple datatype:-

è tuple can be used to store group of values in a single entity.

è It is immutable datatype.

è tuple we can represent using "()"

E.g:-

t = (10,20,30,40,50)
print(t)
print(type(t))

O/P:-

(10, 20, 30, 40, 50)

<class 'tuple'>

 

list datatype:-

è To store group of values in a single entity.

è Where insertion order is preserved and duplicates also allowed.

è list datatype can be represented using "[]"

 

E.g:-

li = [10,5,20,15,30,40,50,10,5]
print(li)
print(type(li))

O/P:-

[10, 5, 20, 15, 30, 40, 50, 10, 5]

<class 'list'>

 

set datatype:-

è  To store group of values in a single entity.

è  It will not maintain insertion order because it uses hashing mechanism

è  It will not allow duplicates.

è  We can represent using "{}"

 

E.g:-

s = {10,5,20,15,30,40,50,5,10}
print(s)
print(type(s))

O/P:-

{5, 40, 10, 15, 50, 20, 30}

<class 'set'>

 

frozenset datatype:-

è  It is similar to set except that it is immutable.

è  immutable means not changeable.

è  frozenset represents in "({})"

 

E.g:-

s = {10,5,20,15,30,40,50,5,10}
fs = frozenset(s)
print(fs)
print(type(fs))

O/P:-

frozenset({50, 20, 5, 40, 10, 30, 15})

<class 'frozenset'>

 

dict datatype:-

è is a collection key, value pairs.

è we can represent dict using "{}".

E.g:-

d = {
    101:'sreenivas',
    102:'swathi',
    103:'badri',
    104:'alia'
}
print(d)
print(type(d))

O/P:-

{101: 'sreenivas', 102: 'swathi', 103: 'badri', 104: 'alia'}

<class 'dict'>

è Key, value separated with ':'

è Key value pairs separated with ','

è key should be unique.

E.g:-

d = {
    101:'sreenivas',
    102:'swathi',
    103:'badri',
    104:'alia',
    101:'pavan kalayan'
}
print(d)
print(type(d))

O/P:-

{101: 'pavan kalayan', 102: 'swathi', 103: 'badri', 104: 'alia'}

<class 'dict'>