In [1]:
l = [1,2,3]
l.count(2)


Out[1]:
1

In [2]:
l.pop(1)
l


Out[2]:
[1, 3]

In [3]:
print(type(1))
print(type([]))
print(type(()))
print(type({}))


<class 'int'>
<class 'list'>
<class 'tuple'>
<class 'dict'>

In [4]:
# create a new object type called Sample
class Sample(object):
    pass

# instance of sample
x = Sample()

print(type(x))


<class '__main__.Sample'>

In [12]:
class Dog(object):
    # Class Object Attribute
    species = 'mammal'
    
    
    def __init__(self,breed,name):
        self.breed = breed
        self.name = name

In [13]:
sam = Dog('Lab','Sam')

In [14]:
sam.name


Out[14]:
'Sam'

In [15]:
sam.species


Out[15]:
'mammal'

In [16]:
class Circle(object):
    pi = 3.1415
    
    # Circle get instantiated with a radius (default is 1)
    def __init__(self, radius = 1):
        self.radius = radius
        
    # Area method calculates the area. Note the use of self.
    def area(self):
        return self.radius * self.radius * Circle.pi
    
    # Method for resetting Radius
    def setRadius(self, radius):
        self.radius = radius
    
    # Method for getting radius (Same as just calling .radius)
    def getRadius(self):
        return self.radius
    
c = Circle()
c.setRadius(2)
print('radius is: ', c.getRadius())
print('area is: ', c.area())


radius is:  2
area is:  12.566

In [17]:
c = Circle()
print('radius is: ', c.getRadius())
print('area is: ', c.area())


radius is:  1
area is:  3.1415

In [16]:
# create a new object type called sample
class Dog(object):
    # class object attribute
    species = 'mammal'
    
    def __init__(self,breed,name='stinky'):
        self.breed = breed
        self.name = name

# instance of sample
sam = Dog('Lab')

In [17]:
print(sam.breed)
print(sam.name)
print(sam.species)


Lab
stinky
mammal

In [18]:
class Circle(object):
    pi = 3.1415
    
    def __init__(self, radius=1):
        self.radius = radius
        
    def area(self):
        return self.radius * self.radius * Circle.pi

    def setRadius(self, radius):
        self.radius = radius

    def getRadius(self):
        return self.radius

c = Circle()

c.setRadius(2.109583)
print('Radius is: ',c.getRadius())
print('Area is: ',c.area())


Radius is:  2.109583
Area is:  13.980744473062297

In [21]:
class Attire():
    clothing = 'suit'
    
    def __init__(self,bald='no'):
        self.bald = bald
        
    def test_function(self):
        return print(self.bald*10)
rich = Attire()

In [22]:
rich.bald


Out[22]:
'no'

In [23]:
rich.clothing


Out[23]:
'suit'

In [25]:
rich.test_function()


nononononononononono

In [26]:
class Animal(object):
    def __init__(self):
        print('Animal Created')
    
    def whoAmI(self):
        print('Animal')
    
    def eat(self):
        print('Eating')

class Dog(Animal):
    def __init__(self):
        Animal.__init__(self)
        print('Dog created')
    
    def whoAmI(self):
        print('Dog')
        
    def bark(self):
        print('Woof!')

d = Dog()


Animal Created
Dog created

In [27]:
d.whoAmI()


Dog

In [29]:
d.bark()


Woof!

In [30]:
class Book(object):
    def __init__(self, title, author, pages):
        print('a book is created')
        self.title = title
        self.author = author
        self.pages = pages
    
    def __str__(self):
        return 'title:%s, author:%s, pages:%s ' %(self.title, self.author, self.pages)
    
    def __len__(self):
        return self.pages
    
    def __del__(self):
        print('a book is destroyed')

In [31]:
book = Book('python rocks!', 'rich burnside', 159)


a book is created

In [32]:
print(book)


title:python rocks!, author:rich burnside, pages:159 

In [34]:
print(len(book))


159

In [35]:
del book


a book is destroyed