python 例
说明局部变量的程序![](zzz/editor/img/code.gif)
#!/usr/bin/python def func(x): print 'x is',x x = 2 print 'Changed local x to',x x = 50 func(x) print 'x is still',x
$ ./func_local.py
x is 50
Changed local x to 2
x is still 50
[ 本帖最后由 madfrogme 于 2012-8-30 21:10 编辑 ]
#!/usr/bin/python def func(): global x print 'x is',x x = 2 print 'Changed local x to',x x = 50 func() print 'Value of x is',x
#!/usr/bin/python def say( message, times = 1): print message * times say('Hello') say('World',3)
#!/usr/bin/python def func(a, b=5, c=10): print 'a is', a, 'and b is', b, 'and c is', c func(3, 7) func(25, c=24) func(c=50, a=100)
#!/usr/bin/python def printMax(x, y): ''' Prints the maximum of two number. The two values must be integers.''' x = int(x) y = int(y) if x > y: print x, 'is maximum' else: print y, 'is maximum' printMax(3,5) print printMax.__doc__
#!/usr/bin/python def printMax(x, y): ''' Prints the maximum of two number. The two values must be integers.''' x = int(x) y = int(y) if x > y: print x, 'is maximum' else: print y, 'is maximum' printMax(3,5) print printMax.__doc__
#!/usr/bin/python import sys print 'The command line arguments are:' for i in sys.argv: print i print '\n\nThe PYTHONPATH is', sys.path, '\n'
#!/usr/bin/python # Filename: using_name.py if __name__ == '__main__': print 'This program is being run by itself' else: print 'I am being imported from another module'