Python实参与形参(1)

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'}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值