>>> menber=["小甲魚","不定","怡欣","mt"]
>>> for each in menber:
print(each,len(each))
python的内置對象預覽:
Number(數字):3.0145,1234,99L,3+4j(負數常量)
String(字元串):'sapm',"紅色經'kkk'典"
List(清單):[1,[2,'three points'],4]
Dictionary(字典):{'food':'spam','taste':'yum'}
Tuple(元組):(1,'spam',4,'U')
File(檔案):text=open{'segg','r'}.read()
python的比較操作符與java一樣
> 大于
< 小于
------------------------------------------------------------
條件分支文法:
①if 條件:
→縮進 條件為真執行
else:
→縮進條件為假執行操作
②while
while 條件:
條件為真執行操作
and邏輯操作運算符
随機:random子產品
randint(),會傳回一個随機整數
類型轉換
整數→字元串str()例如str(132412)變為'132412'
整數→浮點型float()
int()注意:浮點數轉換為整數時會采取截斷處理。
擷取類型資訊
type()傳回類型
例子:a='reui'
type(a)
isinstance()方法
例子:isistance('eq',str)
傳回一個布爾類型值。是否是這個類型
循環:
while 循環:
while 條件:、
循環體
for循環:
for 目标 in 表達式清單:
range()
文法:range() ([strat,] stop[,step=1])
step=1,預設的值為1;range作用是生産一個從start參數的值開始到stop參數的數字序列
清單:
因為python中變量沒有類型,而數組元素的類型是相等的,是以python沒有數組,是以清單是加強版的數組~
①建立普通清單
例如:數組名=[1,23,3,4,4,4,4]
②混合清單(清單的成員變量類型包括很多類型)
③建立空清單:empty=[]
對清單的操作:
顯示長度→len(清單名)
向清單中添加元素→清單名.append(變量)
向清單中插入清單→清單名.extend([變量1,變量2 ,] )
插入清單中任意位置→清單名.insert(2,"ds") 插入第二個位置
删除清單元素→remove("成員變量")
del menber[4]→删除第五個成員
傳回并删除該值→pop(5) 删除第6個元素
清單的分片slice
menber[1:3] :将會顯示第二個和第三個成員變量,形成了對源清單的拷貝!
清單的比較操作符:
>>> list1=[123,345]
>>> list2=[234,123]
>>> list1>list2
False
隻要清單1的第一個元素大于清單2,那麼,後面的數就不用比較了。
+号運算
>>> list1+"xiaojiayu"
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
list1+"xiaojiayu"
TypeError: can only concatenate list (not "str") to list
>>> list1+list2
[123, 345, 234, 123]
>>>
*号運算
>>> list1*3
[123, 345, 123, 345, 123, 345]
in運算符 隻能夠影響一層
>>> list5=[123,["xiaojiayu","why"]]
>>> list4=[123,"xiaojiayu","why"]
>>> "why" in list4
True
>>> "why" in list5
False
>>>
檢視list的内置函數:
>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>
使用:
count 計數
>>> list4.count(123)
1
轉置reverse()
排序sort(0):預設是從小到大排序
>>> list6=[1,0,9,5,4,7,6,2,11,10]
>>> list6.sort()
>>> list6
[0, 1, 2, 4, 5, 6, 7, 9, 10, 11]
>>> list6.reverse()
>>> list6
[11, 10, 9, 7, 6, 5, 4, 2, 1, 0]
>>>
或者
>>> list6.sort(reverse=True)
>>> list6
[11, 10, 9, 7, 6, 5, 4, 2, 1, 0]
>>>
清單的複制:
>>> list7=list6[2:9]
>>> list7
[9, 7, 6, 5, 4, 2, 1]
如果是用=号時,就是給這個清單起了另一個名字,而分片兒複制則會在記憶體中實實在在的配置設定存儲空間。
元組:戴上了加鎖的清單(不能随意插入,删除等操作)
>>> tuple1=(1,2,3,5,8,6,9)
>>> tuple1
(1, 2, 3, 5, 8, 6, 9)
>>> tuple1[3]
5
>>> tuple1[1:3]
(2, 3)
>>> temp=(1)
>>> type(temp)
<class 'int'>
>>> type(temp1=(1,))
TypeError: type() takes 1 or 3 arguments
>>> temp=(1,)
>>> type(temp)
<class 'tuple'>
>>>
插入操作:(生成新的元組)
>>> temp=("意境","和","下架與")
>>> temp=temp[:2]+("哇")+temp[2:]
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
temp=temp[:2]+("哇")+temp[2:]
TypeError: can only concatenate tuple (not "str") to tuple
>>> temp=temp[:2]+("哇",)+temp[2:]
>>> temp
('意境', '和', '哇', '下架與')
>>>
字元串之内置方法
>>> str='i am fool ,yami'
>>> str
'i am fool ,yami'
>>> find("fool")
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
find("fool")
NameError: name 'find' is not defined
>>> find('fool')
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
find('fool')
NameError: name 'find' is not defined
>>> str.find('fool')
5
>>> str.join('123')
'1i am fool ,yami2i am fool ,yami3'
>>> "{a} love {b} {c}".format("i" ,"want" ,"to do")
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
"{a} love {b} {c}".format("i" ,"want" ,"to do")
KeyError: 'a'
>>> "{a} love {b} {c}".format(a="i" ,b="want" ,c="to do")
'i love want to do'
>>> "{1} love {2} {3}".format("i" ,"want" ,"to do")
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
"{1} love {2} {3}".format("i" ,"want" ,"to do")
IndexError: tuple index out of range
>>> "{0} love {1} {2}".format("i" ,"want" ,"to do")
'i love want to do'
>>>
序列:
清單,數組和字元串的共同點
可以通過索引得到每個元素
索引預設為從0開始
可以通過分片的方法得到一個範圍内的元素的集合
共同操作符
list()将一個可疊代對象轉換為清單
list(iterable) -> new list initialized from iterable's items
>> help(list)
Help on class list in module builtins:
class list(object)
| list() -> new empty list
| list(iterable) -> new list initialized from iterable's items
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __delitem__(self, key, /)
| Delete self[key].
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __gt__(self, value, /)
| Return self>value.
|
| __iadd__(self, value, /)
| Implement self+=value.
|
| __imul__(self, value, /)
| Implement self*=value.
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mul__(self, value, /)
| Return self*value.n
|
| __ne__(self, value, /)
| Return self!=value.
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| __repr__(self, /)
| Return repr(self).
|
| __reversed__(...)
| L.__reversed__() -- return a reverse iterator over the list
|
| __rmul__(self, value, /)
| Return self*value.
|
| __setitem__(self, key, value, /)
| Set self[key] to value.
|
| __sizeof__(...)
| L.__sizeof__() -- size of L in memory, in bytes
|
| append(...)
| L.append(object) -> None -- append object to end
|
| clear(...)
| L.clear() -> None -- remove all items from L
|
| copy(...)
| L.copy() -> list -- a shallow copy of L
|
| count(...)
| L.count(value) -> integer -- return number of occurrences of value
|
| extend(...)
| L.extend(iterable) -> None -- extend list by appending elements from the iterable
|
| index(...)
| L.index(value, [start, [stop]]) -> integer -- return first index of value.
| Raises ValueError if the value is not present.
|
| insert(...)
| L.insert(index, object) -- insert object before index
|
| pop(...)
| L.pop([index]) -> item -- remove and return item at index (default last).
| Raises IndexError if list is empty or index is out of range.
|
| remove(...)
| L.remove(value) -> None -- remove first occurrence of value.
| Raises ValueError if the value is not present.
|
| reverse(...)
| L.reverse() -- reverse *IN PLACE*
|
| sort(...)
| L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None