-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdef.py
More file actions
74 lines (45 loc) · 1.12 KB
/
def.py
File metadata and controls
74 lines (45 loc) · 1.12 KB
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
# def 函数定义
'''
def 函数名(函数参数):
函数体
'''
import sys
print(dir(sys))
def area(w, h):
print('width=', w, 'height=', h, 'aera=', w*h)
def welcome(name):
print('welcome', name)
welcome('python')
area(40, 50)
'''
对,学习编程就是这么简单呀
'''
def printInfo(name, age=18):
print('name=', name, 'age=', age)
printInfo(age=28, name="文孝礼")
printInfo(name="wxl")
def myfn(a, *rest): # 用*varTuple来表示剩下的参数
print(a)
print(rest)
for i in rest:
print(i)
myfn('a', 'b', 'c', 'd')
def sum(*args): # 定义一个求和的参数
sum = 0
for i in args:
sum += i
return sum
print(sum(1, 2, 3, 4, 5))
def myfn2(a, **rest): # 两个**代表传入的是一个字典数据类型
print(a)
for i, j in rest.items(): # 这个方法是遍历dictionary的字典数据结构的
print(i, j)
myfn2('a', name="文孝礼", age="29")
# 函数作用域
num = 10
def total(num):
# global num #想要在函数内部改变全局的num就需要使用global关键字
num = num + 30
print(num)
print(num)
total(num)