python - 函数
# -*- coding:utf-8 -*-
'''
@project: jiaxy
@author: Jimmy
@file: study_函数.py
@ide: PyCharm Community Edition
@time: 2018-11-08 09:08
@blog: https://www.cnblogs.com/gotesting/
'''
# 函数
# append pop insert len range print input int str list replace strip find split
s = '1234567890ABCDEFGHIJKLMN'
print(len(s))
'''
def 函数名(参数):
代码块
return 变量的个数
'''
def sums(a,b):
sums = 0
for i in range(a,b):
sums += i
return sums
count = sums(1,100)
print(count)
# 参数可以有0个,也可以有多个
# 参数分为位置参数(形参)、动态参数、默认参数、关键字参数
# 默认值必须放在 位置参数后面
# 参数,按顺序赋值
# 指定关键字,关键字跟参数名保持一致
# return表示当前函数已终止
def hello(name='Jimmy',content='Hello'):
print('{},{}!'.format(name,content))
return name
hello()
hello('Tom')
hello('Jack','Hi')
hello(content='Marry',name='Good')
t = hello('abcd','defg')
print(t)
# 动态参数===不定常参数===不定个数===想输入几个就输入几个
# 动态参数 *变量名 args=arguments
def coding(*args):
# print(args) #不定长参数变成了元组
return args
c = coding('python','java','c','ruby','go')
print(c)
# 关键字参数 **kwargs --- key word agruments
def student_info(**kwargs):
print(kwargs) #变成了字典
student_info(name='Jimmy',age=18,lover='study')
# 各类型参数可以混合使用,但要注意顺序
# 位置,动态,默认,关键字
def print_msg(a,*args,b=10,**kwargs):
print('a',a)
print('b',b)
print('args',args)
print('kwargs',kwargs)
print_msg(1,2,3,4,5,6)