##language:zh
#pragma section-numbers on
'''
[[StartProgramming| (首页)开始编程之旅]]
翻译自Lee Harr的[[http://staff.easthighschool.net/lee/computers/book/Start_Programming.html|Start Programming]]

本文是使用[[http://www.nongnu.org/pygsear/|pygsear]]+[[http://pygame.org|pygame]]作为开发环境,以初级用户角度来分步分阶段学习[[http://www.python.org|PYTHON]]基本概念,并以小游戏开发项目为具体案例,介绍的十分详细。编写风格清新朴实,没有象一般教科书那样枯燥,极其适合初级用户来激发兴趣时使用。
'''
<<TableOfContents>>
= StartProgramming-2-1 引入Import =

At the top of most Python programs, you will see import statements like this:

'''在大部分的Python程序的开头,你都会看到 import 语句:'''

{{{#!python
import random
import os.path
from math import sqrt
from penguin import *
}}}

The import statement lets you quickly and easily load in python modules.

'''import 语句可以使我们快速并且简单在python中的加载组件.'''

Modules are useful code that is already written -- either code which someone else wrote or your own code. Before starting any programming project, it is a good idea to look around and see if there is already a module which will do what you need.

'''组件是已经写过的有用的代码 -- 在别人写的代码和你的代码之间。在开始一个编程项目之前,看一看有没有你需要用的已经写过的组件是一个不错的主意。'''

Why would you want to import a module?

'''你为什么想要导入一个组件?'''

If you need to make your game behave randomly, you might want to choose from different attack strategies:

'''如果你想要你的游戏表现的很随机,你可能想要选择不同的攻击策略:'''

{{{#!python
import random
strategies = ['aggressive', 'cautious', 'defensive']
strategy = random.choice(strategies)
}}}

Or, if your game needs to know the size of a file:

'''或者,假如你的游戏需要知道文件的大小:'''

{{{#!python
import os.path
os.path.getsize('.')
}}}

Or, if you need to know the distance between two objects:

'''或者,假如你想要知道两个物体之间的距离:'''

{{{#!python
from math import sqrt
x0, y0 = (150, 75)
x1, y1 = (275, 300)
distance = sqrt((x0 - x1)**2 + (y0 - y1)**2)
}}}

This distance formula (the Pythagorean Theorem) is so useful that it is included in the math module:

'''这个距离运算(毕达格拉斯定理)是如此的有用,所以它已经被收录到 math 组件当中了:'''

{{{#!python
import math
distance2 = math.hypot((x0 - x1), (y0 - y1))
}}}

You can see the values, and check if they are the same:

'''你能够看到变量的值,并且检查它们是否相等:'''

{{{#!python
print distance, distance2, distance == distance2
}}}

Or, if you just want to use the penguin graphics module:

'''或者,假如你正想使用企鹅绘图组件:'''

{{{#!python
from penguin import *
pete.star() 
}}}