[root@kaibin ~]# ipython
In [1]: import platform
In [2]: print platform.uname()
('Linux', 'kaibin.test1', '2.6.32-431.el6.x86_64', '#1 SMP Fri Nov 22 03:15:09 UTC 2013', 'x86_64', 'x86_64')
In [3]: dir(platform) #檢視platform支援的功能:dir(platform)
In [4]: exit()
簡單方法:
1.編譯安裝新版本至某特定路徑:
2.pyenv
源碼包安裝python:
[root@kaibin ~]# ll Python-2.7.6.tgz
-rw-r--r--. 1 root root 14725931 1月 25 2015 Python-2.7.6.tgz
[root@kaibin ~]# tar xvf Python-2.7.6.tgz
依賴readlinde-devel開發程式
[root@kaibin ~]# yum -y install readline-devel
[root@kaibin ~]# cd Python-2.7.6
[root@kaibin Python-2.7.6]# ls
config.guess configure.ac Grammar Lib Makefile.pre.in Objects PCbuild README Tools
config.sub Demo Include LICENSE Misc Parser pyconfig.h.in RISCOS
configure Doc install-sh Mac Modules PC Python setup.py
[root@kaibin Python-2.7.6]# ./configure --prefix=/usr/local/python-27
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking for --enable-universalsdk... no
checking for --with-universal-archs... 32-bit
checking MACHDEP... linux2
checking EXTRAPLATDIR...
checking for --without-gcc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/root/Python-2.7.6':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
需要安裝 gcc
[root@kaibin Python-2.7.6]# yum -y install gcc
[root@kaibin Python-2.7.6]# ./configure --prefix=/usr/local/python-27
[root@kaibin Python-2.7.6]# make && make install
Tab補全功能:(為ipython功能子產品
[root@kaibin ~]# wget http://archive.ipython.org/release/1.2.1/ipython-1.2.1.tar.gz
[root@kaibin ~]# tar xf ipython-1.2.1.tar.gz
[root@kaibin ~]# cd ipython-1.2.1
[root@kaibin ipython-1.2.1]# /usr/local/python-27/bin/python2.7 setup.py build
[root@kaibin ipython-1.2.1]# /usr/local/python-27/bin/python2.7 setup.py install
[root@kaibin ipython-1.2.1]# ln -sv /usr/local/python-27/bin/python2.7 /usr/bin/python2.7
"/usr/bin/python2.7" -> "/usr/local/python-27/bin/python2.7"
[root@kaibin ipython-1.2.1]# ln -sv /usr/local/python-27/bin/ipython /usr/bin/ipython
"/usr/bin/ipython" -> "/usr/local/python-27/bin/ipython"
資料結構
通過某種方式(例如對元素進行編号)組織在一起的資料元素的集合,這些元素可以是數字或者字元,甚至可以是其他的資料結構
Python最基本的資料結構是序列
序列中的每個元素被配置設定一個序号--即元素的位置,也成為索引:索引從0開始編号
Python包含6種内建的資料序列:清單,元組,數字串
Unicode字元串,buffer對象和xrange對象
Python的關鍵要素
1.基本資料類型
2.對象引用
3.組合資料類型
4.邏輯操作符
5.控制流語句
6.算數操作符
7.輸入/輸出
8.函數的建立與調用
1. 基本資料類型
變量指派:
數字:
In [8]: num=1
In [9]: id(num)
Out[9]: 32365672
In [10]: num=2
In [11]: id(num)
Out[11]: 32365648
字母:
In [12]: name="Jerry"
In [15]: print name[0]
J
In [13]: print name[1]
e
In [14]: print name[2]
r
In [16]: id(name)
Out[16]: 43542256
檢視變量類型:
In [17]: type(num)
Out[17]: int
In [18]: type(name)
Out[18]: str
#檢視platform支援的功能:dir(platform)
In [3]: dir(platform)
In [1]: name="Jack" #字元串是不可變量
In [2]: id(name)
Out[2]: 41585664
In [3]: iname="Jack"
In [4]: id(iname)
Out[4]: 41585664
變量規則
*隻能包含字母,數字,和下劃線,且不能以數字開頭
*區分字母大小寫
*禁止使用保留字(系統中的關鍵字)
Python2和Python3的保留字有所不同
命名慣例
*以單一下劃線開頭的變量名(_x)不會被from module import*語句導入
*前後有雙下劃線的變量名(__x__)是系統定義的變量名,對Python解釋器有特殊的意義
*以兩個下劃線開頭但結尾沒有下劃線的變量名(__x)是類的本地變量
*互動模式下,變量名"_"用于儲存最後表達式的結果
In [1]: 1+1
Out[1]: 2
In [2]: print _
注意:變量名沒有類型,對象才有
In [7]: name="Jack"
In [8]: type(name)
Out[8]: str
In [9]: name=3.14
In [10]: type(name)
Out[10]: float
通過某種方式(例如對元素進行編号)組織在一起的資料元素的集合
Python常用的資料類型
序列類型
*清單:使用[]建立
In [11]: a1=["This","is","a","Pig"]
In [12]: a1[0] #引用對象
Out[12]: 'This'
In [13]: a1[0][0]
Out[13]: 'T'
*元組:使用()建立,如('one','two')
In [14]: t1=("This","is")
In [15]: t1[0]
Out[15]: 'This'
In [16]: t1[1]
Out[16]: 'is'
In [17]: t1[0][0]
Out[17]: 'T'
清單和元組的差別
In [11]: a1=["This","is","a","Pig"]
In [18]: print a1
['This', 'is', 'a', 'Pig']
In [19]: a1[3]="sheep"
In [20]: print a1
['This', 'is', 'a', 'sheep']
In [21]: id(a1)
Out[21]: 45304216
In [22]: a1[3]="Jack"
In [23]: id(a1)
Out[23]: 45304216 #前後記憶體空間位置一緻
In [14]: t1=("This","is")
In [24]: print t1
('This', 'is')
In [28]: t1[1]="what" #嘗試修改,但是報錯,無法修改
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-28-367d778d7cb3> in <module>()
----> 1 t1[1]="what"
TypeError: 'tuple' object does not support item assignment
*字元串也屬于序列類型
In [29]: name="Jerry"
In [30]: name[0]
Out[30]: 'J'
In [31]: name[0:1]
Out[31]: 'J'
In [32]: name[0:2]
Out[32]: 'Je'
In [33]: name[:2]
Out[33]: 'Je'
In [34]: name[2:]
Out[34]: 'rry'
In [35]: name[0:4]
Out[35]: 'Jerr'
In [36]: name[0:4:2]
Out[36]: 'Jr'
集合類型
集合
映射
字典
字典是可變對象,元組是不可變序列
Python中,組合資料類型也是對象,是以其可以嵌套
實質上,清單和元組并不是真正存儲資料,而是存放對象引用
Python對象可以具有其可以被調用的特定"(方法函數)"
元組,清單以及字元串等資料類型是"有大小的",其長度可使用内置函數len()測量
In [37]: print name
Jerry
In [38]: len(name) #擷取元素個數
Out[38]: 5
In [39]: print a1
['This', 'is', 'a', 'Jack']
In [40]: len(a1)
Out[40]: 4
*邏輯運算時任何程式設計語言的基本功能
*Python提供了4組邏輯運算
*身份操作符
is:判定左端對象引用是否相同于右端對象引用;也可以與None進行;
In [7]: name="Hello"
In [8]: name1="Hi"
In [9]: name is name1
Out[9]: False
In [10]: name="Hello"
In [11]: name1="Hello"
In [12]: name is name1
Out[12]: True
In [13]: type(name) is type(name1)
Out[13]: True
*比較操作符
<,>,<=,>=,!=,==
*成員操作符
in或not in:測試成員關系
*邏輯運算符
an,or,not
*控制流語句是過程式程式設計語句的基本控制機制
*python的常見控制流語句
if
while
for...in
try
*Python提供了完整的算數操作符集
*很多的Python資料類型也可以使用增強型的指派操作符,如+=,-=
*同樣的功能使用增強型的指派操作符的性能較好
*Python的int類型是不可變的,是以,增強型指派的實際過程是建立了一個新的對象來存儲結果後将變量名執行了重新綁定
7.輸入輸出
*現實中,具有實際功能的程式必須能夠讀取輸入(如從鍵盤或檔案中),以及産生輸出,并寫到終端檔案中;
*Python的輸入/輸出
輸出
Python3:print()函數
Python2: print語句
輸入
input()
raw_input()
In [14]: raw_input("please input a num:")
please input a num:4
Out[14]: '4'
In [16]: a=raw_input("please input a num:")
please input a num:b
In [18]: print a
b
*Python解釋器提供了3種标準檔案對象,分别是标準輸入,标準輸出和标準錯誤,他們在sys子產品中分别以sys.stdin,sys.stdout,和sys.stderr形式提供
*Python的print語句實作列印一一一個對程式友好的标準輸入流接口
*從技術角度來講,print是把一個多或者多個對象轉換為其文本表達形式,然後發送給标準輸入或者另一個類似檔案的流
在Python中,列印與檔案和流的概念聯系緊密
檔案寫入方法是把字元串寫入到任意檔案
print預設把對象列印到stdout流,并添加了一定的格式化
實質上,print語句隻是Python的人性化特征的具體實作,他提供了sys.stdout.write()的簡單接 口,再加上一些預設的格式設定
print結構一個逗号分隔的對象清單,并為行尾自動添加一個換行符,如果不需要,則在最後個元素後添加逗号
In [19]: a="Jack"
In [20]: b="Tom"
In [21]: print a,b
Jack Tom
print的格式化輸出:
In [23]: num=7.9
In [24]: print "The num is %f" % num
The num is 7.900000
In [25]: print "The num is %d" % num
The num is 7
In [26]: num1=9.13
In [27]: print "The nums are %d and %f" %(num,num1)
The nums are 7 and 9.130000
In [28]: print "The nums are %d and %f" %(num,3.1)
The nums are 7 and 3.100000
In [29]: print "The nums are %e and %f" %(num,3.1)
The nums are 7.900000e+00 and 3.100000
資料類型的轉換:
In [23]: num=7.9
In [30]: name="hello"
In [31]: print "The name is %s." %name
The name is hello.
In [32]: print "The name is %s." %num
The name is 7.9.
In [33]: test1=str(num)
In [34]: type(test1)
Out[34]: str
In [35]: type(num)
Out[35]: float
檢視常用的内置函數(由内建函數引用)
In [39]: dir(__builtins__)
Out[38]:
['ArithmeticError',
'AssertionError',
'AttributeError',
'BaseException',
'BufferError',
'BytesWarning',
'DeprecationWarning',
.....
檢視函數用法
In [39]: help(str)
In [40]: name="jack"
In [41]: name.upper()
Out[41]: 'JACK'
In [42]: print "The nums are %e and %f" %(num,3.1)
The nums are 7.900000e+00 and 3.100000
In [43]: print "The nums are %+e and %f" %(num,3.1)
The nums are +7.900000e+00 and 3.100000
In [44]: print "The nums are %+e and %f" %(num,-3.1)
The nums are +7.900000e+00 and -3.100000
In [45]: print "The nums are %f and %f" %(num,-3.1)
The nums are 7.900000 and -3.100000
In [46]: print "The nums are %+f and %+f" %(num,-3.1)
The nums are +7.900000 and -3.100000
In [48]: print "The nums are %+20f and %+f" %(num,-3.1)
The nums are +7.900000 and -3.100000
In [50]: print "The nums are %-20f and %+f" %(num,-3.1)
The nums are 7.900000 and -3.100000
In [5]: print "The nums are %+20.10f and %+f" %(num,-3.1)
The nums are +7.9000000000 and -3.100000
In [7]: print "The nums are %+20.15f and %+f" %(num,-3.1)
The nums are +7.900000000000000 and -3.100000
字典:鍵值的集合(kv集合)
In [9]: d1={'a':31, 'b':68}
In [10]: d1['a']
Out[10]: 31
In [11]: d1={1:31, 2:68}
In [12]: d1[1],d1[2]
Out[12]: (31, 68)
In [8]: d1={'a':31,'b':66}
In [9]: print "The float is %(a)f." %d1
The float is 31.000000.
In [10]: d2={0:31,1:66}
In [11]: d2[0]
Out[11]: 31
In [12]: print "The float is %(0)f." %d2 #嘗試用數字元素,但是失敗
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-12-f157136aebc0> in <module>()
----> 1 print "The float is %(0)f." %d2
KeyError: '0'
In [14]: def printName(name): #自定義函數printName
....: print name
....:
In [15]: printName("Jack") #調用printName函數,傳參"Jack"
Jack
In [16]: test="Hello"
In [17]: printName(test)
Hello
In [19]: callable(printName) #檢視函數是否能夠調用
Out[19]: True
In [21]: dir(__builtin__) #檢視可用内置函數
Out[21]:
['ArithmeticError',
'AssertionError',
'AttributeError',
'BaseException',
'BufferError',
'BytesWarning',
'DeprecationWarning',
'EOFError',
'Ellipsis',
......
In [23]: help(range) #檢視range的用法
In [24]: range(10)
Out[24]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [27]: range(1,11)
Out[27]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [28]: range(2,11,2)
Out[28]: [2, 4, 6, 8, 10]
In [29]: import random #加載random子產品
In [30]: random.random() #随機生成一個随機數
Out[30]: 0.10932424859218137
In [32]: students=['a','b','c','d'] #定義一個清單
In [33]: random.choice(students) #從中随機選取一個參數
Out[33]: 'b'
In [34]: random.choice(students)
Out[34]: 'c'