class Point(object):
def __init__(self,x:int,y:int):
self.x = x
self.y = y
class Rectangle(object):
def __init__(self,top_left:Point,bottom_right:Point):
self.top_left = top_left
self.bottom_right = bottom_right
def get_area(self):
length = abs(self.bottom_right.x - self.top_left.x)
width = abs(self.top_left.y - self.bottom_right.y)
return length * width
def is_inside(self,point):
if self.bottom_right.x >= point.x >= self.top_left.x and self.top_left.y >= point.y >= self.bottom_right.y:
return True
else:
return False
p1 = Point(4,20)
p2 = Point(30,8)
r = Rectangle(p1,p2)
print(r.get_area())
p = Point(10,13)
print(r.is_inside(p))
pp = Point(4,19)
print(r.is_inside(pp))
class Auto(object):
def __init__(self,color,weight,speed=0,wheel_count=4):
self.color = color
self.weight = weight
self.speed = speed
self.wheel_count = wheel_count
def chamge_speed(self,x):
if x == 0:
self.speed = 0
return
self.speed += x
class CarAuto(Auto):
def __init__(self,color,weight,ac,navigator,speed=0,wheel_count=4):
super(CarAuto,self).__init__(color,weight,speed,wheel_count)
self.navigator = navigator
self.ac = ac
car = Auto('白色',1.6)
car.chamge_speed(-10)
print(car.speed)
car.chamge_speed(100)
print(car.speed)
car.chamge_speed(0)
print(car.speed)
car = CarAuto('白色',1.6,'美的','Android')
file = open('xxx.txt',encoding='utf8')
print(file.read())
print(file.readline())
file.readlines()
file.close()
import csv
file = open('test.csv','r',encoding='utf8',newline='')
r = csv.reader(file)
for data in r:
print(data)
file.close()
from io import StringIO,BytesIO
s_io = StringIO()
s_io.write('hello')
s_io.write('good')
print(s_io.getvalue())
s_io.close()
s = ''
s += 'hello'
s += 'world'
s += 'python'
print(s)
s_io = StringIO()
print('hello',file=s_io)
print('helloo',file=s_io)
print('hellooo',file=s_io)
print(s_io.getvalue())
s_io.close()
b_io = BytesIO()
b_io.write('你好'.encode('utf8'))
print(b_io.getvalue())
print(b_io.getvalue().decode('utf8'))
b_io.close()
import sys
import json
names = ['zss','lisi','jack','tony','shy']
x = json.dumps(names)
print(x)
file = open('names.txt','w',encoding='utf8')
json.dump(names,file)
file.close()
x = '{"name":"lisi", "age":"18"}'
p = json.loads(x)
print(p)
x = open('names.txt','r',encoding='utf8')
p = json.load(x)
print(p)
x.close()
import pickle
names = ['zss', 'lisi', 'jack', 'tony', 'shy']
b_names = pickle.dumps(names)
print(b_names)
file = open('names.txt','wb')
file.write(b_names)
file.close()
file1 = open('names.txt','rb')
x = file1.read()
y = pickle.loads(x)
print(y)
file1.close()
file2 = open('names.txt','wb')
pickle.dump(names,file2)
file2.close()
file3 = open('names.txt','rb')
pickle.load(file3)
file3.close()
class Dog(object):
def __init__(self,name,color):
self.name = name
self.color = color
def eat(self):
print(self.name + '在吃东西')
d = Dog('大黄','白色')
pickle.dump(d,open('dog.txt','wb'))
dd = pickle.load(open('dog.txt','rb'))
dd.eat()
print(dd.name,dd.color)
info = '{"name":"zss", "age":18, "height":1.75,"pass": true,"hobbies": ["sing","dance","basketball","rap"]}'
person = json.loads(info)
print(person)
print(person['name'])
result = {'success':True}
x = json.dumps(result)
print(x)
def div(a,b):
return a / b
try:
x = div(5,0)
except Exception as e:
print('程序出错了')
print(e)
else:
print('计算结果是',x)
print(22)
age = input('请输入你的年龄:')
try:
age = float(age)
except Exception as e:
print('输入的不是数字')
else:
if int(age) > 18:
print('欢迎来到我的网站')
else:
print('未满18')
转载请注明原文地址:https://blackberry.8miu.com/read-32297.html