python函数基础

1、函数的定义

def 函数名([参数]):
函数体
[return 返回值]

dic = {'name':'JOJO','age':'18','sex':'man'}

def getDicValue(dic):
  temp = len(dic)
  return temp;

temp = getDicValue(dic)
print(temp)
3

2、函数的传参方式

1、位置传参
2、关键字传参
3、变量传参

#-*- coding:UTF-8 -*-
dic = {'name':'JOJO','age':'18','sex':'man'}
def getDicValue(temp1,temp2):
  if(temp1 == 1 and temp2 ==2):
    temp = True
  else:
    temp = False
  return temp
temp1 = getDicValue(1,2)#位置传参
temp2 = getDicValue(temp1=1,temp2=2)#关键字传参
a = 1
b = 2
temp3 = getDicValue(a,b)#变量传参

print(temp1,temp2,temp3)
True True True

3、函数的其他功能

1、函数的注释
使用’’’ ‘’’ 可以进行函数的注释,使用help(函数名)可以查看注释

def getDicValue(temp1,temp2):
  '''
  这个函数是用来测试的
  '''
  if(temp1 == 1 and temp2 ==2):
    temp = True
  else:
    temp = False
  return temp
print(help(getDicValue))
getDicValue(temp1, temp2)
    这个函数是用来测试的

2、函数的健壮性
判断传递的参数类型是否正确

if not(type(temp1) == int and type(temp2)==int):
    print("参数必须为整型")
    return

3、参数的默认值
有默认值的参数一定要放在没有默认值的参数的后面

def getDicValue(temp1,temp2 = 1):
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值