在许多编程语言中,函数(function)是有返回值的,过程(procedure)是简单、特殊并且没有返回值的。而在Python中,严格来说只有函数没有过程。 例如:
>>> def hello(): print('hello fishc!') >>> temp = hello() hello fishc! >>> temp 没有任何显示,为什么?因为我们这个hello函数没有return任何东西。 >>> print(temp) None >>> type(temp) <class 'NoneType'> 就算函数没有写上return语句,python也会返回一个None对象在Python中,函数是可以返回多个值的,如下:
>>> def back(): return [1,'xiaojiayu',3.14] >>> back() [1, 'xiaojiayu', 3.14] >>> def back(): return 1,'xiaojiayu',3.14 >>> back() (1, 'xiaojiayu', 3.14)在函数里面定义的参数以及变量都称之为局部变量; 在函数外边定义的变量称之为全局变量。
函数内若试图修改全局变量,Python会新建一个同名局部变量用于存储修改值,原全局变量的值不变。
def discounts(price,rate): final_price = price * rate # print('这里试图打印全局变量old_price的值是:',old_price) old_price = 50 #这里试图修改全局变量 print('修改后old_price的值1是:',old_price) return final_price old_price = float(input('请输入原价:')) rate = float(input('请输入折扣率:')) new_price = discounts(old_price,rate) print('修改后old_price的值2是:',old_price) print('打折后价格是:',new_price) #print('这里试图打印局部变量final_price的值:',final_price) #请输入原价:100 #请输入折扣率:0.8 #打折后价格是: 80.0 #Traceback (most recent call last): # File "D:\python test\局部变量和全局变量.py", line 12, in <module> # print('这里试图打印局部变量final_price的值:',final_price) #NameError: name 'final_price' is not defined #在函数里面定义的参数以及变量都称之为局部变量, #在函数外边定义的变量称之为全局变量 ==================== RESTART: D:\python test\局部变量和全局变量.py ==================== 请输入原价:100 请输入折扣率:0.8 修改后old_price的值1是: 50 修改后old_price的值2是: 100.0 打折后价格是: 80.0如果没有使用return语句指定返回值,Python也不是什么都不返回id,Ta会返回一个None对象,所以我们说Python所有的函数都有返回值。
>>> def hello(): print('Hello FishC!') >>> temp = hello() Hello FishC! >>> temp >>> print(temp) None 请问Python的return语句可以返回多个不同类型的值吗? 可以丫,默认用逗号隔开,是以元祖的形式返回,你当然也可以用列表包含起来返回: >>> def myFun(): return '操操操操', 520, 3.14, True >>> myFun() ('操操操操', 520, 3.14, True) >>> def myFun2(): return ['小甲鱼', 1314, 5.12, False] >>> myFun2() ['小甲鱼', 1314, 5.12, False] >>> 目测以下程序会打印什么内容 def fun(var): var = 1314 print(var, end='') var = 520 fun(var) print(var) 运行结果: 1314520 目测以下程序会打印什么内容? var = 'Hi' def fun1(): global var var = ' Baby ' return fun2(var) def fun2(var): var += 'I love you' fun3(var) return var def fun3(var): var = '小甲鱼' print(fun1()) 运行结果: ================== RESTART: C:/Users/ThinkPad/Desktop/11.py ================== Baby I love you编写一个函数,判断传入的字符串参数是否为“回文联”
##方法一: def huiwen(string): length = len(string) last = length - 1 length //= 2 flag = 1 for each in range(length): if string[each] != string[last]: flag = 0 last -= 1 if flag == 1: return 1 else: return 0 string = input('请输入一句话:') if huiwen(string) == 1: print('是回文联') else: print('不是回文联') ##方法二: def huiwen(string): list1 = list(string) list2 = reversed(list1) if list1 == list(list2): return '是' else: return '不是' print(huiwen('上海自来水来自海上')) 编写一个函数,分别统计出传入字符串参数的英文字母、空格、数字和其他字符的个数。 #分别统计出传入字符串参数的英文字母、空格、数字和其他字符的个数。 def count(*param): length = len(param) for i in range(length): zm = 0 kg = 0 sz = 0 qt = 0 for each in param[i]: if each.isalpha(): zm += 1 elif each.isdigit(): sz += 1 elif each == ' ': kg += 1 else: qt += 1 print('第 %d 个字符串共有:英文字母 %d 个,数字 %d 个,空格 %d 个,其他字符 %d 个。' % (i+1,zm,sz,kg,qt)) count('I love fishc.com','I love you,you love me','5201314')转载于:https://www.cnblogs.com/wanbin/p/9514691.html
