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 ......
最近在研读Python源码剖析一书,此书相当不错,如果自己冲动的去分析Python源码可能会到处碰“鼻”,看到此书时是09年,那时为了研究内存机制才发现有这么一本书,但是工作太忙,根本没时间去分析源码,到了2010年,这是非常有深重意义的一年,所以这一年一定要比之前做的还要付出更多,要想成为技术顶尖就必须研 ......
今天学习了一下Python的操作符重载,总结了几点比较神奇的东东:
------------------------------------------------------------------------------------------------------------
关于iter:
Technically, iteration contexts work by calling the iter built-in function to try to
find an _ _iter_ _ method, whi ......
(1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如:
python d:\pythonSrc\test\test.py
&nb ......