1 #author F
2 import time
3 #面向对象 : 类 --->class
4 #面向过程 : 过程-->def
5 #函数式编程 : 函数-->def
6
7 #定义函数
8 def func1():
9 """testing"""
10 print("in the func1")
11 return 0
12 #定义过程
13 def func2():
14 """testing"""
15 print("in the func2")
16
17 x=func1() #in the func1
18 y=func2() #in the func2
19
20 print("result from func1 %s" % x) #result from func1 0
21 print("result from func2 %s" % y) #result from func2 None #无return 隐式返回一个none
22
23
24 def function():
25 time_format = "%Y-%m-%d %X"
26 time_current = time.strftime(time_format)
27 print("hahahaha %s" %time_current)
28
29 function()
30 function()
31 function()
32
33 #返回值
34 def function1():
35 print("function1")
36
37 def function2():
38 print("function2")
39 return 0
40
41 def function3():
42 print("function3")
43 return 1, "string", ["list1", "list2"], {"name":"Aha"} #以元祖的形式返回
44
45 x = function1()
46 y = function2()
47 z = function3()
48 print(x) #返回数= 0 (未返回) ->返回none
49 print(y) #返回数= 1 (返回1个值) ->返回object
50 print(z) #返回数= 多个 ->返回元组
51
52 #有参函数 形参
53
54 def add(x,y): #形参 位置参数
55 print(x)
56 print(y)
57
58 # add(1, 2) #位置参数调用 与形参一一对应
59 # add(y=2, x=1) #关键字调用 与形参顺序无关
60 ##关键参数是不能写在位置参数前面的 关键字参数一定要在位置参数后面 位置参数在关键字参数前面
61 # add(2, x=3) #既不是位置调用 也不是关键字调用 无法运行 2给x,3又通过关键字调用给了x
62 add(3, y=2)
63
64
65 ##默认参数特点:调用函数时 默认参数非必须传递(相当于默认安装值)
66 def test(x, y=2):
67 print(x)
68 print(y)
69
70 ##参数组
71 def test_arr(*args): #接受多个实参 (N个__位置参数__ 转换成元组的形式 不接受关键字参数)
72 print(args)
73 test_arr(1, 2, 3, 4, 5, 6) #参数组传参方式
74 test_arr(*[1, 3, 3, 5, 5]) #参数组args = turple([1,3,3,5,5])
75
76 def test_combine(x, *args):
77 print(x)
78 print(args)
79 test_combine(1,2,3,45,5)
80
81 def test_diction(**kwargs): #接受字典 (N个__关键字__参数转为字典)
82 print(kwargs)
83 print(kwargs['name'])
84
85 test_diction(name='HHH', age="2", sex="F") #{'name': 'HHH', 'age': '2', 'sex': 'F'}
86 test_diction(**{'name':'HHH', 'age':"2", 'sex':"F"}) #{'name': 'HHH', 'age': '2', 'sex': 'F'}
87
88 def test_diction_combine(name, **kwargs):
89 print(name)
90 print(kwargs)
91 # test_diction_combine("axiba", "ss") #报错 ss是字符串 要用关键字方式传递
92 test_diction_combine("axiba", sex="male", hobby="drink")
93
94 def test_diction_args(name, age=18, **kwargs):
95 print(name)
96 print(age)
97 print(kwargs)
98 test_diction_args("aaa", sex="m", hobby="sing")
99 test_diction_args("aaa", age="111", sex="m", hobby="sing")