栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

02-python补充2

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

02-python补充2

目录

自己定义的结构

关于产品设计

def定义函数三部曲

class类

机器学习典型的封装模型

选择和循坏

 if

while & break


自己定义的结构
  1. def 函数
  2. class 类(后面的模型都会封装成类)
  3. module 模块(py文件)
  4. package包(文件夹,放很多py文件)

关于产品设计
  1. 用户是邪恶的
  2. 用户是傻的

def定义函数三部曲
  1. 参数校验
  2. 处理逻辑
  3. 返回值
#定义函数,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.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/499339.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号