運算符
+ - * /
// %
** (乘方)
Python 有兩種除法運算符,單斜槓用作傳統除法,
雙斜槓用作浮點除法(對結果進行四捨五入)。傳統除法是指如果兩個操作數都是整數的話,它將執行是地板除(取比商小的最大整數)
< <=
> >= ==
!= <>
兩種“不等於”比較運算符, != 和 <>
and or
not
變量和賦值
Python 是動態類型語言, 也就是說不需要預先聲明變量的類型
Python 也支持增量賦值,也就是運算符和等號合併在一起
Python 不支持C 語言中的自增1 和自減1 運算符, 這是因爲 + 和 - 也是單目運算符,Python 會將 --n 解釋爲-(-n) 從而得到 n , 同樣 ++n 的結果也是 n.
數字
int long
bool float
complex
字符串
索引運算符( [ ] )和切片運算符( [ : ] )索引規則:第一個字符 0,最後一個字符 -1
( + )連接,( * )重複
單引號或雙引號 三引號包含特殊字符
列表和元組
列表( [ ])元素的個數及元素的值可以改變。元組(( ))不可以更改
字典({ })
>>> a={'ho':'22'}
>>> a['wq']=8
>>> a
{'ho': '22', 'wq': 8}
>>> a.keys()
['ho', 'wq']
>>> a['wq']
8
>>> a['wq']=8
>>> a
{'ho': '22', 'wq': 8}
>>> a.keys()
['ho', 'wq']
>>> a['wq']
8
>>> for key in a:
... print key,a[key]
...
ho 22
wq 8
... print key,a[key]
...
ho 22
wq 8
if 語句
if expression:
if_suite
if_suite
if expression:
if_suite
else:
else_suite
if_suite
else:
else_suite
if expression1:
if_suite
elif expression2:
elif_suite
else:
else_suite
if_suite
elif expression2:
elif_suite
else:
else_suite
while 循環
while expression:
while_suite
while_suite