Python函数中的变量和函数返回值

简介:

1.函数的变量

局部变量和全局变量:

Python中的任何变量都有特定的作用域

在函数中定义的变量一般只能在该函数内部使用,这些只能在程序的特定部分使用的变量我们称之为局部变量

在一个文件顶部定义的变量可以供文件中的任何函数调用,这些可以为整个程序所使用的变量称为全局变量。

    def fun():

        x=100

        print x

    fun()

    x = 100

    

    def fun():

        global x   //声明

        x +=1

        print x

    fun()

    print x


外部变量被改:

    x = 100

    def fun():

        global x   //声明

        x +=1

        print x

    fun()

    print x


内部变量外部也可用:

    x = 100

    def fun():

        global x

        x +=1

       global y

        y = 1

        print x

    fun()

    print x

    print y

    x = 100

    def fun():

        x = 1

        y = 1

        print locals()

    fun()

    print locals()

    {'y': 1, 'x': 1}

    统计程序中的变量,返回的是个字典

    {'__builtins__': <module '__builtin__' (built-in)>, '__file__': 'D:/PycharmProjects/untitled/python/2018.01.03/bianliang.py', '__package__': None, 'x': 100, 'fun': <function fun at 0x02716830>, '__name__': '__main__', '__doc__': None}

    

2. 函数的返回值

函数返回值:

函数被调用后会返回一个指定的值

函数调用后默认返回None

return返回值

返回值可骒任意类型

return执行后,函数终止

return与print区别

    def fun():

        print 'hello world'

          return 'ok'

        print 123

    print fun()

    hello world

    123

    None


    #/usr/bin/env python

    # -*- coding:utf-8 -*-

    # @time   :2018/1/2 21:06

    # @Author :FengXiaoqing

    # @file   :printPID.py

    import sys

    import os

    def isNum(s):

        for i in s:

            if i not  in '0123456789':

        return False

        return True

    for i in os.listdir("/proc"):

        if isNum(i):

     print i

    

    import sys

    import os

    def isNum(s):

        if s.isdigit():

            return True

        return False

    for i in os.listdir("/proc"):

        if isNum(i):

           print i

或:

    #/usr/bin/env python

    # -*- coding:utf-8 -*-

    # @time   :2018/1/2 21:06

    # @Author :FengXiaoqing

    # @file   :printPID.py

    import sys

    import os

    def isNum(s):

        if s.isdigit():

            return True

        else:

            return False

    for i in os.listdir("/proc"):

        if isNum(i):

           print i

习题

1. 设计一个程序,从终端接收10个数字,并使用自己编写的排序函数,对10个数字排序后输出.

2. 设计一个函数,接收一个英文单词,从文件中查询该单词的汉语意思并返回.

    


本文转自 枫叶云  51CTO博客,原文链接:http://blog.51cto.com/fengyunshan911/2057207


相关文章
|
2天前
|
Serverless 数据处理 数据库
Python函数的多返回值详解
Python函数的多返回值详解
6 1
|
2天前
|
程序员 Python
Python中的变量作用域:深入解析与示例
Python中的变量作用域:深入解析与示例
4 1
|
2天前
|
机器学习/深度学习 Python
在Python中监听变量值的变化
在Python中监听变量值的变化
12 2
|
2天前
|
数据处理 Python
Python函数:深入理解与应用
Python函数:深入理解与应用
6 1
|
2天前
|
算法 Python
Python函数的嵌套调用:深入理解与应用
Python函数的嵌套调用:深入理解与应用
9 1
|
2天前
|
IDE 开发工具 开发者
Python函数说明文档:编写清晰易懂的文档字符串
Python函数说明文档:编写清晰易懂的文档字符串
7 1
|
2天前
|
Python
Python函数的返回值深入解析
Python函数的返回值深入解析
7 1
|
2天前
|
存储 Python
Python函数参数详解
Python函数参数详解
8 1
|
3天前
|
Python
Python中的函数
Python中的函数
|
18天前
|
算法 Python 容器
Python编程 - 不调用相关choose库函数,“众数“挑选器、随机挑选器 的源码编程实现
Python编程 - 不调用相关choose库函数,“众数“挑选器、随机挑选器 的源码编程实现
43 0