1. Variable Functions
Local and global variables:
Any variable in Python has a specific scope
Variables defined in the function generally only used in the interior of the function, these variables can be used in a specific part of the program we call the local variables
At the top of a file variable can be defined for any function file calls these variables can be used for the entire program called global variables.
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. The return value of the function
Function return value:
Returns the value of a specified function is called after
After the function call returns the default None
return return value
The return value can be any type of Ke
After the return to perform the function is terminated
return and print the difference
def fun():
print 'hello world'
return 'ok'
print 123
print fun()
hello world
123
None
#/usr/bin/env python
# -*- coding:utf-8 -*-
# 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
#/usr/bin/python
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 -*-
# 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