python语法介绍
1.1. 语法
1.1.1. if
>>> x=int(raw_input("please enter an integer:"))
please enter an integer:-8
>>> if x<0:
... print 'negative'
... elif x==0:
... print 'zero'
... else:
... print 'positive'
...
negative
这里有几个知识点需要提醒:
1。和shell中if语句的区别
我们来回顾一下shell中的if语句。
shell中if语句的结构是:
if 表达式
then 命令表
[else]
fi
举个简单的例子:ifsingle
#!/bin/bash
#filename:ifsingle
echo "please enter the first string:"
read word1
echo "please enter the second string:"
read word2
echo "_______________"
if test $word1 = $word2 这里注意$word1和$word2之间的等号前后必须有空格,不然就变成了赋值语句,程序的功能就体现不出来了。
then
echo " the first string is equal to the second string"
fi
echo "the program has finished"
并且if语句可以无限层的嵌套在其他if语句中。
python中的是一个if,elif,else语句实现多路判断
我们同样举一个相似的例子:
使用vi编辑器进行编辑:
#!/bin/bash
#filename:ifelif
echo "please enter the first argu:"
read str1 z注意这里切不要使用$1,因为$1是指除了程序名的第一个参数
echo "please enter the second argu:"
read str2
echo "please enter the third argu:"
read str3
if test $str1 = $str2 -a $str2 = $str3 test 用[ ]是test的语法,需要用-a表示逻辑与,当然test 也可以不实用[]
then
echo "the three argu are same"
elif test $str1 = $str2
then
echo "the first argu is equal to the second argu"
elif test $str2 = $str3
then
echo "the second argu is equal to the third argu"
elif test $str1 = $str3
then
echo "the third argu is equal to the first argu"
else
echo " they are different"
fi
2
相关文档:
zz from 《可爱的Python》
http://www.woodpecker.org.cn/
Python标准库 http://www.woodpecker.org.cn:9081/doc/Python/_html/PythonStandardLib/
简明Python教程 http://www.woodpecker.org.cn:9081/doc/abyteofpython_cn/chinese/index.html
Python快速介绍 http://www.zoomquiet.org/share/s5/intropy/070322-intro ......
俄罗斯方块游戏,使用Python实现,总共有350+行代码,实现了俄罗斯方块游戏的基本功能,同时会记录所花费时间,消去的总行数,所得的总分,还包括一个排行榜,可以查看最高记录。
排行榜中包含一系列的统计功能,如单位时间消去的行数,单位时间得分等。
附源码:
from Tkinter import *
from tkMessageBox import *
i ......
原文出处:http://www.amk.ca/python/howto/regex/
原文作者:A.M. Kuchling (amk@amk.ca)
授权许可:创作共享协议
翻译人员:FireHare
校对人员:Leal
适用版本:Python 1.5 及后续版本
简介
Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式。Python 1.5之前版本则是通过 regex
模块提供 ......
8.Python中没有switch语句,可以用if..elif..else语句完成同样的工作(某些场合,使用字典会更加快捷)
9.while语句包含一个else的从句.
10.range向上延伸到第二个数,即它不包含第二个数.
11.使用global语句可以清楚地表明变量是在外面的块定义的. ......