“abc”*3
“abcabcabc”
True False
比較運算符
== 等於
!=
賦值運算符
+=
a+=1
a=a+1
沒有++,--只有 a+=1 a-=1
**= 冪賦值運算符 c**=a c=c**a
//= 取整數運算符c//=a c=c//a
邏輯運算符
And , x and y
Or ,x or y
Not , not x
位運算符
& | ^ ~ << >>
成員運算符
In not in
身份運算符
Is 類似於 id(a)==id(b) 判斷兩個對象是不是引用自同一個對象 id()用於獲取對象內存地址
Is not
Python3六個標準的數據類型
1,Number
Int bool float *
complex
2,String *
3List *
4,Tupe
5,Set
6,Dictionary *
python2中無true and false 用1和0代替,3中,true與false是關鍵字,值分別是1和0
字符串
字符串截取
變量[x:y]
字符串*2 *表示複製當前字符串,數字表示覆制次數
列表
列表中類型可以不相同
>>> a[0:]
[10,’hello', 30, 'nihaoxiaoyiyi']
>>> b=[70,90]
>>> a+b
[10, 'hello', 30, 'nihaoxiaoyiyi', 70, 90]
>>> b*2
[70, 90, 70, 90]
>>>
Tuple
元祖,元素不能改變,可以相同
a=(10,20)
b=(30,40)
a+b爲(10, 20, 30, 40) ,但是a和b中的元素不能改
>>> a=(10,20,10)
>>> b=(20,20)
>>> a+b
(10, 20, 10, 20, 20)
Set 無序不重複的序列
-,&,|,^
>>> a={10,20,30}
>>> type(a)
<type 'set'>
>>> a=set(). #定義一個空set
>>> a.add(1)
>>> a
set([1])
>>> a.add(5)
>>> a
set([1, 5])
>>> b={30,40,50}
>>> b
set([40, 50, 30])
>>>
字典
有序對象的集合
{“name”:”xiaoyi”,”age”:18}
a[‘name’]
>>> a={} #空括號定義dic
>>> type(a)
<type 'dict'>
>>>
遍歷字典
>>> a={"name":"xiaoyiyi","age":18}
>>> for i in a: #I是key值
... print a[i]
...
18
xiaoyiyi
range(10):輸出0-9的列表
range(2,7):2-6的列表
>>> for i in range(0,51,5): #step爲5
... print i
...
0
5
10
15
20
25
30
35
40
45
50
>>>
>>> for i in range(10,0,-1): #遞減
... print i
...
10
9
8
7
6
5
4
3
2
1
>>>
使用range()和len()結合遍歷輸出容器類數據,優點是可以把i輸出
>>> for i in range(len(a)):
... print a[i]
...
aaa
bbb
ccc
>>>
>>> for i in range(len(a)):
... print i
...
0
1
2
>>>
round() 四捨五入
>>> import math
>>> math.ceil(4.0001)
5.0
>>> math.floor(4.999)
4.0
>>>
>>> import ramdom
>>> random.random()
0.3898325921927488
>>> random.randrange(0,11,2) #隨機0到10的偶數
6
>>> random.randrange(0,11)#隨機0到10的數
5
>>>
>>> random.choice([10,20,30,40])#列表中隨機選一個
40
>>> print(r"aaa\\''bb") #原樣輸出
aaa\\''bb
>>> print("aaa\\''bb")
aaa\''bb
>>>print ("%s---%d"%("nihao",5)) string格式化輸出
>>> print("4.5556==>%0.2f"%(4.5556))
4.5556==>4.56
>>>
字符串查找,find index都可以,index找不到會報錯,find返回-1
>>> name
'chenjian'
>>> name.find("ss")
-1
>>> name.index("ss")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>>
Str 去除空格
>>> s=" name "
>>> s
' name '
>>> len(s)
6
>>> s.strip()
'name'
>>> s
' name '
>>> len(s.strip())
4
>>>
List
>>> a
['aaa', 'bbb', 'ccc', 60]
>>> a.append([10,20,30])
>>> a
['aaa', 'bbb', 'ccc', 60, [10, 20, 30]]
>>> a.extend(10,20,30)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extend() takes exactly one argument (3 given)
>>> a.extend([10,20,30])
>>> a
['aaa', 'bbb', 'ccc', 60, [10, 20, 30], 10, 20, 30]
>>>
>>> a.pop(0)
'aaa'
>>> a
['bbb', 30, 'ccc', 60, [10, 20, 30], 10, 20, 30]
>>> a.pop(0)
'bbb'
>>> a
[30, 'ccc', 60, [10, 20, 30], 10, 20, 30]
>>>
>>> a.remove("ccc")
>>> a
[30, 60, [10, 20, 30], 10, 20, 30]
>>>
Python 2.7沒有copy嗎???
>>> a
[30, 60, [10, 20, 30], 10, 20, 30]
>>> b=a.copy()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'copy'
>>>
>>> t=(2) #一個元素的元祖,默認不是元祖
>>> t
2
>>> type(t)
<type 'int'>
>>> tt=(3,)
>>> tt
(3,)
>>> type(tt)
<type 'tuple'>
>>>
Set是無序不重複元素集合
set()或者{}來創建集合,空集合只能是set(),{}是用來創建字典的
>>> s=set()
>>> s={1,2,3,4,5}
>>> s[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
>>> s.add(8)
>>> s
set([1, 2, 3, 4, 5, 8])
>>> s.update([9,10])
>>>
>>> s
set([1, 2, 3, 4, 5, 8, 9, 10])
>>> s.remove(3)
>>> s
set([1, 2, 4, 5, 8, 9, 10])
>>> s,remove(6)#不存在的話就報錯
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'remove' is not defined
>>> s.discard(6)#不存在不會報錯
>>> s
set([1, 2, 4, 5, 8, 9, 10])
>>> s.disacrd()8
File "<stdin>", line 1
s.disacrd()8
^
SyntaxError: invalid syntax
>>> s.disacrd(8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'set' object has no attribute 'disacrd'
>>> s.discard(8)
>>> s
set([1, 2, 4, 5, 9, 10])
>>>
Dic
d.items()返回鍵值對
d.get(“aaa”)
>>> d={"name":"Hedy","age":18,"sex":"f"}
>>> d
{'age': 18, 'name': 'Hedy', 'sex': 'f'}
>>> d.get("habit")
>>> d.get("habit","drawing”)#如果這個值不在字典裏,就設置值爲drawing
'drawing'
>>> d["habit"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'habit'
>>>
>>> d.update({'age':22,"phone":'11111’})#批量更改
>>> d
{'phone': '11111', 'age': 22, 'name': 'Hedy', 'sex': 'f'}
>>
>>> d.pop("phone")
'11111'
>>> d
{'age': 22, 'name': 'Hedy', 'sex': 'f'}
>>>