控制流程 if while for 模块 使用sys模块 自定义模块 集合 列表 元组 字典 序列 多线程 异常 面向对象 self 创建一个类 对象的方法 __init__方法 __del__方法 变量 权限控制 继承 __init__, __del__在继承中的使用 字符串函数 修改 查找 检测 编码 遍历
控制流程 if 1 2 3 4 5 6 7 tmp = 0 if tmp > 0 : print (">" ) elif tmp < 0 : print ("<" ) else : print ("=" )
while 1 2 3 4 5 6 tmp = 0 while tmp < 3 : print (tmp) tmp +=1 else : print ("over" )
for 1 2 3 4 5 6 7 8 9 10 for i in [0 ,1 ,2 ,3 ,4 ]: print (i) if i > 2 : break else : continue else : print ('loop over' )
或者
1 2 3 for i in range (0 , 10 , 1 ) print (i)
模块 模块是一个包含函数和变量的文件。为了在其他程序中重用模块,模块的文件名必须以.py为扩展名。
使用sys模块 1 2 3 4 import sysfor argv in sys.argv : print (argv)
使用from..import.., import可以使用*
1 2 3 4 from sys import argvfor argvtmp in argv : print (argvtmp)
模块的name,下面的语法输出当前模块的name
自定义模块
建立mymodule.py文件1 2 3 4 def printModuleName (): print (__name__)
建立test_mymodule.py文件1 2 3 import mymodulemymodule.printModuleName()
需要注意的是mymodule.py文件的Filename必须和文件名相同
如果module的name是__main__说明这个module是由用户启动的
集合 列表
声明一个列表 list = [123, "ad"]
索引第一个元素 list[0]
在尾部添加一个元素 list.append(2.56)
对第一个元素重新赋值 list[0] = "0"
获取列表长度 len(list)
删除第一个元素 del list[0]
元组 元组和列表十分类似,只不过元组和字符串一样是 不可变的 即你不能修改元组
1 2 3 tumple = (123 , "adf" ) print (tumple[0 ])print (len (tumple))
字典 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 map = { "key1" :"value1" , "key2" :"value2" , "key3" :"value3" , } print (map )print (len (map ))print (map ["key1" ])del map ["key1" ]print (map )for key in map : print (key + " " + map [key]) if "key2" in map : print ("map contains key2" ) help (dict )
序列 序列的两个主要特点是索引操作符和切片操作符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 shoplist = ['apple' , 'mango' , 'carrot' , 'banana' ] print ('Item 0 is' , shoplist[0 ])print ('Item -1 is' , shoplist[-1 ])print ('Item 1 to 3 is' , shoplist[1 :3 ])print ('Item 2 to end is' , shoplist[2 :])print ('Item 1 to -1 is' , shoplist[1 :-1 ])print ('Item start to end is' , shoplist[:])name = 'swaroop' print ('characters 1 to 3 is' , name[1 :3 ])print ('characters 2 to end is' , name[2 :])print ('characters 1 to -1 is' , name[1 :-1 ])print ('characters start to end is' , name[:])
多线程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import socketimport threadingimport timecount = 0 def socketSendData (): client=socket.socket(socket.AF_INET,socket.SOCK_STREAM) client.connect(('www.baidu.com' ,80 )) time.sleep(1 ) for i in range (0 , 20000 , 1 ): try : t = threading.Thread(target=socketSendData) info = t.start() except : count += 1 print "Error: unable to start thread " + str (count)
异常 捕获异常
1 2 3 4 5 6 7 try : fh = open ("test" , "w" ) except IOError: print "Error: open error" else : print "hava open file" fh.close()
抛出异常raise [Exception [, args [, traceback]]]例如
1 2 if (num < 100 ): raise RuntimeError("num is greater than 100!" )
面向对象 self 类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称self
创建一个类 1 2 3 4 5 class Person : pass p = Person() print p
对象的方法 1 2 3 4 5 6 class Person : def run (self ): print ("run" ) p = Person() p.run()
__init__方法 __init__方法在类的一个对象被建立时,马上运行
1 2 3 4 5 6 7 8 class Person : def run (self ): print ("run" ) def __init__ (self ): print ("init" ) p = Person() p.run()
__del__方法 1 2 3 4 5 6 7 class Person : def __init__ (self ): print ("init" ) def __del__ (self ): print ("__destory__" ) p = Person()
变量
1 2 3 4 5 6 7 8 class Father : age = 0 father = Father() father.age = 10 Father.age = 20 print (father.age)print (Father.age)
权限控制 对象的属性(变量和方法)如果名字以__开头则不能被外部访问,但是如果名称构成形式为__xxx__则被称为特殊属性,是可以被外界访问的.
继承 1 2 3 4 5 6 7 8 9 10 11 12 class Father : name = "Tom" def run (self ): print ("run" ) class Son (Father ): pass son = Son() print (son.name)son.run()
__init__, __del__在继承中的使用Python不会自动调用父类的constructor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Mother : pass class Father : name = "Tom" def run (self ): print ("run" ) def __init__ (self ): print ("Father init" ) def __del__ (self ): print ("Father del" ) class Son (Father, Mother ): def __init__ (self ): print ("Son init" ) def __del__ (self ): print ("Son del" ) son = Son() print (son.name)son.run()
字符串函数 修改
capitalize(...) : Python capitalize()将字符串的第一个字母变成大写,其他字母变小写。print "abc".capitalize()
center(...) : 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格。print "abc".center(10, "1")结果为111abc1111
expandtabs(...) : S.expandtabs(tabsize=8) -> str 把字符串中的 tab 符号(‘\t’)转为空格,默认的空格数 tabsize 是 8。
format(...) : "abcd{0}fg".format("sdf") {0}被替换成sdf
join(...) : S.join(iterable) -> str 用于将序列中的元素以指定的字符连接生成一个新的字符串。
ljust(...) : S.ljust(width[, fillchar]) -> str 返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
rjust(...) : S.rjust(width[, fillchar]) -> str 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串。
lower(...) : S.lower() -> str 转换字符串中所有大写字符为小写。
lstrip(...) : S.lstrip([chars]) -> str 用于截掉字符串左边的空格或指定字符。
replace(...) : S.replace(old, new[, count]) -> str 把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
split(...) : S.split(sep=None, maxsplit=-1) -> list of strings 通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串. 另外参考rsplit(...)
strip(...) : S.strip([chars]) -> str 用于移除字符串头尾指定的字符(默认为空格)。
rstrip(...) : S.rstrip([chars]) -> str 删除 string 字符串末尾的指定字符(默认为空格).
swapcase(...) : S.swapcase() -> str 用于对字符串的大小写字母进行转换。
translate(...) : S.translate(table) -> str 根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 del 参数中。
upper(...) : S.upper() -> str 将字符串中的小写字母转为大写字母。
zfill(...) : S.zfill(width) -> str 返回指定长度的字符串,原字符串右对齐,前面填充0。
title(...) : S.title() -> str 返回”标题化”的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())。
查找
count(...) : 用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。"abcdefg".count("c", 2, 4)结果为1
find(...) : S.find(sub[, start[, end]]) -> int 检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。
rfind(...) : S.rfind(sub[, start[, end]]) -> int 返回字符串最后一次出现的位置,如果没有匹配项则返回-1。
endswith(...) : 用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数”start”与”end”为检索字符串的开始与结束位置。"abcdefg".endswith("d", 2, 4)结果为true
index(...) : S.index(sub[, start[, end]]) -> int检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。
rindex(...) : S.rindex(sub[, start[, end]]) -> int 返回子字符串 str 在字符串中最后出现的位置,如果没有匹配的字符串会报异常,你可以指定可选参数[beg:end]设置查找的区间。
partition(...) : S.partition(sep) -> (head, sep, tail) Search for the separator sep in S, and return the part before it,the separator itself, and the part after it. If the separator is not found, return S and two empty strings.
rpartition(...) : S.rpartition(sep) -> (head, sep, tail) 类似于 partition()函数,不过是从右边开始查找.
startswith(...) : S.startswith(prefix[, start[, end]]) -> bool 用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。
检测
isalnum(...) : S.isalnum() -> bool 判断字符串是否包含字母数字。
isalpha(...) : S.isalpha() -> bool 检测字符串是否只由字母组成。
isdecimal(...) : S.isdecimal() -> bool 检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。
isdigit(...) : S.isdigit() -> bool 检测字符串是否只由数字组成。
islower(...) : S.islower() -> bool 检测字符串是否由小写字母组成。
isnumeric(...) : S.isnumeric() -> bool 检查是否只有数字字符组成的字符串。这种方法目前只对unicode对象。
isspace(...) : S.isspace() -> bool 是否只由空格组成。
istitle(...) : S.istitle() -> bool 检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。
isupper(...) : S.isupper() -> bool 检测字符串中所有的字母是否都为大写。
编码
encode(...) : encoding 指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案。S.encode(encoding='utf-8', errors='strict') -> bytes
遍历
splitlines(...) : S.splitlines([keepends]) -> list of strings 返回一个字符串的所有行,可选包括换行符列表(如果num提供,则为true)