python学习 函数 一起敲敲键盘

"""
函数 同c
def 函数名称(参数列表):
    函数体#可递归调用
    [return返回值列表]#可出现在任意位置,结束函数;可return多个值,结果为一元组→解包赋值 variable1,variable2,variable3...=函数名称(参数列表)

函数名称(参数列表)#参数列表可嵌套函数

函数内部定义局部变量
程序开头定义全局变量or函数内部关键字定义全局变量 global 变量名
函数内同名全局变量和局部变量的使用,局部变量优先级高

lambda无名函数,可简化只有一句代码且只有一个返回值的函数体
result=lambda 参数列表:表达式

常用内置函数
类型转换函数
bool(obj)
str(obj)
int(x)
float(x)
list(sequence)
tuple(sequence)
set(sequence)
数学函数
abs(x) x的绝对值
divmod(x,y) x与y的商和余数,结果为一元组(商,余数)
max(sequence) sequence的最大值,按ascii计数
min(sequence) sequence的最小值
sum(iter) 对可迭代对象求和
pow(x,y) x的y次幂
round(x,d) 对x保留d位小数,默认保留整数
迭代器操作函数(常见可迭代对象:集合数据类型,如list、tuple、dict、set、str等;生成器(generator),包括生成器和带yield的生成器函数(generator function)。
sorted(iter)
reversed(sequence) 反转序列生成新的迭代器对象
zip(iter1,iter2) 将iter1和iter2打包成元组并返回一个可迭代的zip对象
enumerate(iter,start=0) 根据iter创建一个enumerate对象,start为下标起始位置的值
all(iter) 判断iter中所有元素是否都为true
any(iter) 判断iter中所有元素是否都为false,是则返回 False,如果有一个为 True,则返回 True
next(iter) 获取迭代器下一元素
filter(function,iter) 通过指定条件过滤序列并返回一个迭代器对象
map(function,iter) 通过function对iter操作,返回一个迭代器对象
其他
format(value,format_spec)
len(s)
id(obj)
type(x)
eval(s)
"""
def joint0(who,number0,what,how,number1):
    string=who+str(number0)+'\t'+what+'\tby\t'+how+str(number1)
    return string

who=input()
number0=eval(input())
what=input()
how=input()
number1=eval(input())
print(joint0(who,number0,what,how,number1))#位置传参
print(joint0(number1=number1,how=how,what=what,number0=number0,who=who))#关键字传参
print(joint0(who,number0,number1=number1,how=how,what=what))#位置传参,关键字传参共用,位置传参在前,关键字传参在后
#print(函数) 打印return值,若无return值,则打印None
def output(item):
    print(item)
    return item
print(output(input('item:')))#print打印两次
def joint1(who='pycharm',number0=2024,what='print hello world',how='python',number1=3.12):#默认值传参
    print(who+str(number0)+'\t'+what+'\tby\t'+how+str(number1))

joint1()
joint1(how,number1)#位置传参,默认值传参共用,位置优先
def traverse0(*element):#* 参数个数可变函数
    print(type(element))
    for item in element:
        print(item)

traverse0(1,2,3)
traverse0(*range(1,4))#* 参数个数可变函数系列解包操作
def traverse1(**element):#* 个数可变的关键字参数函数
    print(type(element))
    for key,value in element.items():
        print(key,'-'*5,value)

traverse1(who='python',number0=3.12,what='print hello world by pycharm',number1=2024)
dictionary0={'who': 'python', 'number0':3.12, 'what': 'print hello world by pycharm', 'number1':2024}#keywords must be strings
traverse1(**dictionary0)#** 个数可变的关键字参数函数系列解包操作

get_sum=lambda a, b: a + b
print(get_sum(1, 2), type(get_sum))#<class 'function'>函数类型

dictionary0={3:0, 2:1, 1:2, 0:3}
for i in range(len(dictionary0)):
    result=lambda x:x.get(i)#遍历
    print(result(dictionary0))
dictionary0_new=sorted(dictionary0.items(), key=lambda x:x[0])#排序 x[0]按key排
print(dictionary0_new, type(dictionary0_new))
print(dict(dictionary0_new))
dictionary1={0:3, 1:2, 2:1, 3:0}
dictionary1_new=sorted(dictionary1.items(),key=lambda x:x[1])#排序 x[1]按value排
print(dictionary1_new)
def fbnq(n):#斐波那契数列
    if n==1 or n==2:
        return 1
    else:
        return fbnq(n-1)+fbnq(n-2)

list_fbnq=[]
for i in range(1,10):
    list_fbnq.append(fbnq(i))
print(list_fbnq)

print(abs(-1),abs(0),abs(1))
print(divmod(13,4),type(divmod(13,4)))
print(max(list_fbnq),max('hello world'))
print(min(list_fbnq),min('hello world'))
print(sum(list_fbnq))
print(pow(2,3))
print(round(3.1415926),round(3.1415926,3),round(314.15926,-1))


print(type(reversed(list_fbnq)),list(reversed(list_fbnq)))
print(type(enumerate(list_fbnq,1)),tuple(enumerate(list_fbnq,1)))
print(all(list_fbnq),all(['h','e','l','l','o','','w','o','r','l','d']))
print(any(list_fbnq),any([0]))
package=zip(list_fbnq, ['python', 'print', 'hello', 'world'])
for i in range(len(['python','print','hello','world'])):
    print(next(package))
def odd(number):
    return number%2!=0

print(type(filter(odd,list_fbnq)),list(filter(odd,list_fbnq)))
print(type(map(odd,list_fbnq)),list(map(odd,list_fbnq)))

print(str(format(3.14,'10')),'\n',format('hello','10'))#数字默认右对齐,字符默认左对齐
print(format('hello','*<10'),'\n',format('hello','*>10'),'\n',format('hello','*^10'),'\n',str(format(16,'*^10b')))#可自定义

运行结果

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值