Python入门学习笔记6:函数
1 #函数 2 #参数 3 #1.必须参数 2.关键字参数 3.默认参数 4.形式参数(形参) 4 # round(操作变量,保留小数点几位,且四舍五入) 5 a = 1.12386 6 print(round(a,3)) # 1.124 7 8 9 def functionname(str): 10 print(str) 11 return str 12 13 functionname('123') 14 15 def add(x,y): 16 result = x + y 17 return result 18 19 print(add(1,2)) 20 print(add(y=2,x=1))#关键字参数 21 22 def damage(skill1,skill2): 23 damage1 = skill1 * 3 24 damage2 = skill2 * 2 +10 25 return damage1,damage2 26 27 damages = damage(3,6) 28 print(damages,type(damages)) 29 print(damages[0],damages[1]) #9 22 30 31 #序列解包 32 skill1_damage,skill2_damage = damage(3,6) 33 print(skill1_damage,skill2_damage) #9 22 34 35 a = 1 36 b = 2 37 c = 3 38 39 a,b,c = 3,2,1 40 print(a,b,c)#3 2 1 41 42 d = 1,2,3 43 print(type(d))#<class 'tuple'> 44 45 a=b=c=1 46 print(a,b,c) 47 48 49 def print_student_files(name,age=18,gender='男',college='人民路小学'): 50 print('我叫'+name+ 51 '\n我今年'+str(age)+'岁', 52 '\n我是'+gender+'生', 53 '\n我在'+college+'上学') 54 print_student_files('晓明',18,'男','人民路小学') 55 print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') 56 print_student_files('晓红')