Types and Data Structure
Notes
No primitivetypes in python- In python everything is
object objectis an instance of specificclass- There are
mutable(list, dict etc) andimmutable(str, int) object
Immutable objects
stringinstance ofstrclassbooleaninstance ofboolclassintegerinstance ofintclassdecimalnumber instance offloatclasstupleinstance oftupleclassNoneinstance ofNoneTypeclass
Mutable objects
listinstance oflistclassdictionaryinstance ofdictclasssetinstance ofsetclass- user defined class
Variables and objects
variables are simply pointer of location in memory, called id where objects are stored. It means variable contains a reference of the object.
to get the location of object in memory use id() function.
python
name = 'Kibria'
address_in_memory = id(name)
print(address_in_memory)It is possible multiple variables refer to the same object in memory. The contain same pointer. So the memory location will be same.
python
name = 'Kibria'
print(id(name))
# 14036459474849487
name_copy = kibria
print(id(name_copy))
# 14036459474849487
print(id(name) == id(name_copy))
# TrueHow to check the type of object?
use the type() function.
python
name = 'Kibria'
print(type(name))
# <class'str'>
age = 41
print(type(age))
# <class 'int'>How to check the type of class?
python
print(type(str))
# <class'type'>
print(type(int))
# <class'type'>
print(type(bool))
# <class'type'>How to check the instance of a class?
python
name = 'Kibria'
# name is an instance of str
# str is an instance of object
print(isinstance(name, str))
# True
print(isinstance(name, object))
# True
print(isinstance(name, int))
# FalseHow to see all attributes of a classs or object?
python
name = 'Kibria'
print(dir(name))
# return a list of all attributes of name