Python学习笔记第13天
每日一句:愿每次回忆,对生活都不感到负疚。
将car.py进行优化分解,通过导入模块的方式进行实现
car.py
class Car():
"""一次模拟汽车的简单尝试"""
def __init__(self, make, model, year): # 制造商,型号,年份
"""初始化描述汽车的属性"""
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0 # 设定默认值,不用在形参中填写
def get_descriptive_name(self):
"""返回整洁的描述性信息"""
long_name = str(self.year) + ' ' + self.make + ' ' + self.model
return long_name.title()
def read_odometer(self):
"""打印一条关于汽车里程的信息"""
print("This car has " + str(self.odometer_reading) + " miles on it.")
def update_odometer(self, mileage):
"""1.将里程表读数设置为指定的值,2.禁止将里程表读数往回调"""
# 1.self.odometer_reading=mileage
# 2.修改1.
if mileage >= self.odometer_reading:
self.odometer_reading = mileage
else:
print("You can't roll back an odometer!")
def increment_odometer(self, miles):
"""将里程表读数增加指定的量"""
self.odometer_reading += miles
class Battery():
"""一次模拟电动汽车的简单尝试"""
def __init__(self, battery_size=70):
"""初始化电瓶的属性"""
self.battery_size = battery_size
def describe_battery(self):
"""打印一条描述电瓶容量"""
print("This car has a " + str(self.battery_size) + "-kwh battery.")
def get_range(self):
"""打印一条信息,指出电瓶的续航里程"""
if self.battery_size == 70:
range = 240
elif self.battery_size == 85:
range = 270
message = "This car can go approximately " + str(range)
message += " miles on a full charge."
print(message)
class ElectricCar(Car):
# 创建了子类ElectricCar,括号中是父类的名字
# 父类与子类必须在一个文件里,父类要在子类前面
"""电动汽车的独特之处"""
def __init__(self, make, model, year):
"""1.初始化父类的属性"""
"""2.电动汽车的独特之处
初始化父类的属性,再初始化电动汽车的属性
"""
super().__init__(make, model, year)
# 给子类定义属性和方法
# self.battery_size = 70
self.battery = Battery()
# def describe_battery(self):
# """打印一条描述电瓶容量的信息"""
# print("This car has a "+ str(self.battery_size) + "-kwh battery.")
def fill_gas_tank(self):
"""电动汽车没有油箱"""
print("This car doesn't need a gas tank!")
my_car.py
from car import Car
my_new_car = Car('audi', 'a4', 2016)
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()
print("--------")
# 修改属性方法一:直接修改属性的值
my_new_car.odometer_reading = 200
my_new_car.read_odometer()
2016 Audi A4
This car has 0 miles on it.
--------
This car has 200 miles on it.
my_electric_car.py
from car import Car,ElectricCar
my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()
2016 Tesla Model S
This car has a 70-kwh battery.
This car can go approximately 240 miles on a full charge.
my_cars.py
# 方法一
# from car import Car, ElectricCar
# my_beetle=Car('volkswagen', 'beetle', 2016)
# print(my_beetle.get_descriptive_name())
# my_tesla=ElectricCar('tesla', 'roadster', 2016)
# print(my_tesla.get_descriptive_name())
# 方法二
import car
my_beetle=car.Car('volkswagen', 'beetle', 2016)
print(my_beetle.get_descriptive_name())
my_tesla=car.ElectricCar('tesla', 'roadster', 2016)
print(my_tesla.get_descriptive_name())
2016 Volkswagen Beetle
2016 Tesla Roadster
标准库的使用
from collections import OrderedDict
favorite_languages=OrderedDict()
favorite_languages['jen']='python'
favorite_languages['sarah']='c'
favorite_languages['edward']='rudy'
favorite_languages['phil']='python'
for name,language in favorite_languages.items():
print(name.title()+"'s favorite language is "+language.title()+".")
Jen's favorite language is Python. Sarah's favorite language is C. Edward's favorite language is Rudy. Phil's favorite language is Python.
文件
从文件中读取数据
with open('pi_digits.txt') as file_object:
contents=file_object.read()
print(contents)
# open(x):打开文件,x为文件名,返回给file_object
# read():读取file_object所存储的所有数据
# 逐条读取
filename='pi_digits.txt'
with open(filename) as file_object:
for line in file_object:
print(line)
print(line.rstrip()) # 去除空格
# 创建一个包含文件每行内容的列表
filename='pi_digits.txt'
with open(filename) as file_object:
lines=file_object.readlines()
print(lines)
for line in lines:
print(line.rstrip())
# 使用文件的内容
# 读取的内容都会被转化为字符串
filename='pi_digits.txt'
with open(filename) as file_object:
lines=file_object.readlines()
pi_string=''
for line in lines:
pi_string+=line.strip()# 去除换行符
# 使用rstrip() 中间会有空格间隔
print(pi_string)
print(len(pi_string))
写入文件
# write_message.py
# 写入
filename='prohramming.txt'
with open(filename,'w') as file_object:
file_object.write("I love prohramming!")
# python只能将字符串写入文件,其他类型需要先转为字符串类型才能写入
# 读出
with open('prohramming.txt') as file_object:
contents=file_object.read()
print(contents)
# 写入多行
filename='prohramming_1.txt'
with open(filename,'w') as file_object:
file_object.write("I love prohramming!\n")
file_object.write("I like study !\n")
# 需要添加换行符,不然句子会挤在一起
# 读出
with open('prohramming_1.txt') as file_object:
contents=file_object.read()
print(contents)
# 附加到文件
filename='prohramming_1.txt'
with open(filename,'a') as file_object:
file_object.write("It's new message_1!\n")
file_object.write("It's new message_2!\n")
# 'a' 并非覆盖文件,而是添加进去
# 读出
with open('prohramming_1.txt') as file_object:
contents=file_object.read()
print(contents)

浙公网安备 33010602011771号