Quantcast
Channel: sunny
Viewing all articles
Browse latest Browse all 44

[Python] codecademy-20140324

$
0
0

+ Inheritance

# parent class
class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    # add class method
    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"

## child class
class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        self.model = model
        self.color = color
        self.mpg = mpg
        self.battery_type = battery_type
        
        
my_car = ElectricCar("DeLorean", "silver", 88, "molten salt")
print my_car.condition
print my_car.model
print my_car.color
print my_car.mpg
print my_car.battery_type

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

+ Override

# parent class
class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    # add class method
    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"

# child class
class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        self.model = model
        self.color = color
        self.mpg = mpg
        self.battery_type = battery_type
    
    # override
    def drive_car(self):
        self.condition = "like new"
        
        
my_car = ElectricCar("DeLorean", "silver", 88, "molten salt")
print my_car.condition
print my_car.model
print my_car.color
print my_car.mpg
print my_car.battery_type

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

+ 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 "(%d, %d, %d)" % (self.x, self.y, self.z)
        

my_point = Point3D(1, 2, 3)
print my_point # (1, 2, 3)

+ see it to believe it
file i/o

my_list = [i**2 for i in range(1,11)]
# Generates a list of squares of the numbers 1 - 10

f = open("output.txt", "w")

for item in my_list:
    f.write(str(item) + "\n")

f.close()

+ The open() function

# w - write-only mode
# r - read-only mode
# r+ - read and write mode
# a - append mode
my_file = open("output.txt", "r+")

+ Writing

my_list = [i**2 for i in range(1,11)]

my_file = open("output.txt", "r+")

# Add your code below!
for item in my_list:
    my_file.write(str(item) + "\n")

# close
my_file.close()

# reding

my_file = open("output.txt", "r")

print my_file.read()

my_file.close()

+ Reading between the lines

my_file = open("text.txt", "r")

print my_file.readline()
print my_file.readline()
print my_file.readline()

my_file.close()

+ PSA: Buffering Data

# Open the file for reading
read_file = open("text.txt", "r")

# Use a second file handler to open the file for writing
write_file = open("text.txt", "w")
# Write to the file
write_file.write("Not closing files is VERY BAD.")

# add
write_file.close()

# Try to read from the file
print read_file.read()
# add
read_file.close()

+ The ‘with’ and ‘as’ keywords

with open("text.txt", "w") as textfile:
	textfile.write("Success!")

+ Case closed

with open("text.txt", "w") as my_file:
    my_file.write("Hello Python!")
    
    if not my_file.closed:
        my_file.close()
    
    print my_file.closed

Viewing all articles
Browse latest Browse all 44

Trending Articles