Blog Single
Python Data Structures

  •  Number
    |__integer (int)
    |__float ( float)
    |__complex (complex)
  •  String ( str ) - Collection of character
  •  byte ( byte ) - Collection of ASCII character
  •  boolean (bool) - return True/False
  •  None - Empty value
  •  List (list) – Collection of ordered elements (index based)
  •  Tuple ( tuple) - Collection of ordered elements (index based)
  •  Set ( set ) – Collection of elements
  •  Dictionary ( dict ) – collection of un-ordered elements (key based)

How to find out the data structure to which our object belongs to?

For this, python supports magical function type()

Syntax:-

type(object) => return type of an object.
Here object refers to a namedvariable (or) value.

Now let’s have a look at its usage.

# int 
>>> type(10) 
<class 'int'>
>>>

>>> v1=120 
>>> v2=4.45 
>>> 
>>> type(v1)  # v1 is a named variable , it’s holding value 120. 
The value 120 is belongs to int type of data structure. 
<class 'int'> 
>>> type(len(""))  # length of empty string return will be zero(0)  
<class 'int'>
>>>

>>> def f1():
...     return 10
...
>>> type(f1()) # function f1() return will be 10 (int type) 
<class 'int'>
>>>

# bytes
bytes - collection ASCII(int) values.

>>> v=b'ABC' 
>>> type(v) 
<class 'bytes'> 
Variable v is a bytes type- which is holding ASCII value of 'ABC'.
>>> v 
b'ABC' 
>>> v[0] 
65 
here v[0] refers to 0th index value of variable 'v' (i.e.,'A') 
Python returns the ASCII value of the corresponding index value (ASCII of 'A' is 65) 

>>>type(v[0]) # is equivalent to type(65) 
<class 'int'> 

>>> v[1] 
66 
>>> v[2] 
67 
>>> 
>>> type(67) 
<class 'int'> 
>>> type(v[2]) 
<class 'int'> 
>>>

>>> type(v) 
<class 'bytes'> 
>>>

>>> type(b'abc') 
<class 'bytes'> 
>>>

>>> type(b'abc'[0]) 
<class 'int'> 
>>>

>>> type(b'abc'[0]) 
<class 'int'> 
	>>>

>>> type(len(cgi.FieldStorage().keys())) 
<class 'int'> 
>>>

# float 
>>> type(20.45) 
<class 'float'> 
>>> 
>>> type(10+20.45) 
<class 'float'> 

>>> v1=120 
>>> v2=4.45 

>>> type(v2)
<class 'float'>
>>>

# str  
>>> type("abc") 
<class 'str'> 

>>> type("") 
<class 'str'> 
>>> 

>>> type('') 
<class 'str'>  
>>> 
>>> type('a') 
<class 'str'> 
>>> 
>>> type(b'a') 
<class 'bytes'> 
>>>

# Other collections  

>>> type([]) 
<class 'list'> 

>>> type(()) 
<class 'tuple'> 

>>> type({}) 
<class 'dict'> 

>>>v=set() 
>>>type(v) 
<class 'set'>

#  function(or)method  
>>> type(print) 
<class 'builtin_function_or_method'> 
>>>

def f1(): 
	return 10 

>>> type(f1) 
<class 'function'> 
>>>

>>> def f2(): 
...     pass 
... 
>>> type(f2) 
<class 'function'> 
>>>

>>> type(lambda a:a+10) 
<class 'function'> 

#  None   
>>> def f3(): 
...     pass 
... 
>>> type(f3()) 
<class 'NoneType'> 
>>> 
>>>

#  bool  
>>> type("AB" == "AB") 
<class 'bool'> 
>>> 
>>> type("AB" != "AB") 
<class 'bool'> 
>>> 
>>> type("AB" != "ABC") 
<class 'bool'> 
>>> 
>>> type("python" in "programming python") 
<class 'bool'> 
>>> 
>>> type(True) 
<class 'bool'> 
>>>

#  type  
>>>type(int) 
<class 'type'> 

>>>type(float) 
<class 'type') 
>>>type(str) 
<class 'type'> 

>>>type(bytes) 
<class 'type'>  

>>> type(list)  
<class 'type'>  

>>> type(tuple)  
<class 'type'>  

>>> type(set)  
<class 'type'> 

>>>type(dict) 
<class 'type')

  
# module 
sys     |sys.path| sys.path[index] 
module |  list  |  str  

>>> import sys 
>>> type(sys) 
<class 'module'> 
>>> 

>>> type(sys.path)  
<class 'list'> 

>>> type(sys.path[0]) 
<class 'str'> 
>>> 

>>> import os 
>>> type(os) 
<class 'module'> 
>>> 

>>> import bs4 
>>> type(bs4) 
<class 'module'> 
>>> 

>>> type(bs4.BeautifulSoup) 
<class 'type'> 
>>> 

>>> type(bs4.BeautifulSoup()) 
<class 'bs4.BeautifulSoup'> 

# cgi    | cgi.FieldStorage() | cgi.FieldStorage().keys() 
# module       |  class|  class instance. Method () 
#   

>>> import cgi 
>>> type(cgi) 
<class 'module'> 
>>> 

>>> type(cgi.FieldStorage()) 
<class 'cgi.FieldStorage'> 
>>> 

>>> type(cgi.FieldStorage().keys()) 
<class 'list'>
Tags:
Share:
Comments
Write a comment
Your email address will not be published. Required fields are marked *
Review*
Name*
Email*
Notify me of new posts by email