python3自学之路-笔记15_函数的基本概念与使用
一、基本概念
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File : 函数基本概念.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date : 2019/3/15
#函数分为自带函数,三方函数,自定义函数 定义函数 def fucc(): 函数不管会否带参数都要有()
#函数传参数,可以带默认参数 def func(xx==oo)
#函数不定长参数1 *args 不字长元组
#函数不定长参数2 **kwargs 不定长字典
#函数指定传参数与顺序传参数
#函数传参可以值传递和地址传递
def act(*args):
print(args)
print(*args)
a=sum(args)
return a
def log(a,b,c):#参数要对应函数的key值
print(a)
print(b)
print(c)
def ast(**kwargs):
print(kwargs)
#print(**kwargs) #两个*拆包无法直接打印的,但是可以用
log(**kwargs)
act(1,2,3)
print(act(1,2,3))
ast(a=1,b=40,c=30)
二、使用
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File : 函数的使用.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date : 2019/3/15
#平时一般定义的函数传参为引用(地址)传递
#如果参数是可变类型,就会被改变。不可变类型就不会被改变
#函数返回值 return
def change(num):
print(id(num))
num=num+1
def change1(lis):
print(id(lis))
lis.append(22)
b=10
print(id(b))
change(b)
print(b)
c=[1,2,3]
print(id(c))
change1(c)
print(c)