解释:函数与过程
函数(Function):有返回值
过程(procedure):是简单、特殊并没有返回值的
1.函数
Python是只有函数没有过程的
>>> def hell():
print('Hi好') #hell()函数没有返回值
>>> temp = hell() #hell()赋值给temp,hell()函数没有返回值,所以temp没有返回值
Hi好
>>> temp
>>> print(temp)
None #没有返回值,也会返回none,所以都是有返回值
>>> type(temp)
<class 'NoneType'>
>>>
以上说明Python只有函数(都有返回值)
2.函数的返回值
可以是多个值,不同类型
>>> def test1():
return [1,2,3.14,'hello',[4,5,6]]
>>> test1()
[1, 2, 3.14, 'hello', [4, 5, 6]]
>>>
>>> def test1():
return 1,2,3.14,'hello',[4,5,6]
>>> test1()
(1, 2, 3.14, 'hello', [4, 5, 6])
>>>
3.函数变量的作用域
全局变量 、局部变量
def discounts(price,rate):
final_price = price * rate
return final_price
rate = float(input('请输入折扣率:'))
old_price = float(input('请输入原价是:'))
new_price = discounts(old_price,rate)
print('打折后的价格是:',new_price)
结果:
============ RESTART: D:/cj/python/Python script/19/jububianliang.py ===========
请输入折扣率:0.8
请输入原价是:188
打折后的价格是: 150.4
>>>
局部变量
只能在其函数内部生效,出了函数的范围,是无效的,例如:
def discounts(price,rate):
final_price = price * rate
return final_price
rate = float(input('请输入折扣率:'))
old_price = float(input('请输入原价是:'))
new_price = discounts(old_price,rate)
print('打折后的价格是:',new_price)
print('这里试图打印局部变量:',final_price)
结果:
============ RESTART: D:/cj/python/Python script/19/jububianliang.py ===========
请输入折扣率:0.8
请输入原价是:100
打折后的价格是: 80.0
Traceback (most recent call last):
File "D:/cj/python/Python script/19/jububianliang.py", line 9, in <module>
print('这里试图打印局部变量:',final_price)
NameError: name 'final_price' is not defined
>>>
总结:在函数里面定义的参数,如上面的final_price都称之为局部变量,出了这个函数,这些变量是无效的。
全局变量
函数外面定义的变量叫做全局变量,作用域是整个代码段。
例如:在函数内部读取全局变量
def discounts(price,rate):
final_price = price * rate
print('这里试图打印全部变量old_price,',old_price)
return final_price
rate = float(input('请输入折扣率:'))
old_price = float(input('请输入原价是:'))
new_price = discounts(old_price,rate)
print('打折后的价格是:',new_price)
结果:
============ RESTART: D:/cj/python/Python script/19/jububianliang.py ===========
请输入折扣率:0.8
请输入原价是:100
这里试图打印全部变量old_price, 100.0
打折后的价格是: 80.0
>>>
试图在函数中修改全局变量,Python会在函数内创建一个跟全局变量名字一样的局部变量,名字是一模一样。
def discounts(price,rate):
final_price = price * rate
old_price = 50
print('修改后的old_price的价格1是::',old_price)
return final_price
rate = float(input('请输入折扣率:'))
old_price = float(input('请输入原价是:'))
new_price = discounts(old_price,rate)
print('修改后的old_price的价格2是::',old_price)
print('打折后的价格是:',new_price)
结果:
============ RESTART: D:/cj/python/Python script/19/jububianliang.py ===========
请输入折扣率:0.8
请输入原价是:100
修改后的old_price的价格1是:: 50
修改后的old_price的价格2是:: 100.0
打折后的价格是: 80.0
>>>
总结:不要在函数中修改全局变量,可以在函数中调用全部变量。(实在要改,后面学习方法)
课后习题:
0.下边程序会输入什么?
>>> def next():
print('我在next()函数里')
pre()
>>> def pre():
print('我在pre()函数里面')
>>> next()
我在next()函数里
我在pre()函数里面
>>>
1.请问一下这个函数有返回值吗?
>>> def Fun():
print('hello world')
>>> print(Fun())
hello world
None
>>>
>>> temp = Fun()
hello world
>>> print(temp)
None
>>>
有返回值,如果函数没有return语句也是有返回值的,返回的是一个None对象,所以我们说python所有的函数都有返回值
2.请问Python的return语句可以返回多个不同类型的值吗?
可以,默认用逗号隔开,是以元组的形式返回,也可以用列表包含起来返回。
>>> def test1():
return 1,2,3.14,'hello',[4,5,6]
>>> test1()
(1, 2, 3.14, 'hello', [4, 5, 6])
>>>
3.目测一下程序会打印什么内容?
def fun(var):
var = 1314
print(var,end = '')
var = 520
fun(var)
print(var)
结果:1314520 (var的赋值操作只在函数内有效。)
4.目测一下程序会打印什么内容?
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())
结果:Baby I love you
0.编写一个函数,判断传入的字符串参数是否为‘回文联’(回文联即用回文形式携程的对联,既可顺读,也可倒读。例如:上海自来水来自海上)
def palindrome(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 palindrome(string) == 1:
print('是回文联')
else:
print("不是回文联")
1. 编写一个函数,分别统计出传入字符串参数(可能不只一个参数)的英文字母、空格、数字和其它字符的个数。
def type_count(*string):
length=len(string) ##收集参数的个数就是len(string)
for i in range(length):
letters=0
digit=0
space=0
others=0
for each in string[i]:
if each.isalpha():
letters+=1
elif each.isdigit():
digit+=1
elif each == '':
space+=1
else:
others+=1
print('第%d个字符串有%d个字母,%d个数字,%d个空格,%d个其他字符' %(i+1 , letters, digit , space, others))
type_count('I love fishc.com.','i love you, you love me.')