默认参数
def print_info(name,age=20):
print("Name",name)
print("Age",age)
print_info(name="ki")
print_info(name='ken',age=21)
Name ki
Age 20
Name ken
Age 21
不定长参数
def test(a,b,*arg):
print(a)
print(b)
print(arg)
test(11,12)
11
12
()
def test(a,b,*arg):
print(a)
print(b)
print(arg)
test(11,12,33,44,55,66)
11
12
(33, 44, 55, 66)
末尾使用kwargs参数
def test(a,b,*arg,**kwargs):
print(a)
print(b)
print(arg)
print(kwargs)
test(11,12,33,44,55,66)
11
12
(33, 44, 55, 66)
{}
def test(a,b,*arg,**kwargs):
print(a)
print(b)
print(arg)
print(kwargs)
test(11,12,33,44,55,66,77,x=11,y=12,z=12)
11
12
(33, 44, 55, 66, 77)
{'x': 11, 'y': 12, 'z': 12}
函数的四种类型
1.无参数,无返回值的函数
def print_menu():
print('------------------')
print(' 涮涮锅 点菜系统 ')
print(' 1. 羊肉涮涮锅 ')
print(' 2. 牛肉涮涮锅 ')
print(' 3. 猪肉涮涮锅 ')
print('------------------')
print_menu()
------------------
涮涮锅 点菜系统
1. 羊肉涮涮锅
2. 牛肉涮涮锅
3. 猪肉涮涮锅
------------------
无参数,有返回值的函数
def get_temperature():
return 26
temperature = get_temperature()
print("当前的温度为:",temperature)
print("现在的温度为:",get_temperature())
当前的温度为: 26
现在的温度为: 26
有参数,无返回值
def number_result(number_one,number_tow):
result = number_one + number_tow
print('计算结果为:%d'%result)
有参数,有返回值
def calculate(number):
result = 0
i = 1
while i <= number:
result = result + i
i += 1
return result
result =calculate(100)
print('1~100的累积和为:',result)
1~100的累积和为: 5050
函数案例——名片管理器
输出名片管理器的菜单
def display_menu():
print("-"*3)
print(" 名片管理器 v8.8")
print("1. 添加名片")
print("2. 删除名片")
print("3. 修改名片")
print("4. 查询名片")
print("5. 获取所有名片信息")
print("6. 退出系统")
print("-"*3)
i = 0
while i < 1:
display_menu()
获取用户输入信息
def get_choice():
selected_key = input("请选择序号: ")
return int(selected_key)
key=get_choice()
通过获取序号执行不同的功能
if key == 1:
pass
elif key == 2:
pass
elif key == 3:
pass
elif key == 4:
pass
elif key == 5:
pass
elif key == 6:
pass
else:
print("输入有误,请重新输入...")
函数的嵌套调用
def func_one():
print('------ func_one start ------')
print('这里是func_one函数执行的代码')
print('------ func_one end --------')
def func_tow():
print('---- func_tow start ----')
func_one()
print('----- func_tow end -----')
func_tow()
---- func_tow start ----
------ func_one start ------
这里是func_one函数执行的代码
------ func_one end --------
----- func_tow end -----
递归函数和匿名函数
递归函数
def func(count):
if count == 1:
result = 1
else:
result = func(count-1) * count
return result
number =int(input("请输入一个正整数:"))
print("%d!="%number,func(number) )
请输入一个正整数:3
3!= 6
匿名函数
add = lambda a,b:a+b
print("运行结果: ",add(10,20))
print("运行结果: ",add(20,20))
运行结果: 30
运行结果: 40
def func(a,b,operation):
print("a=%d"%a)
print("b=%d"%b)
print("result=", operation(a,b))
func(11,22, lambda x,y:x+y)
print("-------------------")
func(11,22, lambda x,y:x-y)
a=11
b=22
result= 33
-------------------
a=11
b=22
result= -11
students = [
{"name":"zhangsan","age":18},
{"name":"lisi","age":21},
{"name":"wangwu","age":19}
]
students.sort(key=lambda x:x['name'])
print("按name排序后的结果为: ",students)
students.sort(key=lambda x:x['age'])
print("按age排序后的结果: ",students)
按name排序后的结果为: [{'name': 'lisi', 'age': 21}, {'name': 'wangwu', 'age': 19}, {'name': 'zhangsan', 'age': 18}]
按age排序后的结果: [{'name': 'zhangsan', 'age': 18}, {'name': 'wangwu', 'age': 19}, {'name': 'lisi', 'age': 21}]
日期时间函数
时间戳
import time
ticks = time.time()
print("当前时间戳为:", ticks)
当前时间戳为: 1653967259.4192438
时间元组
import time
struct_time_obj = time.localtime()
print(struct_time_obj)
time.struct_time(tm_year=2022, tm_mon=5, tm_mday=31, tm_hour=11, tm_min=26, tm_sec=16, tm_wday=1, tm_yday=151, tm_isdst=0)
日历函数
import calendar
calendar_march=calendar.month(2022,6)
print("以下输出2022年6月份的日历")
print(calendar_march)
以下输出2022年6月份的日历
June 2022
Mo Tu We Th Fr Sa Su
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
随机函数
import random
print(random.random())
print(random.random())
0.5259084512727711
0.6034228296268306
import random
print(random.randint(10,20))
print(random.randint(1,100))
13
28
random.shuffle函数(洗牌-打乱元素顺序)
import random
demo_list = ["python","java","c","c+"]
random.shuffle(demo_list)
print(demo_list)
['java', 'python', 'c', 'c+']
random.sample(随机取元素)
import random
list_num = [1,2,3,4,5,6,7,8,9,10]
slice = random.sample(list_num,6)
print(slice)
print(list_num)
[9, 8, 6, 7, 2, 1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
8```
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)