Python基礎教程---讀書筆記一

1. 除法運算:整數相除,結果只留整數,除非python

1)用浮點數ide

2)明確python除法執行方式:from __future__ import division函數

>>> 1/2spa

0code


2. 整除運算(include float)://字符串

>>> 5.0//2input

2.0it


3. 取餘運算(including float): %io

>>> 2.75%0.5ast

0.25


4. 冪運算(include float): **

>>> (-2.0)**2

4.0


5. 長整型數:L


6. 變量不能以數字開頭,使用以前賦值


7. 單引號字符串和轉義引號:

>>> "Let's go!"

"Let's go!"

>>> 'lets go!'

'lets go!'

>>> 'Let\'s say \"hello world!\"'

'Let\'s say "hello world!"'


8. 字符串拼接:+, 字符串和數字不能夠直接拼接

>>> print "tmp is " + tmp

Traceback (most recent call last):

 File "<stdin>", line 1, in <module>

TypeError: cannot concatenate 'str' and 'int' objects

>>> print "tmp is %d" % tmp

tmp is 2

>>> print "tmp is " + str(tmp)

tmp is 2


9. 長字符串使用三個單引號或者三個雙引號,中間能夠包含單引號和雙引號


10. 原始字符串:以r開頭,最後一個字符不能是反斜線,除非進行轉義


11. Unicode字符串:以u爲前綴


12. 經常使用函數

raw_input(prompt): 獲取用戶輸入,返回類型爲字符串,儘可能使用raw_input

int(object): 將數字字符串和數字轉換爲整數

long(object): 將數字字符串和數字轉換爲長整型數

float(object): 將數字字符串和數字轉換浮點數

str(object): 將值轉換爲合適形式的字符串

repr(object): 返回值的字符串表示