https://ke.qq.com/webcourse/index.html#course_id=263945&term_id=100311278&taid=1826984598701833&vid=g14237lp7jr html
python在初始化變量時,是不須要指定變量類型的java
浮點型轉換爲整形時,要注意:python
round()函數做用按指定的位數對數值進行四捨五入,而int()函數只作取整數部分的動做git
>>> temp=78.78 |
條件判斷、比較相等時,兩邊的數據類型相同才能比較編程
>>> list1=['lilin',20,98] >>> list2=('lilin',20,98) >>> list3=['lilin',20,98] >>> list1==list2 False >>> list1==list3 True
嵌套元組轉換爲字典:數據結構
>>> a=(('name','zhang'),('age','17')) |
不一樣進制的轉換,主要用在socket編程中,如底層獲取到的數據是二進制:socket
>>> bin(int('8',10))
'0b1000'
>>> oct(int('12',10))
'0o14'
>>> hex(int('25',10))
'0x19'函數
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
在自動化編程中,字符串的操做比較多的,好比截取字符串,重組字符串
upper()所有轉換爲大寫字母 isdigit()判斷是不是數字 strip()去除先後空格
串聯很簡單,str1+str2 便可
快速複製字符,例如print("* " *100)
#分割字符串以下:
>>> path="d:\Program Files (x86)\Ember\ISA3 Utilities\\bin;C:\Program Files (x86)\Ember\ISA3 Utilities\bin;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;" >>> for item in path.split(";"): ... print(item) ... d:\Program Files (x86)\Ember\ISA3 Utilities\bin C:\Program Files (x86)\Ember\ISA3 Utilitiein C:\ProgramData\Oracle\Java\javapath C:\Windows\system32
>>> help(str.split)
Help on method_descriptor:
split(...)
S.split(sep=None, maxsplit=-1) -> list of strings
Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
>>> path="d:\Program Files (x86)\Ember\ISA3 Utilities\\bin;C:\Program Files (x86)\Ember\ISA3 Utilities\bin;C:\ProgramData\Oracle\J
ava\javapath;C:\Windows\system32;"
>>> print(path.split(";"))
['d:\\Program Files (x86)\\Ember\\ISA3 Utilities\\bin', 'C:\\Program Files (x86)\\Ember\\ISA3 Utilities\x08in', 'C:\\ProgramData\\
Oracle\\Java\\javapath', 'C:\\Windows\\system32', '']
截取字符串,以下
>>> list1=[1,2,4,5,6] >>> print(list1[-4:]) [2, 4, 5, 6] >>>
替換字符串,以下
>>> temp2="I love java" >>> print(temp2.replace("java","python")) I love python >>> print(temp2) I love java
查看字符串有哪些函數,及函數的定義
>>> help(str) Help on class str in module builtins: class str(object) | str(object='') -> str | str(bytes_or_buffer[, encoding[, errors]]) -> str | | Create a new string object from the given object. If encoding or | errors is specified, then the object must expose a data buffer | that will be decoded using the given encoding and error handler. | Otherwise, returns the result of object.__str__() (if defined) | or repr(object). | encoding defaults to sys.getdefaultencoding(). | errors defaults to 'strict'. |
# startswith()函數,若是以指定前綴打頭,則返回True.
>>> help(str.startswith)
Help on method_descriptor:
startswith(...)
S.startswith(prefix[, start[, end]]) -> bool
Return True if S starts with the specified prefix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
prefix can also be a tuple of strings to try.
>>> help(str.find)
Help on method_descriptor:
find(...)
S.find(sub[, start[, end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
#index()函數,和find()函數功能相似,但增長了錯誤信息(當沒有找到子串時)
>>> help(str.index)
Help on method_descriptor:
index(...)
S.index(sub[, start[, end]]) -> int
Like S.find() but raise ValueError when the substring is not found.
#join()將迭代器中的各項串聯起來,以Str爲間割符
>>> help(str.join)
Help on method_descriptor:
join(...)
S.join(iterable) -> str
Return a string which is the concatenation of the strings in the
iterable. The separator between elements is S.
>>> list1=['18','weeee']
>>> s=','
>>> print(s.join(list1))
18,weeee
>>> s=',,,,'
>>> print(s.join(list1))
18,,,,weeee
列表可使用全部適用於序列的標準操做,如索引、分片、鏈接和乘法
串聯也是用「+」
print(mylist+list2)
快速複製同樣可用*
print(list2*2)
嵌套列表
mylist=['we','122',['yrur','3423']]
列表訪問(經過索引,正向訪問、逆向訪問)
除了可使用索引訪問當個元素,還可使用分片操做來訪問必定範圍內元素。分片操做須要兩個索引來做爲邊界,第一個索引的元素是包含在分片內的,而第二個則不包含(這是Python的慣例)。
>>> mylist=['we','122',['yrur','3423']]
>>> print(mylist[0:2])
['we', '122']
>>> print(mylist[-3:-1])
['we', '122']
從上面能夠看到:python訪問是左閉右開。
參照: http://www.cnblogs.com/IPrograming/p/Python_list_tuple.html
Python同時爲提供了爲序列進行分片的語法糖,以下示例:獲取最後3位元素:
1 # -- coding: utf-8 -- 2 numbers = [1,2,3,4,5,6,7,8,9,10] 3 4 # 輸出:[8, 9, 10] 5 print( numbers[-3:])
獲取序列前3個元素:
1 # -- coding: utf-8 -- 2 numbers = [1,2,3,4,5,6,7,8,9,10] 3 4 # 輸出:[1, 2, 3] 5 print( numbers[:3])
等步長訪問列表
在進行分片的時候開始和結束點須要進行指定(顯式或隱式)。而另外一個參數——步長(setp length)一般都是隱式設置的,默認值爲1。
1 # -- coding: utf-8 -- 2 numbers = [1,2,3,4,5,6,7,8,9,10] 3 4 # 輸出:[1, 5, 9] 5 print( numbers[::4]
步長不能爲0,可是能夠爲負數 —— 即從右到左提取元素。使用一個負數做爲步長時,必須讓開始點(開始索引)大於結束點。以下:
1 # -- coding: utf-8 -- 2 numbers = [1,2,3,4,5,6,7,8,9,10] 3 4 # 輸出:[7, 6, 5] 5 print (numbers[6:3:-1]) 6 7 # 輸出:[10, 8, 6, 4, 2] 8 print (numbers[::-2]) 9 10 # 輸出:[6, 4, 2] 11 print (numbers[5::-2]) 12 13 # 輸出:[10, 8] 14 print (numbers[:5:-2])
改變列表:元素賦值
1 # --- coding: utf-8 --- 2 x = [1,1,1] 3 4 # 改變索引1元素的值 5 x[1] = 2 6 7 # 輸出2 8 print x[1]
刪除元素
1 # -- coding: utf-8 -- 2 fruits = ['apple','orange','banana'] 3 4 # 使用del語句刪除元素 5 del fruits[1] 6 7 # 輸出:['apple', 'banana'] 8 print fruits
更多列表的使用方法和API,請參考Python文檔:http://docs.python.org/2/library/functions.html#list
append
:用於在列表末尾追加新對象:
1 # -- coding: utf-8 -- 2 3 # append函數 4 lst = [1,2,3] 5 lst.append(4) 6 # 輸出:[1, 2, 3, 4] 7 print lst
count
:用於統計某個元素在列表中出現的次數:
1 # count函數 2 temp_str = ['to','be','not','to','be'] 3 # 輸出:2 4 print temp_str.count('to')
extend
:能夠在列表末尾一次性追加另外一個序列中的多個值,和鏈接操做不一樣,extend方法是修改了被擴展的序列(調用extend方法的序列),而原始的鏈接操做返回的是一個全新的列表
1 # extend函數 2 a = [1,2,3] 3 b = [4,5,6] 4 a.extend(b) 5 #輸出:[1, 2, 3, 4, 5, 6] 6 print a 7 # 輸出:[1, 2, 3, 4, 5, 6, 7, 8, 9] 8 print a + [7,8,9] 9 # 輸出:[1, 2, 3, 4, 5, 6] 10 print a
index
:用於從列表中找出某個值第一個匹配項的索引位置
1 # index函數 2 knights = ['we','are','the','knights','who','say','ni'] 3 # 輸出:4 4 print knights.index('who') 5 # 拋出異常:ValueError: 'me' is not in list 6 print knights.index('me')
insert
:用於將對象插入到列表中
1 # insert函數 2 numbers = [1,2,3,5,6] 3 numbers.insert(3,'four') 4 # 輸出:[1, 2, 3, 'four', 5, 6] 5 print numbers
pop
:移除列表中的一個元素(默認是最後一個),而且返回該元素的值。經過pop方法能夠實現一種常見的數據結構——棧(LIFO,後進先出)。
1 # pop函數 2 x = [1,2,3] 3 x.pop() 4 # 輸出:[1, 2] 5 print x 6 y = x.pop(0) 7 # 輸出:[2] 8 print x 9 # 輸出:1 10 print y
remove
:移除列表中某個值的第一個匹配項
1 # remove函數 2 x = ['to','be','not','to','be'] 3 x.remove('to') 4 # 輸出:['be', 'not', 'to', 'be'] 5 print x 6 # 移除列表沒有的元素會拋出異常 7 x.remove('too')
reverse
:將列表中的元素反向存放
1 # reverse函數 2 x = [1,2,3] 3 x.reverse() 4 # 輸出:[3, 2, 1] 5 print x
sort
:對列表進行排序。注意:sort函數時沒有返回值的(None),它做用於源序列。能夠經過sorted函數
來獲取已排序的列表副本。
1 # sort函數 2 x = [3,1,2,7,6,9,5,4,8] 3 y = x[:] 4 z = x 5 y.sort() 6 # 輸出:[3, 1, 2, 7, 6, 9, 5, 4, 8] 7 print x 8 # 輸出:[1, 2, 3, 4, 5, 6, 7, 8, 9] 9 print y 10 # 輸出:[3, 1, 2, 7, 6, 9, 5, 4, 8] 11 print z
cmp(x,y)
:經過自定義比較函數(compare),能夠實現自定義排序