8.9第五周学习记录
第五周学习记录
这周学习了以下内容:
1.形参与实参
2.未知函数
3.关键字函数
4.为参数设置默认值
5.可变参数
6.返回值
7.局部变量
8.全局变量
9.匿名函数
10.面向对象涉及
11.定义类
12.创建类实例
13.创建__init__()方法
14.访问限制
代码部分
形参与实参,BMI指标
def function1(height,weight,name='路人'):
'''
根据身高体重计算bmi
'''
bmi = weight/(height*height)
print("BMI: ",bmi)
if bmi >= 18.5 and bmi < 24.9:
print("体重正常")
if bmi <18.5:
print("体重过轻")
if bmi >= 24.9:
print("体重过重")
while 1:
name = input('请输入姓名')
height = float(input('请输入身高(m)'))
weight = int(input('请输入体重(kg)'))
function1(height,weight)
def coffee(*coffeename):
print('\n我喜欢的咖啡有:')
for item in coffeename:
print(item) #输出咖啡名
coffee("蓝山")
coffee("蓝山","卡布奇诺","巴西")
一个星号的,得到的是列表
def function1(*person):
'''
根据身高体重计算bmi(升级版)
person:可变参数
'''
for list1 in person:
for item in list1:
person = item[0]
height = item[1]
weight = item[2]
bmi = weight/(height*height)
print(person+"的BMI: ",bmi)
if bmi >= 18.5 and bmi < 24.9:
print("体重正常")
if bmi <18.5:
print("体重过轻")
if bmi >= 24.9:
print("体重过重")
list_a = [['wyn',1.71,59],['cj',1.70,55],['lsm',1.69,50]]
list_b = [['lmy',1.70,55],['ccc',1.65,58]]
function1(list_a,list_b )
两个星号的,得到的是字典
def sign(**sign):
print()
for key,value in sign.items():
print(key,"country:",value)
sign(wyn='China',lmy='France')#或括号里面用,dict1{xx=xxx,xx=xx}
sign(cj='England',lsm='Japan')
返回值,return
def function1(money):
money_old = sum(money)
money_new = money_old
if 500 <= money_old < 1000:
money_new = '{:.2f}'.format(money_old * 0.9)#改两位小数,享受九折优惠
elif 1000 <= money_old < 2000:
money_new = '{:.2f}'.format(money_old * 0.8)
elif 2000 <= money_old < 3000:
money_new = '{:.2f}'.format(money_old * 0.7)
elif 3000 <= money_old:
money_new = '{:.2f}'.format(money_old * 0.6)
return money_new ,money_old
print('开始结算:\n')
list_money = []
while True:
inmoney = float(input("请输入商品金额(0表示输入完毕)😊)
if int(inmoney) == 0:
break
else:
list_money.append(inmoney)
money = function1(list_money)
print('Total: ',money[0],'应付:',money[1])
全局变量
pinetree = "我是一棵松树"
def fun1():
global pinetree #在函数内修改外部变量
pinetree = "挂上彩灯、礼物,我变成圣诞树-" #修改全局变量的值
print(pinetree)
print("\n下雪了开始做梦")
fun1()
print(pinetree)
pinetree = "123"
print(pinetree)
匿名函数
import math
def cir(r):
return math.pirr #面积
r = 10
print("半径:",r,"面积",cir(r))
r= 10
result = lambda r:math.pirr
print(result(r))
创建init方法,类的实例
class Geese:
'''大雁类'''
#属性
#方法
def init(self,beak,wing,claw): #定义构造方法
print('我是大雁类,我有一下特征:')
print(beak) #喙
print(wing)
print(claw)
beak1 = "喙的基部较高,和头的长度几乎相等"
wing1 = "翅膀长而尖"
claw1 = "爪子璞状"
wildGoose = Geese(beak1,wing1,claw1) #创建大雁类的一个实例
wildGoose1 = Geese()
wildGoose2 = Geese()
wildGoose3 = Geese()
print(wildGoose)
创建实例方法并访问
class Geese:
'''大雁类'''
neck = '脖子较长' #类属性
wing = '振翅频率高'
leg = '腿位于身体重心支点'
number = 0
def __init__(self): #定义构造方法
Geese.number += 1 #将编号+1
print('\n我是第%d只大雁,我是大雁类,我有以下特征:'%Geese.number)
print(Geese.neck) #喙
print(Geese.wing)
print(Geese.leg)
list1 = []
for i in range(4):
list1.append(Geese())#创建大雁类的实例
print("一共有%d只大雁"%Geese.number)
Geese.beak = "喙的基部较高"#添加类属性
print(list1[1].beak )
创建实例方法并访问2,实例属性
class Geese:
'''大雁类'''
def init(self):
self.neck = '脖子较长' #实例属性,只作用于当前实例当中;而类属性是全体通用的
wing = '振翅频率高'
leg = '腿位于身体重心支点'
print("我有以下特征:")
print(self.neck) #访问实例属性
print(self.wing)
print(self.leg)
geese = Geese() #实例化类的对象
访问限制
class Swan:
_neck_swan = "天鹅脖子长" #保护类型的属性
def init(self):
print("init():",Swan._neck_swan) #访问保护类型的属性
swan = Swan() #创建Swan类的实例
print("直接访问: ",swan._neck_swan) #通过实例名访问保护类型的属性
所以受保护类型的属性可以通过实例名访问
print('\n')
class Swan:
__neck_swan = "天鹅脖子长" #私有类型的属性
def init(self):
print("init():",Swan.__neck_swan) #访问私有类型的属性
swan = Swan() #创建Swan类的实例
print("直接访问: ",swan._Swan__neck_swan) #通过实例名访问保护类型的属性