1、print函式,在3.0一下直接 print 42可以將42列印出來,但在
3.0以後print作為函式,需要print(42)這樣使用。
2、input函式, x = input("x:")
3、輸入乙個很大的數,python會自動轉換為長整型,比如:100000000000 輸出:100000000000l
4、//整除號 1.0//2=0 1/2=0 1.0/2=0.5 1/2.=0.5
5、**冪運算子 2**3=8 -3**2=-9 (-3)**2=9,也可以用函式代替pow,pow(2,3) = 2**3 = 8
6、匯入模組 import math 匯入後可以使用math模組中的函式,如:math.floor(2.5) = 2.0
7、匯入函式 from math import sqrt 將sqrt函式匯入,然後就可以直接使用sqrt函式了
8、使用變數引用函式 foo=math.sqrt 則可以 foo(4) = 2
9、python中將數值轉換成字串三種方式:
a、str函式 print str(1000l) 輸出:1000
b、repr函式 print repr(1000l) 輸出:1000l,將數值轉換成標準的pytho表示式,建立乙個新的字串
c、反引號 『』 功能與repr函式相同,在3.0以後不可用
10、python中兩種輸入函式
a、input函式,python預設使用者輸入的是標準的python表示式。
例:name = input("enter name:"), 當執行時,
使用者輸入:tom,會報錯,
使用者輸入:"tom" 正常執行。
b、raw_input函式。他會把使用者輸入的當作原資料,然後將其放入字串中。上面例子中在這裡輸入tom是可執行的
例:>>> input("input a number:")
input a number: 3
3
>>> raw_input("input a number:")
input a number: 3
'3' #輸出的是字串
>>> x = input("input a number:")
input a number:2
>>> y = input("input a number:")
input a number:3
>>> x + y
5
>>> x = raw_input("input a number:")
input a number:2
>>> y = raw_input("input a number:")
input a number:3
>>> x + y
'23'
寫程式時,盡量使用raw_input函式11、長字串、原始字串和unicode
a、長字串如果需要寫乙個很長的字串,他需要跨多行,那麼,可以使用三個引號來代替普通引號。
例:>>> print '''abcdef
ghijkl'
mnopkrst''
uvwxyz
123456'''
輸出:
abcdef
ghijkl'
mnopkrst''
uvwxyz
普通字串也可以跨行,如果一行之中最後乙個字元為反斜線,那麼換行符本身就「轉義」了,也就被忽略了。
例:>>> print 'hello \
world'
hello world
>>> #同樣適用於表示式
>>> print 1 + 2 \
+ 4 + 5
>>> print \
'hello world'
hello world
b、原始字串原始字串不會把反斜線當成特殊字元,在原始字串中輸入的每個字串都會與書寫的方式保持一致。
例:
>>> print "c:\nabcddskf"
c:
abcddskf
>>> print r"c:\nabcdfds"
c:\nabcdfds
不能在原始字串結尾輸出反斜線。除非對最後的反斜線進行轉義。否則會出錯:
例:>>> print r"hello\"
syntaxerror: eol while scanning single-quoted string
>>> print r"hello\\"
hello\\
如果想要乙個字串是以反斜線結尾:
例:>>> print "hello" '\\'
hello\c、unicode字串
unicode字串可以表示在字元更多,在python3.0中,所有字串都是unicode字串。
例:>>> print u"hello"
hello
本章函式:
python學習之一 python安裝
python學習之一 python安裝 一 在linux系統 在linux上很可能已經自帶了python 這可能和你在安裝linux時的包的選擇有關係 測試方法是在linux環境下開啟shell然後輸入 pyton v python 2.6.6 注意這裡的命令 v的v是大寫的,我的系統中返回的是2....
python學習系列之一
python是一種容易學習而又功能強大的程式語言,是一種object oriented programming.python優雅的語法及dynamic typing,及intepretion本質。python是一種高階語言,還可以把程式切成小模組。python中也有內建的模組可以提供許多功能如fil...
Python學習筆記 一手漂亮的Python函式
使用類和函式定義模型 函式是抽象和封裝的基本方法之一 重構函式 命名合理 具有單一功能 包含文件注釋 返回乙個值 不超過 50 行 冪等函式,盡可能是純函式 函式太長 重構 refactor 冪等函式 idempotent function 可測試性 可維護性 在函式程式設計中,如果函式是冪等函式且...