1. Variable of function
Local and global variables:
Any variable in Python has a specific scope
Variables defined in a function can only be used inside the function, and those 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. These variables that can be used by the entire program are called global variables.
def fun(): x=100 print x fun() x = 100 def fun(): global x //statement x +=1 print x fun() print x
External variable changed:
x = 100 def fun(): global x //statement x +=1 print x fun() print x
Internal variables are also available externally:
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} //Count the variables in the program and return a dictionary {'__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. Return value of function
Function return value:
The function returns a specified value when called
Function call returns None by default
Return return value
The return value can be of any type
When return is executed, the function is terminated
Difference between return and print
def fun(): print 'hello world' return 'ok' print 123 print fun() hello world 123 None #/usr/bin/env python # -*- coding:utf-8 -*- # 2018/11/27 21:06 # FengXiaoqing #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
Or:
#/usr/bin/env python # -*- coding:utf-8 -*- # 2018/11/27 21:06 # FengXiaoqing # :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