
常量和变量

局部变量与全局变量

global 声明全局变量

#!/usr/bin/python
def func1():
print "func1: local x is", x
def func2():
x = 2
print 'func2: local x is', x
def func3():
global x
print "func3: before change, x is", x
x = 2
print 'func3: changed x to', x
x = 1
print 'Global x is', x
func1()
print 'Global x is', x
func2()
print 'Global x is', x
func3()
print 'Global x is', x
def func1():
print "func1: local x is", x
def func2():
x = 2
print 'func2: local x is', x
def func3():
global x
print "func3: before change, x is", x
x = 2
print 'func3: changed x to', x
x = 1
print 'Global x is', x
func1()
print 'Global x is', x
func2()
print 'Global x is', x
func3()
print 'Global x is', x

复杂类型

控制语句

if 语句

if ... elif ... else , 示例:(注意冒号和缩进)

#!/usr/bin/python
# Filename : if.py
number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.' # new block starts here
print "(but you don't win any prizes!)" # new block ends here
elif guess < number:
print 'No, it is a little higher than that.' # another block
# You can do whatever you want in a block ...
else:
print 'No, it is a little lower than that.'
# you must have guess > number to reach here
print 'Done'
# This last statement is always executed, after the if statement
# is executed.
# Filename : if.py
number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.' # new block starts here
print "(but you don't win any prizes!)" # new block ends here
elif guess < number:
print 'No, it is a little higher than that.' # another block
# You can do whatever you want in a block ...
else:
print 'No, it is a little lower than that.'
# you must have guess > number to reach here
print 'Done'
# This last statement is always executed, after the if statement
# is executed.

while 循环语句

while ... [else ...] ,示例:(else 可选)

#!/usr/bin/python
# Filename : while.py
number = 23
stop = False
while not stop:
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.'
stop = True # This causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that.'
else: # you must have guess > number to reach here
print 'No, it is a little lower than that.'
else:
print 'The while loop is over.'
print 'I can do whatever I want here.'
print 'Done.'
# Filename : while.py
number = 23
stop = False
while not stop:
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.'
stop = True # This causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that.'
else: # you must have guess > number to reach here
print 'No, it is a little lower than that.'
else:
print 'The while loop is over.'
print 'I can do whatever I want here.'
print 'Done.'

内置函数和对象

函数

数学/逻辑/算法

其他

__import__(name, globals, locals, fromlist) -> module : 动态加载模块

def importName(modulename, name):
""" Import name dynamically from module
Used to do dynamic import of modules and names that you know their
names only in runtime.
Any error raised here must be handled by the caller.
@param modulename: full qualified mudule name, e.g. x.y.z
@param name: name to import from modulename
@rtype: any object
@return: name from module
"""
module = __import__(modulename, globals(), {}, [name])
return getattr(module, name)
""" Import name dynamically from module
Used to do dynamic import of modules and names that you know their
names only in runtime.
Any error raised here must be handled by the caller.
@param modulename: full qualified mudule name, e.g. x.y.z
@param name: name to import from modulename
@rtype: any object
@return: name from module
"""
module = __import__(modulename, globals(), {}, [name])
return getattr(module, name)
