函数初探
Posted jly1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了函数初探相关的知识,希望对你有一定的参考价值。
函数的定义.返回值.参数
#len()
s = "jinlaoban"
#len()
# i = 0
# for k in s:
# i += 1
# print(i)
# s1 = "ahdsajks"
#
# i = 0
# for k in s1:
# i += 1
# print(i)
s = "jinlaoban"
# def my_len(): #personal function
# i = 0
# for k in s:
# i += 1
# # print(i)
# return i #return
#
# length = my_len()
# print(length)
#function
# define function ,you can use in anywhere
#no return just only print
#the importance of return
#a,b
#len(a) #inner function
#len(b)
# the function define and transfer is not porcess
#Return .the return has three situation
#1.no return no return , just return :end the function, return None
# 2.return only one results
#it can return any types
# if return it can accept
# it will stop while meet the return
# 3.return many results
# many results matching the return
#only one variable can accept as turple
# def func():
# l = ["jinlaoban", "erge"]
# for i in l:
# print(i)
# if i == "jinlaoban":
# return i
# # return
# # print("ergeerge")
#
# ret = func()
# print(ret)
# def func():
#
# return {"k":"v"}
# return 1
#
# print(func())
#Return many results
# def func2():
# return 1, 2, 3
#
# r1 = func2()
#
# print(r1)
# s = "jinlaoban"
# def my_len(s): #personal function s------>resembling parameter(形式参数)
# i = 0
# for k in s:
# i += 1
# # print(i)
# return i
#
# s = "jinlaoban"
# ret = my_len(s) #actual parameter(实际参数)
# print(ret)
# ret = my_len([1,2,3,4,5])
# print(ret)
# my_len(ret) #deliver parameter
# def f1(l1):
# for i in l1:
# print(i)
#
# def f2(l1):
# f1(l1)
# for i in l1:
# print(i)
#
# f2([1,2,3])
# f1([1,2,3,4])
# f2([1,2,3,4])
#parameter
# no parameter-------->the define function and the transter function is just no things
# one parameter ------->transter something is something
# many parameters------->1.positions parameter 2.keywords transfer 3. mixture
# def my_sum(a, b):
# res = a + b
# return res
#
# ret = my_sum(b = 1,a = 2)
#
# print(ret)
# def classmate(name, sex):
# print("{} : {}" .format(name, sex))
#
# classmate("jinliaoyuan", "boy")
#use the default settings:
# def classmate(name, sex ="boy"):
# print("{} : {}" .format(name, sex))
# classmate("jinliaoyuan")
# classmate("zhangjiahui", "girl")
#Have problems
# def sum(a, b , c=0,):
# pass
#
# sum(1,2)
# sum(1,2,3)
# sum(1,2,3,4)
#dynamic parameter
# def sum(*args):
# n = 0
# for i in args:
# n += i
# return n
#
# sum(1,2)
# sum(1,2,3)
# sum(1,2,3,4)
# parameter sequence 1.positional parameter 2.dynamic parameter(*agrs), 3.default parameter
#problems transfer many dafault parameter -->turple
# def func(*agrs, l =[]):
# print(agrs, l)
#
#
# func(1,2,3, ["lat", 1])
# func(1,2,3,l = [1,2])
# #**kwargs --------->dic
# def func(**kwargs):
# print(kwargs)
#
# func(a=1,b=2, c=3)
# func(a=1, b =2)
# func(a=1)
# advantage : it can accept many parameter
# def func(*args, **kwargs):
# print(args,kwargs)
#
# func(1,2,3,4,5, a= "aaa", b = "bbb")
#position parameter, *args, "default parameter , **kwargs,
# def func(*args, default = 2, **kwargs):
# print()
#dynamic parameter have another methed
# def func(*args): #站在形参的角度上,给变量加上*,就是组合所有打散的值
# print(args)
#
# func(1,2,3,4)
# l = [1,2,3,4,5]
# # func(l[1],l[2],l[3])
# func(*l) #站在实参的角度上,给序号加上*,就是将这个序列按照顺序打散
# def func(**kwargs):
# print(kwargs)
#
# func(a=1,b=2)
# d = {"a":"1", "b":"2"}
# func(**d)
#函数注释
# def func():
# ‘‘‘ 这个函数实现了什么功能
# 参数1,
# 参数2:
# :return ,
# ‘‘‘
# pass
#默认参数的陷阱
#1.函数的定义 def
#2.函数的调用
#3.函数的返回值 return
#返回一个值,返回多个值可以用多个变量接收,也可以用一个变量接收
#函数的参数 形参和实参(按照位置传参,按照关键字传参,先按照位置,在按照关键字)
#形参:
#位置参数
#*args 可以接收任意多个位置参数
#默认参数 可以传可以不传
#**Kwargs:可以接收多个关键字参数
函数:
内置函数
自定义函数
以上是关于函数初探的主要内容,如果未能解决你的问题,请参考以下文章