目录
自己定义的结构
关于产品设计
def定义函数三部曲
class类
机器学习典型的封装模型
选择和循坏
if
while & break
自己定义的结构
- def 函数
- class 类(后面的模型都会封装成类)
- module 模块(py文件)
- package包(文件夹,放很多py文件)
关于产品设计
- 用户是邪恶的
- 用户是傻的
def定义函数三部曲
- 参数校验
- 处理逻辑
- 返回值
#定义函数,a,b是未知参数
def my_add(a, b):
"""
求两数的和
"""
#1.参数校验
if not isinstance(a, (int,float)) or not isinstance(b, (int,float)):#反着写
#判断参数a是否是整数或者浮点数
#isinstance判断数是否符合类型
raise Exception("参数类型有误!")
#抛异常,对用户更加友好
#2.处理逻辑
result= a + b
#3.返回值
return resul
#关键字命名法,参数的赋值可以变化
my_add(b=3, a=6)
#普通直接写比较方便
my_add(6,3)
#默认参数
def increase(a,increment=1):
return a + increment
#默认参数可以改变的
increase(2,3)
class类
class Animal(object):
"""
自定义一个动物类
"""
def __init__(self,color="White",height=0.5,weight=20):
"""
初始化属性
"""
print("正在初始化")
self.color = color
self.height = height
self.weight = weight
机器学习典型的封装模型
# 封装机器学习模型
class Model(object):
"定义一个机器学习模型"
def __init__(self):
"""定义超参数"""
pass
def fit(self,X,y):
"""训练"""
pass
def predict(self,X):
"""预测"""
pass
def score(self,X,y):
"""模型的评价"""
-
选择和循坏
if
# if循环
score = 87
if score >= 90:
print("秀儿")
elif score >= 85:
print("良儿")
elif score >= 60:
print("合格")
else:
print("挂了")
out:"良儿"
while & break
# while Ture循环 + break终止
idx = 0
while True:
idx += 1
print("Hello,world.")
if idx == 10:
break
out:
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
- 用户是邪恶的
- 用户是傻的
def定义函数三部曲
- 参数校验
- 处理逻辑
- 返回值
#定义函数,a,b是未知参数
def my_add(a, b):
"""
求两数的和
"""
#1.参数校验
if not isinstance(a, (int,float)) or not isinstance(b, (int,float)):#反着写
#判断参数a是否是整数或者浮点数
#isinstance判断数是否符合类型
raise Exception("参数类型有误!")
#抛异常,对用户更加友好
#2.处理逻辑
result= a + b
#3.返回值
return resul
#关键字命名法,参数的赋值可以变化
my_add(b=3, a=6)
#普通直接写比较方便
my_add(6,3)
#默认参数
def increase(a,increment=1):
return a + increment
#默认参数可以改变的
increase(2,3)
class类
class Animal(object):
"""
自定义一个动物类
"""
def __init__(self,color="White",height=0.5,weight=20):
"""
初始化属性
"""
print("正在初始化")
self.color = color
self.height = height
self.weight = weight
机器学习典型的封装模型
# 封装机器学习模型
class Model(object):
"定义一个机器学习模型"
def __init__(self):
"""定义超参数"""
pass
def fit(self,X,y):
"""训练"""
pass
def predict(self,X):
"""预测"""
pass
def score(self,X,y):
"""模型的评价"""
-
选择和循坏
if
# if循环
score = 87
if score >= 90:
print("秀儿")
elif score >= 85:
print("良儿")
elif score >= 60:
print("合格")
else:
print("挂了")
out:"良儿"
while & break
# while Ture循环 + break终止
idx = 0
while True:
idx += 1
print("Hello,world.")
if idx == 10:
break
out:
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
class Animal(object):
"""
自定义一个动物类
"""
def __init__(self,color="White",height=0.5,weight=20):
"""
初始化属性
"""
print("正在初始化")
self.color = color
self.height = height
self.weight = weight
机器学习典型的封装模型
# 封装机器学习模型
class Model(object):
"定义一个机器学习模型"
def __init__(self):
"""定义超参数"""
pass
def fit(self,X,y):
"""训练"""
pass
def predict(self,X):
"""预测"""
pass
def score(self,X,y):
"""模型的评价"""
-
选择和循坏
if
# if循环
score = 87
if score >= 90:
print("秀儿")
elif score >= 85:
print("良儿")
elif score >= 60:
print("合格")
else:
print("挂了")
out:"良儿"
while & break
# while Ture循环 + break终止
idx = 0
while True:
idx += 1
print("Hello,world.")
if idx == 10:
break
out:
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
Hello,world.
选择和循坏
# if循环
score = 87
if score >= 90:
print("秀儿")
elif score >= 85:
print("良儿")
elif score >= 60:
print("合格")
else:
print("挂了")
out:"良儿"



