计算机网络/计算机科学与应用/系统/运维/开发

Python 函数

一、函数的基本概念

1、定义函数

def 函数名称(参数值1[,参数值2,...]):

"""函数批注"""

        程序代码

        return [返回值1, 返回值2,...]

无参数无返回值的函数

def greeting():

        """第一个python函数设计"""

        print("hello python")

# 调用函数

greeting()

2、函数参数

def greeting(name):

     """python函数需传递名字name"""

     print("hi",name)

greeting('Nelson')

多个参数传递需要注意传递参数的位置正确

def subtract(x1,x2):

     """减法设计"""

     result = x1 - x2

     print(result)

print("该程序执行 a-b 的运算")

a = int(input("a="))

b = int(input("b="))

print("a-b=",end="")

subtract(a,b)

3、关键词参数  参数名称=值

def interest(interest_type, subject):

      """显示兴趣和主题"""

      print("我的兴趣是"+ interest_type)

   

interest(interest_type = '旅游', subject = '敦煌')


无论人生上到哪一层台阶,阶下有人在仰望你,阶上亦有人在俯视你,你抬头自卑,低头自得,唯有平视,才能看见真实的自己。

评论

^