-
Notifications
You must be signed in to change notification settings - Fork 0
/
函数.py
153 lines (124 loc) · 2.98 KB
/
函数.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Bonnie Li"
# Email: [email protected]
# Date: 4/9/18
#默认参数
# def stu_register(name,age,course = 'python',country = 'CN'):
# print('registration info....')
# print(name,age,country,course)
#
# stu_register('alex',22)
# stu_register('bonnie',23,'java')
#关键参数
# def stu_register(name,course,age,country = 'cn'):
# print('registration info....')
# print(name,age,country,course)
#
#
# stu_register('bonnie','C',age=28)
#包裹参数
# def stu_register(name,*args):
# print('registration info....')
# print(name,*args)
#
#
# stu_register('bonnie',*['python','java','c'])
#
# stu_register('bonnie','python','java','c')
#
# #如果参数中出现*args,传递的参数个数不是固定的,传来过的数是远祖
#**args
# def stu_register(name,*args,**kwargs):
# print('registration info....')
# print(name,args,kwargs)
#
# d={'degree':'primary school'}
# stu_register('bonnie',*['python','java','c'],**d)
#函数返回值, 遇到return 代表函数的终止,
#返回多个值会自动转成元组
# def stu_register(name,course,age):
# print('registration info....')
# print(name,course,age)
# # if age >22:
# # return 'your age is too older'
# # else:
# # return True
# return name,age
#
#
# status=stu_register('bonnie','C',age=28)
# print(status)
#局部变量 就是定义再函数里的变量
#全局变量是定义再函数外边,或者可以说是以一级代码
#再函数内部,可以引用全局变量
#全局变量和局部变量同时在,局部变量优先级高
# name = 'bonnie'
#
# def change_name():
# global name #不建议你在函数里修改全局变量
# name = '花花'
# print('you name is %s ' %name)
#
# change_name()
# print(name)
# 改列表,可以通过改值实现,但是不能重新赋值
# names = ['alex','black girl','peiqi']
#
# def change_name():
# del names[2]
# # names = ['alex','black girl']
# print(names)
#
# change_name()
# print(names)
#嵌套函数
#由内向外一层一层找变量
# def func1():
# print('alex')
#
# def func2(): #不调用的话,只是把函数放入内存,并没有调用的话就不会执行
# print('eric')
# func2()
# func1()
# age = 18
# def func1():
# # age =73
# #print(age)
#
# def func2(): #不调用的话,只是把函数放入内存,并没有调用的话就不会执行
# # age = 84
# print(age)
# age =73
# func2()
#
# func1()
#递归总结
#匿名函数
# data = list(range(10))
#
#
# print(list(filter(lambda n: n if n%2 ==1 else False, data)))
#
# level = 'L0'
# n = 22
#
#
# def func():
# level = 'L1'
# n = 33
# print(locals())
#
# def outer():
# n = 44
# level = 'L2'
# print(locals(),n)
#
# def inner():
# level = 'L3'
# print(locals(),n) #此外打印的n是多少?
# inner()
# outer()
#
#
# func()