python之函数

 1 #函数
 2 # print('helloword')
 3 # print('helloword')
 4 # print('helloword')
 5 
 6 
 7 # #定义一个函数,函数必须先定义再执行,光定义不会执行代码
 8 # def func():
 9 #     print('helloword')
10 #     print('helloword')
11 #     print('helloword')
12 #     print('helloword')
13 # #函数调用
14 # func()
15 
16 #函数的参数  ,形参和实参
17 #定义一个函数,函数必须先定义再执行,光定义不会执行代码
18 
19 # def get_sum(a,b):#a,b就行形参,函数名最好见名知意,如果有变量名的形参,那就是必填形参(函数调用个数要一致)
20 #     print(a+b)
21 #
22 # #函数调用
23 # get_sum(1,2)#1,2就是实参 ,如果函数调用的时候,出现了变量名=值得写法,后面的参数要保持一致
24 
25 #函数的返回值:函数运行完之后有一个对象或值返回
26 # def get_sum(a,b):
27     #取消函数在控制台打印
28     #print(a+b)
29     #return a+b
30 #函数调用
31 # get_sum(1,2)
32 #函数的返回值可以参与运算,return是可以结束函数的运行的,return后面可以写多个对象,
33 # 但是返回的只有一个对象-都是元组(出现两个及两个以上对象),如果return后面只有一个对象,返回的就是这个对象的类型
34 #return后面同一个缩进下不能有语句
35 # print(get_sum(1,2)+5)
36 
37 def func(a,b):
38     if a>b:
39         return 1
40     elif a<b:
41         return -1
42     else:
43         return 0
44 if func(11,11):#if 后面非0即真 ,后面只要是非0数值,非空列表,非空字符串,就是为真
45     print("不相等")
46 
47 
48 # str() 将其他类型转换为字符串类型
49 #int() 将字符串转换成 int 类型 ,对float进行取整数部分,字符串转换为int类型,不能带有小数点
50 #float 对其他类型转换为float类型
51 # list() 转换成列表
52 # tuple 转换成元组
53 #input ,返回值是字符串
原文地址:https://www.cnblogs.com/zhaobobo001/p/14418089.html