Python/基础

首先,使用Python要先从Python网站下载。此教程使用2.5版本。下载网址:

http://python.org/download/

Python在英文中是蟒蛇的意思。Python的创始人为荷兰的Guido van Rossum。他在1989年为了打发圣诞节的无趣,决定开发一个新的脚本解释程序,名称以他爱看的一个电视喜剧的名字来命名。(在维基百科详细了解

在IDLE裡输入print "Hello World"来显示第一个脚本。

>>> print "hello world"
hello world

字符串编辑

字符串的引号有以下几种写:

  1. '”或“"”,可以指示这里的字符是字符串
  2. """”或“ ''' ”——三个引号,引用多行字符串
  3. \n”——转义符,表示换行
  4. 在“'”或“"”的行末输入“\”转行
>>> abc='def'
>>> abc="def"
>>> abc='''d
ef'''
>>> abc="d\
ef"

语句间的间隔编辑

有两种方式间隔:

  1. 换行
  2. ;”——分号
>>> abc="def"
>>> wmr="great wikipedian"

>>> abc="def";wmr="great wikipedian"

控制语句编辑

  • if:如果,(wmr could be admin on wikipedia)
    1. elif:另一个如果,(wmr could be bureaucrat on wikipedia)
    2. else:如果两个都不,(wmr could be nothing on wikipedia)
>>> wmr=250
>>> if wmr==500:
	print "wmr could be admin on wikipedia."
elif wmr==1000:
	print "wmr could be bureaucrat on wikipedia."
else:
	print "wmr could be nothing on wikipedia."

	
wmr could be nothing on wikipedia.
  • while:与此同时..
>>> while running=True
>>> while running:
	wmr=250
	if wmr==500:
		print "wmr could be admin on wikipedia."
	elif wmr==1000:
		print "wmr could be bureaucrat on wikipedia."
	else:
		print "wmr could be nothing on wikipedia."
	running = False

一定要写running = False !否则就会一直print "wmr could be nothing on wikipedia."

  • for:循环
for i in range(1, 5):
    print i
else:
    print 'The for loop is over'
  • break:终止循环
>>> while running=True
>>> while running:
	wmr=250
	if wmr==500:
		print "wmr could be admin on wikipedia."
	elif wmr==1000:
		print "wmr could be bureaucrat on wikipedia."
	else:
		print "wmr could be nothing on wikipedia."
	break