1.函数的定义
def one(): print("123456") print("123456") one() one()
结果:
123456 123456 123456 123456
2.函数的形参、实参应用
def one(frist,last): print("你好",frist) if last>100: print("你考试考的很好") else: print("你考试还需努力") one("李明",120) one("小美",80)
结果:
你好 李明 你考试考的很好 你好 小美 你考试还需努力
3.函数弄个简易的计算器
def number(one,two,three): if two=="+": print(one+three) elif two=="-": print(one-three) elif two=="*": print(one*three) elif two=="/": print(one/three) else: print("输入错误") number(100,"+",100) number(100,"-",100) number(100,"*",100) number(100,"/",100) number(100,"+-/(",100)
结果:
200 0 10000 1.0 输入错误
4.不同形式的实参使用
def chi(zhu,fu,tang): print(zhu,fu,tang) chi(zhu="米饭",fu="果汁",tang="胡辣汤") chi("米饭","果汁","胡辣汤")
结果:
米饭 果汁 胡辣汤 米饭 果汁 胡辣汤
5.固定形参的使用
def student(name,age,gender="男"): print(name,age,gender) student("张海",18) student("宋浩",30) student("猫女",20,"女")
结果:
张海 18 男 宋浩 30 男 猫女 20 女
6.动态形参
def chi(*food): print(food) chi('apple', 'banana', ) chi('apple', 'banana','something') chi('今天不吃了')
结果:
('apple', 'banana') ('apple', 'banana', 'something') ('今天不吃了')
7.进阶动态形参
def one(a,b,*range,c="python",**nummber): print(a,b,c,range,nummber) one(1,2,3,4,5,litter=123,human=987)
结果:
1 2 python (3, 4, 5) {'litter': 123, 'human': 987}
8.简单案例:
def one(*two,**three): print(two,three) one() one(1) one(1,2,3,4,5,four=123) one(1.2,3,4,5,6,7,8,five='12345',six='6789')
结果:
() {} (1,) {} (1, 2, 3, 4, 5) {'four': 123} (1.2, 3, 4, 5, 6, 7, 8) {'five': '12345', 'six': '6789'}标签:形参,Python,chi,参与,three,two,print,100,def From: https://blog.csdn.net/2301_80454440/article/details/137402169