Monday, December 9, 2019

python data types

Data type represents the type of data present inside a variable.
In python we are not required  to specify the type explicitly. Based on value provided, the type will be assigned automatically. Hence python is dynamically typed language.

Python contains following inbuilt data types 
Int 
Float 
Complex 
Bool
Str
Bytes
Bytearray
Range
List
Tuple
Set 
Frozenset 
Dict 
None

Note : python contains several inbuilt functions

  1. type() : to check the type of variable 
  2. id() : to get address of object 
  3. print() : to print the value 


In python everything is an object. 

  1. Int Data Type : we can use int data type to represent whole numbers ( integral values ) 
E.g : a = 10 
        type(a) #int

Note : 
In python2 we have long data type to represent very large integral values.
But in python3 there is no long type explicitly and we can represent long values also by using int type only.

We can represent int values in the following ways 

  1. Decimal form 
  2. Binary form 
  3. Octal form 
  4. Hexa decimal form 

  1. Decimal form ( Base-10)

 It is the default number system in python 
The allowed digits are : 0 to 9 
E.g. : a = 10 

  1. Binary form ( Base-2)

The allowed digits are : 0 & 1 
Literal value should be prefixed with 0b or 0B

E.g:  a= 0B1111 a=0B123 a=b111

  1. Octal form 

The allowed digits are : 0 to 9, a-f ( both lower and upper cases are allowed )
Literal value should be prefixed with  0x or 0X 

Eg: a = 0XFACE a = 0XBeef a = 0XBeer 

Note: Being a programmer we can specify literal values in decimal, binary, octal and hexadecimal forms. But PVM  will always provide values only in decimal form.

a=10        print(a)10
b=0o10   print(b)8
c=0X10   print(c)16
d=0B10  print(d)2 

Base conversions 

Python provide the following in-built functions for base conversions 

  1. bin():
We can use bin() to convert from any base to binary 

  1. >>> bin(15)
      2.  ‘0b1111’
  1. >>> bin(0o11)
  2. ‘0b1001'
  3. >>> bin(0X10)
  4. ‘0b10000'

  1. oct():
We can use oct() to convert from any base to octal 

  1. >>> oct(10)
      2. ‘0o12’
  1. >>> oct(0B1111)
  2. ‘0o17'
  3. >>> oct(0x123)
  4. ‘0o443'

  1. hex();
We can use hex() to convert from any base to hex decimal 

  1. >>> hex(100)
  2. ‘0x64'
  3. >>> hex(0B111111)
  4. ‘0x3f'
  5. >>> hex(0o12345)
  6. ‘0x14e5'

  1. Float Data Type:

We can use float data type to represent floating point values ( decimal values )
E.g: f = 1.234 
      type(f) float 

We can also represent floating point values by using exponential form 
( Scientific Notation )
Eg: f = 1.2e3 —> instead of ‘e’ we can use  ‘E’ 
     Print(f) 1200.0 

The main advantage of exponential form is we can represent big values in less memory.

Note : we can represent int values in decimal , binary octal and hex decimal forms. But we can represent float values only by using decimal form.

  1. >>>f=0B11.01
  2.  File “<stdin>”, line 1
  3.   f=0B11.01
  4.          ^
  5. SyntaxError: invalid syntax

  6. >>> f=0o123.456
  7. SyntaxError: invalid syntax 

  8. >>>f=0X123.456
  9. SyntaxError: invalid syntax 

  1. Complex data type 
A complex number of the form 

a  ( Real part ) + b ( imaginary part ) j  ( j2 = -1 ) 

‘a’ and ‘b’ contain integers or floating point values.

Eg: 
3 + 5j
10 + 5.5j
0.5 + 0.1j

In the real part if we use int value then we can specify that either by decimal, octal, binary or hex decimal form.
But imaginary part should be specified only by using decimal form.

  1. >>> a=0B11+5j
  2. >>> a
  3. (3+5j)
  4. >>>a=3+0B11j
  5.  SyntaxError: invalid syntax 

Even we can perform operations on complex type values.

  1. >>> a=10+1.5j
  2. >>> b=20+2.5j
  3. >>> c=a+b 
  4. >>> print(c)
  5.  (30+4j)
  6. >>> type(c) 
  7. <class ‘complex’>

Note: Complex data type has some inbuilt attributes to retrieve the real part and imaginary part 

c = 10.5+3.6j

c.real —> 10.5 
c.imag —> 3.6 

We can use complex type generally in scientific applications and electrical engineering applications.

  1. Bool datatype 

We can use this data type to represent boolean values.
The only allowed values for this data type are : 
True and False 
Internally python represents True as 1 and False 0 

b = True 
type(b) —> bool 

Eg:

a = 10
b = 20 
c = a<b
print(c) —> True 

True+True —> 2 
True-False —> 1 

  1. str Data type

Str represents string data type.
A string is a sequence of characters  enclosed within single quotes or double quotes.

s1=‘durga’
s1=“durga”

By using single quotes or double quotes we cannot represent multi line string literals.

s1=“durga
soft”

For this requirement we should go for triple single quotes(‘’’)  or triple double quotes(“””)

s1 = '’’durga
soft’’'

s1 = “””durga
soft”””

We can also use triple quotes to use single quote  or double quotes  in our string.
‘’’ This is “ character’’'
‘ This i “ Character’

We can embed one string in another string 
‘’’ This “Python class very helpful” for java students’’'

Slicing of Strings:

  1. Slice means a piece  2) []operator is called slice operator, which can be used to retrieve parts of string
  2. In python strings follows zero based index 
  3. The index can be either +ve or -ve 
  4. +ve index means forward direction from left to right 
  5. -ve index means backward direction from right to left 


-5 
-4 
-3 
-2 
-1 


d
u
r
g
a


0
1
2
3
4



  1. >>> s=“durga"
  2. >>> s[0]
  3. ‘d'
  4. >>> s[1]
  5. ‘u'
  6. >>> s[-1]
  7. ‘a'
  8. >>> s[40]

IndexError: string index out of range

  1. >>> s[1:40]
  2. ‘urga'
  3. >>> s[1:]
  4. ‘urga'
  5. >>> s[:4]
  6. ‘durg'
  7. >>> s[:]
  8. ‘durga'
  9. >>>

  10. >>> s*3 
  11. ‘durgadurgadurga'

  12. >>> len(s)
  13. 5

Note:

In python the following data types are considered as fundamental data types 

Int
Float 
Complex 
bool
Str

In python, we can represent char values also by using str type and explictly char type is not available.

1  >>> c=‘a’
2  >>> type(c)
3  <class ’str’>

Long data type is available in Python2 but not in Python3. In Python3 long values also we can represent by using int type only.

In python we can present char value also by using str type and explicitly char type is not available.



1 comment:

  1. Kaushik Gattu: Python Data Types >>>>> Download Now

    >>>>> Download Full

    Kaushik Gattu: Python Data Types >>>>> Download LINK

    >>>>> Download Now

    Kaushik Gattu: Python Data Types >>>>> Download Full

    >>>>> Download LINK Kt

    ReplyDelete