Codecademy Python Answers Classes

Also Read: How To Chat With Your Friend Using Command Prompt

Classes 1/11 – Class Basics

class Car(object):
    pass

Classes 2/11 – Create an Instance of Class

class Car(object):
    pass
my_car = Car()

Classes 3/11 – Class member Variables

class Car(object):
    condition = "new"
my_car = Car()

Classes 4/11 – calling Class member Variables

class Car(object):
    condition = "new"
my_car = Car()
print my_car.condition

Classes 5/11 – Initializing a class

class Car(object):
    condition = "new"
    def __init__(self,model,color,mpg):
        self.model = model
        self.color = color
        self.mpg = mpg
my_car = Car("DeLorean","silver",88)
print my_car.condition

Classes 6/11 – Reffering to a member Variable

class Car(object):
    condition = "new"
    def __init__(self,model,color,mpg):
        self.model = model
        self.color = color
        self.mpg = mpg
my_car = Car("DeLorean","silver",88)
print my_car.condition
print my_car.model
print my_car.color
print my_car.mpg

Classes 7/11 – Creating Class Methods

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg  = mpg
        
    def display_car(self):
        return "This is a %s %s with %s MPG."%(self.color,self.model,str(self.mpg))

my_car = Car("DeLorean", "silver", 88)
print my_car.display_car()

Classes 8/11 – Modifying Member Variables

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg  = mpg
        
    def display_car(self):
        return "This is a %s %s with %s MPG."%(self.color,self.model,str(self.mpg))
    def drive_car(self):
        self.condition = "used"
my_car = Car("DeLorean","silver", 88)
print my_car.condition
my_car.drive_car()
print my_car.condition

Classes 9/11 – Inheritance

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg = mpg

    def display_car(self):
        return "This is a %s %s with %s MPG. " % (self.color, self.model, self.mpg)

    def drive_car(self):
        self.condition = "used"

class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        super(ElectricCar, self).__init__(model, color, mpg)
        self.battery_type = battery_type

    def display_car(self):
       inherit_Str = super(ElectricCar, self).display_car()
       return inherit_Str + "It has a %s battery. " % (self.battery_type)

my_car = ElectricCar("Ford", "black", 95,"molten salt")
print my_car.display_car()

Classes 10/11 – Overriding Methods

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg = mpg

    def display_car(self):
        return "This is a %s %s with %s MPG. " % (self.color, self.model, self.mpg)

    def drive_car(self):
        self.condition = "used"

class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        super(ElectricCar, self).__init__(model, color, mpg)
        self.battery_type = battery_type

    def display_car(self):
       inherit_Str = super(ElectricCar, self).display_car()
       return inherit_Str + "It has a %s battery. " % (self.battery_type)
    def drive_car(self):
        self.condition = "like new"

my_car = ElectricCar("some", "any", 777, "molten salt")

print my_car.condition
my_car.drive_car()
print my_car.condition

my_car = ElectricCar("Ford", "black", 95,"molten salt")
print my_car.display_car()

Classes 11/11 – Building Useful Classes

class Point3D(object):
    
    
    def __init__(self,x,y,z):
        self.x = x
        self.y = y
        self.z = z
    def __repr__(self):
        return str((self.x,self.y,self.z))
my_point = Point3D(1,2,3)
print my_point
Also Read :   Codecademy Python Answers Pyglatin

Leave a Reply

Your email address will not be published. Required fields are marked *