zoukankan      html  css  js  c++  java
  • Day3

    1.函数基本语法及特性

    函数是什么?

    函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可

     1 while True:
     2     if cpu利用率 > 90%:
     3         #发送邮件提醒
     4         连接邮箱服务器
     5         发送邮件
     6         关闭连接
     7   
     8     if 硬盘使用空间 > 90%:
     9         #发送邮件提醒
    10         连接邮箱服务器
    11         发送邮件
    12         关闭连接
    13   
    14     if 内存占用 > 80%:
    15         #发送邮件提醒
    16         连接邮箱服务器
    17         发送邮件
    18         关闭连接

    优化之后

     1 def 发送邮件(内容)
     2     #发送邮件提醒
     3     连接邮箱服务器
     4     发送邮件
     5     关闭连接
     6   
     7 while True:
     8   
     9     if cpu利用率 > 90%:
    10         发送邮件('CPU报警')
    11   
    12     if 硬盘使用空间 > 90%:
    13         发送邮件('硬盘报警')
    14   
    15     if 内存占用 > 80%:
    16         发送邮件('内存报警')

    特性:

    1. 减少重复代码
    2. 使程序变的可扩展
    3. 使程序变得易维护

    语法:

    #定义函数

      def 函数名():

        函数体

    #执行函数

      函数名()

    创建函数:

    1 name = 'francis'
    2 def a():
    3     print('aaa')
    4 a()
    View Code

    发送邮件示例:

     1 def send_mail():
     2     import smtplib
     3     from email.mime.text import MIMEText
     4     from email.utils import formataddr
     5 
     6     msg = MIMEText('邮件内容', 'plain', 'utf-8')
     7     msg['From'] = formataddr(["赤脚大仙儿",'benet_chentao@163.com'])
     8     msg['To'] = formataddr(["小三儿",'66660929@qq.com'])
     9     msg['Subject'] = "主题"
    10 
    11     server = smtplib.SMTP("smtp.163.com", 25)
    12     server.login("benet_chentao@163.com", "邮箱密码")
    13     server.sendmail('benet_chentao@163.com', ['66660929@qq.com',], msg.as_string())
    14     server.quit()
    View Code

    返回值:

    1 def a():
    2     print("aaa") 
    3     #函数执行到return的时候,立即终止,并把return的结果复制给函数本身
    4     return ""  
    5     #如果没有return,默认赋值None给函数本身
    6 print(a)
    View Code

    注意事项:

    1、def 关键字,创建函数

    2、函数名():        #和变量起名字类似

    3、函数体

    4、返回值

      函数执行到return的时候,立即终止

      把return的结果赋值给函数本身

      如果没有return,默认赋值None给函数本身

      这个值可以用来作为判断条件,也可以作为数据处理







  • 相关阅读:
    DNNClassifier 深度神经网络 分类器
    浏览器对MP4视频 帧宽度 高度的兼容性
    UnicodeEncodeError:'latin-1' codec can't encode character
    文件夹下 文件计数
    the largest value you actually can transmit between the client and server is determined by the amount of available memory and the size of the communications buffers.
    the “identity” of an object
    广告特征 用户特征
    如果一个维度全覆盖,则有效维度应该对该维度全覆盖
    a high-level neural networks AP
    使用 LDA 挖掘的用户喜好主题
  • 原文地址:https://www.cnblogs.com/francis818/p/5762201.html
Copyright © 2011-2022 走看看