Variables and function return values in Python functions

Source: Internet
Author: User

1. Variables for functions

Local variables and global variables:

Any variable in Python has a specific scope

Variables defined in a function are generally only used inside the function, and these variables that can only be used in specific parts of the program are called local variables.

Variables defined at the top of a file can be called by any function in the file, which can be called global variables for the variables used throughout the program.

def Fun ():

x=100

Print x

Fun ()

x =


def Fun ():

Global x //Statement

x +=1

Print x

Fun ()

Print x


External variables are changed:

x =

def Fun ():

Global X//Statement

x +=1

Print x

Fun ()

Print x


Internal variables are also available outside:

x =

def Fun ():

Global x

x +=1

Global y

y = 1

Print x

Fun ()

Print x

Print y

x =

def Fun ():

x = 1

y = 1

Print locals ()

Fun ()

Print locals ()

{' Y ': 1, ' X ': 1}

A variable in a statistical program that returns a dictionary

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


2. return value of function

function return value:

A specified value is returned when the function is called

Default returns none after function call

return value

The return value can be close any type

function terminates after return execution

Return and print differences

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:p rintpid.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

Or:

#/usr/bin/env python

#-*-Coding:utf-8-*-

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

# @Author: fengxiaoqing

# @file:p rintpid.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

Exercises

1. Design a program that receives 10 numbers from the terminal and uses its own sorting function to sort the output of 10 numbers.

2. Design a function, receive an English word, query the Chinese meaning of the word from the file and return it.


Variables and function return values in Python functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.