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 ......
对搜索引擎、文件索引、文档转换、数据检索、站点备份或迁移等应用程序来说,经常用到对网页(即HTML文件)的解析处理。事实上,通过Python语言提供的各种模块,我们无需借助Web服务器
或
者Web浏览器就能够解析和处理HTML文档。本文将详细介绍如何利用Python抓取和解析网页。首先,我们介绍一个可以帮助简化打开位于本地和 ......
#!/usr/bin/python
#coding=utf-8
import Image,ImageDraw,ImageFont,os,string,random,ImageFilter
def initChars():
"""
允许的字符集合,初始集合为数字、大小写字母
usage: initChars()
param: None
return: list
返回允许的字符集和
for: picChecker类初始字符集合
todo: ......
1. 第二章 语法及代码约定
&nb ......
最近看可爱的python,里面有很多以前没有注意的东西。书是借的,期待到2月27号,社区解除图书静默。
图书大概分了3个部分。第一部分主要是本地编程的过程,第二部分主要是网页编程。第三部分是小纸条,
提供了很多知识点的速记。
以前也上过woodpecker,正如网站上写的,确实很乱。我经常不知道看哪,这个只能多逛。书中 ......