函数变量和返回值

2018-01-04  本文已影响0人  C1awn_

1. 函数变量

#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
def fun():
    x=100
    print x
fun()
C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/180104/cs.py
100

Process finished with exit code 0

这里的x是局部变量

#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
x = 'global var'
def fun():
    x=100
    print x
fun()
print x

C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/180104/cs.py
100
global var

Process finished with exit code 0

文件顶部的x是全局变量
举例3:

#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
x = 100
def fun():
    global x    #先声明x是全局变量,下面才能调用全局变量。声明只能这么写,不能多余定义。
    x += 10
    print x
fun()
print x    #这里的x已经是改变过的全局变量
C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/180104/cs.py
110
110

Process finished with exit code 0

举例4:global强制定义全局变量

#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
x = 100
def fun():
    global x
    x += 10
    global y
    y = 1
fun()
print x
print y
C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/180104/cs.py
110
1

Process finished with exit code 0

现在y已经全局变量

locals() 函数会以字典类型返回当前位置的全部局部变量。
对于函数, 方法, lambda 函式, 类, 以及实现了 call 方法的类实例, 它都返回 True。
举例:

>>>def runoob(arg):    # 两个局部变量:arg、z
...     z = 1
...     print (locals())
... 
>>> runoob(4)
{'z': 1, 'arg': 4}      # 返回一个名字/值对的字典
>>>

2. 函数返回值

#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
def fun():
    print 'hello wd'
    return 200
print fun()
C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/180104/cs.py
hello wd
200

Process finished with exit code 0

程序除了输出hello wd,还输出了返回值
联想一下前一天写的打印pid程序

#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
import sys
import os    #要调用os.listdir
def isNum(n):
    for i in n:
        if i in "0123456789":
            pass
        else:
            break
    else:
         print n,
for i in os.listdir('/proc'):    #调用函数
    isNum(i)

此程序可以简化为:

#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
import sys
import os    #要调用os.listdir
def isNum(n):
    for i in n:
        if i not in "0123456789":
            return False    #返回假
    return True  #函数返回真
for i in os.listdir('/proc'): #调用函数
    if isNum(i):  #只有函数返回真的才会到这一步,此时==if True
        print i,

判断数字用isdigit()

3. 练习

  1. 设计一个程序,从终端接收10个数字,并使用自己编写的排序函数,对10个数字排序后输出
  2. 设计一个函数,接收一个英文单词,从文件中查询该单词的汉语意思并返回

1.解答

#!/usr/bin/python
# -*- coding:utf8 -*-
# author: chawn
# date:
s = raw_input('plz input 10 num(no blank): ')
print list(s)
print sorted(list(s))

执行结果

C:\Users\chawn\PycharmProjects\pyex\venv\Scripts\python.exe C:/Users/chawn/PycharmProjects/pyex/180104/给输入的10个数排序输出.py
plz input 10 num(no blank): 14236899556
['1', '4', '2', '3', '6', '8', '9', '9', '5', '5', '6']
['1', '2', '3', '4', '5', '5', '6', '6', '8', '9', '9']

Process finished with exit code 0
  1. 解答
def englishiIntoChinese():

   word = raw_input('please input english word:')

   with open('dictfile.txt', 'r') as f:

       for i in f:

           if word==i.strip().split()[0]:

               return i.strip().split()[1]

       return '不存在!'



print englishiIntoChinese()
上一篇 下一篇

猜你喜欢

热点阅读