2018.06.06
1.1為什么要學(xué)習(xí)python
學(xué)習(xí)方法:
邊看邊做不能只看不做
筆記要記錄詳細(xì)
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、烏拉特后ssl等。為超過(guò)千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的烏拉特后網(wǎng)站制作公司
python是跨平臺(tái)的語(yǔ)言,linux、windows及mac都可以使用
官網(wǎng):www.python.org
學(xué)習(xí)使用的較新版本,使用python3的
windows上的安裝過(guò)程,安裝在C:\Python36目錄下,然后點(diǎn)擊Install開(kāi)始安裝
在windows中添加python的環(huán)境變量,如下:
計(jì)算機(jī)或我的電腦右擊打開(kāi)屬性--系統(tǒng)高級(jí)設(shè)置
添加python的安裝路徑,可在用戶(hù)或系統(tǒng)變量中添加,加入后確認(rèn)即可
打開(kāi)windows的CMD,輸入python進(jìn)入python,輸入exit()退出
linux中安裝python
如果本機(jī)安裝了python2,盡量不要管他,使用python3運(yùn)行python腳本就好,因?yàn)榭赡苡谐绦蛞蕾?lài)目前的python2環(huán)境
比如yum!?。。?!教訓(xùn)- -刪除了python2安裝目錄,導(dǎo)致yum不能使用,由于懶得解決,不去修復(fù)yum,廢了一個(gè)虛擬機(jī)
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
sudo yum -y install gcc gcc-c++
sudo yum -y install zlib
sudo yum -y install libffi-devel
2.下載Python3,官網(wǎng)https://www.python.org/downloads/
3.安裝python3
我個(gè)人習(xí)慣安裝在/usr/local/python3(具體安裝位置看個(gè)人喜好)
創(chuàng)建目錄:
mkdir -p /usr/local/python3
解壓下載好的Python-3.x.x.tgz包(具體包名因你下載的Python具體版本不不同?而不不同,如:我下載的是
Python3.6.1.那我這里就是Python-3.6.1.tgz)
tar -zxvf Python-3.6.1.tgz
4.進(jìn)入解壓后的目錄,編譯安裝。
cd Python-3.6.1
./configure --prefix=/usr/local/python3
編譯安裝后提示執(zhí)行./configure --enable-optimizations,執(zhí)行構(gòu)建穩(wěn)定優(yōu)化的環(huán)境
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations
[root@localhost Python-3.7.0]# ./configure --enable-optimizations
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.7... no
checking for python3... no
checking for python... python
make之前編譯安裝pip和ipython
在pypi.python.org官網(wǎng)下載setuptools庫(kù)和pip
安裝setuptools
tar -zxvf setuptools-*.*.tar.gz
cd setuptools-*.*
python3 setup.py build
python3 setup.py install
報(bào)錯(cuò): RuntimeError: Compression requires the (missing) zlib module
我們需要在linux中安裝zlib-devel包,進(jìn)行支持。
yum install zlib-devel
需要對(duì)python3.* 進(jìn)行重新編譯安裝。
cd python3.*.1
make && make install
又是漫長(zhǎng)的編譯安裝過(guò)程。
重新安裝setuptool
python3 setup.py build
python3 setup.py install
安裝pip的支持?jǐn)U展
cd pip-*.0.*
python3 setup.py build
python3 setup.py install
如果沒(méi)有意外的話(huà),pip安裝完成。
如果沒(méi)有搞好環(huán)境的話(huà),會(huì)碰見(jiàn)親切的報(bào)錯(cuò):
pip3 install paramiko
報(bào)這個(gè)錯(cuò)
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
然后開(kāi)始進(jìn)行如下操作
yum install openssl
yum install openssl-devel
make install 或者 make && make install 進(jìn)行最后安裝
Looking in links: /tmp/tmptbpyw1pv
Requirement already up-to-date: setuptools in /usr/local/lib/python3.7/site-packages (39.0.1)
Requirement already up-to-date: pip in /usr/local/lib/python3.7/site-packages (10.0.1)
5.建立python3命令的軟鏈接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
6.并將/usr/local/python3/bin加入PATH
# vim /etc/profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH
按ESC,輸入:wq回車(chē)退出。
修改完記得執(zhí)行行下面的命令,讓上一步的修改生效:
[root@localhost Python-3.7.0]# source /etc/profile
[root@localhost Python-3.7.0]# python3
Python 3.7.0 (default, Jul 4 2018, 14:06:09)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
重新讀取系統(tǒng)環(huán)境配置變量后能夠正常使用python3來(lái)進(jìn)入python終端環(huán)境了
安裝python3.7的pip和ipython工具
升級(jí)pip
pip3 install --upgrade pip
安裝ipython,可以使用國(guó)內(nèi)源
python3 -m easy_install -i 國(guó)內(nèi)源/simple/地址 ipython
安裝系統(tǒng)python2默認(rèn)ipython命令工具
python支持補(bǔ)全,需要安裝ipython的軟件包,需要安裝python-pip,安裝python-pip需要epel擴(kuò)展
yum install -y epel-release 安裝擴(kuò)展
yum install -y python-pip 安裝python-pip
[root@localhost ~]# pip install ipython
Collecting ipython
Downloading https://files.pythonhosted.org/packages/ee/01/2a85cd07f5a43fa2e86d60001c213647252662d44a0c2e3d69471a058f1b/ipython-6.4.0.tar.gz (5.1MB)
100% |████████████████████████████████| 5.1MB 101kB/s
Complete output from command python setup.py egg_info:
IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2.
When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
Beginning with IPython 6.0, Python 3.3 and above is required.
See IPython `README.rst` file for more information:
https://github.com/ipython/ipython/blob/master/README.rst
Python sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0) detected.
Your pip version is out of date, please install pip >= 9.0.1. pip 8.1.2 detected.
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-dXTgRG/ipython/
You are using pip version 8.1.2, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.安裝后提示有新版本可升級(jí)
[root@localhost ~]# pip install --upgrade pip 執(zhí)行pip install --upgrade pip升級(jí)pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 180kB/s
Installing collected packages: pip
Found existing installation: pip 8.1.2
Uninstalling pip-8.1.2:
Successfully uninstalled pip-8.1.2
Successfully installed pip-10.0.1
安裝pip install ipython
安裝中報(bào)錯(cuò)
[root@localhost ~]# pip install ipython
Collecting ipython
Using cached https://files.pythonhosted.org/packages/52/19/aadde98d6bde1667d0bf431fb2d22451f880aaa373e0a241c7e7cb5815a0/ipython-5.7.0-py2-none-any.whl
Requirement already satisfied: pygments in /usr/lib64/python2.7/site-packages (from ipython) (2.2.0)
Requirement already satisfied: decorator in /usr/lib/python2.7/site-packages (from ipython) (3.4.0)
Requirement already satisfied: backports.shutil-get-terminal-size; python_version == "2.7" in /usr/lib/python2.7/site-packages (from ipython) (1.0.0)
............
----------------------------------------
Command "/usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-aBmrn9/scandir/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-hoCRRX/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-aBmrn9/scandir/
根據(jù)視頻使用pip install ipython==3.2.3找到這個(gè)版本進(jìn)行安裝
[root@localhost ~]# pip install ipython==3.2.3
Collecting ipython==3.2.3
Downloading https://files.pythonhosted.org/packages/a8/be/4903b69c5dfb06e4537adbe4b5497048d303258c6d439466a648a0a05be9/ipython-3.2.3-py2-none-any.whl (3.4MB)
100% |████████████████████████████████| 3.4MB 77kB/s
Installing collected packages: ipython
Successfully installed ipython-3.2.3
[root@localhost ~]# pip list
Package Version
---------------------------------- -------
backports.shutil-get-terminal-size 1.0.0
backports.ssl-match-hostname 3.5.0.1
configobj 4.7.2
decorator 3.4.0
enum34 1.1.6
iniparse 0.4
ipaddress 1.0.16
ipython 3.2.3
ipython-genutils 0.2.0
perf 0.1
pip 10.0.1
pycurl 7.19.0
Pygments 2.2.0
pygobject 3.14.0
pygpgme 0.3
pip list查看安裝好的插件
使用ipython進(jìn)入python環(huán)境使用
[root@localhost ~]# ipython
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
Type "copyright", "credits" or "license" for more information.
IPython 3.2.3 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: print "holle world" 雙擊tab鍵補(bǔ)全
holle world
In [2]:exit()
[root@localhost ~]#
安裝這里我也搞的很迷糊,因?yàn)榈谝淮伟惭b完使用ipython發(fā)現(xiàn)python版本是2.7的,不是我想要的3版本,于是自己動(dòng)手安裝,碰見(jiàn)不少坑文檔記錄的也不是很全,僅供參考
使用方式:一、進(jìn)入命令行交互模式,二、創(chuàng)建文件,在文件中寫(xiě)入語(yǔ)句
文件中寫(xiě)入:
[root@localhost src]# vim python.py
#!/usr/bin/python 使用python的命令解釋器
print 'holle world'
~
~
~
[root@localhost src]# python python.py
holle world
字節(jié)代碼,通過(guò)compile模塊編譯后生成的后綴名為pyc的文件
[root@localhost src]# cat 1.py
#!/usr/bin/python
import py_compile
py_compile.compile('2.py') 2.py是同目錄下的另一個(gè)文件
[root@localhost src]# python 1.py
[root@localhost src]# ls
1.py 2.py 2.pyc
cat查看或編輯2.pyc文件,會(huì)顯示一些亂碼,使源碼不可見(jiàn)
[root@localhost src]# python 2.pyc
holle world
[root@localhost src]# python -O -m py_compile 2.py 字節(jié)代碼pyo
[root@localhost src]# ls
1.py 2.py 2.pyc 2.pyo
同2.pyc文件一樣,cat查看或編輯2.pyo文件,會(huì)顯示一些亂碼,使源碼不可見(jiàn)
[root@localhost src]# python 2.pyo
holle world
變量是計(jì)算機(jī)內(nèi)存中的一個(gè)存儲(chǔ)數(shù)值,變量可以存儲(chǔ)規(guī)定范圍內(nèi)的值,且值可以改變
python下變量是對(duì)一個(gè)數(shù)據(jù)的引用
變量命名規(guī)則
以字母、數(shù)字、下劃線(xiàn)組合
不能以數(shù)字開(kāi)頭
不可以使用關(guān)鍵字
變量的賦值
a = 1
id (a) id是查看變量在內(nèi)存中的地址
python運(yùn)算符
賦值、算術(shù)、關(guān)系、邏輯運(yùn)算符
表達(dá)式是將不同數(shù)據(jù)(包括變量、函數(shù))用運(yùn)算復(fù)航按一定規(guī)則連接起來(lái)的一種式子
python變量賦值
In [1]: a = 123
In [2]: id(a)
Out[2]: 12392528
In [3]: a = 456
In [4]: id(a)
Out[4]: 22193432
In [5]: x = 'abc'
In [6]: x = abc 對(duì)字符串賦值要使用引號(hào)聲明,否則會(huì)將這個(gè)abc識(shí)別為一個(gè)變量
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
in ()
----> 1 x = abc
NameError: name 'abc' is not defined
In [7]:
type (變量) 查看變量的類(lèi)型,python會(huì)對(duì)數(shù)值自動(dòng)歸類(lèi)類(lèi)型
In [26]: x = 2
In [27]: type (x)
Out[27]: int
In [28]: x = '2'
In [29]: type (x)
Out[29]: str
In [30]:
python賦值運(yùn)算符
= : x = 3 , y = 'abcd'
+= : x += 2
-= : x -= 2
*= : x *= 2
/= : x /= 2
%= : x %= 2
python算術(shù)運(yùn)算符+ 加運(yùn)算 - 減運(yùn)算 * 乘運(yùn)算 / 除運(yùn)算,除數(shù)加上小數(shù)點(diǎn)可以取得一個(gè)精確的浮點(diǎn)結(jié)果 // 整除 % 除運(yùn)算取余
** 運(yùn)算乘方
In [30]: 3 + 4 加運(yùn)算
Out[30]: 7
In [31]: 5 - 3 減運(yùn)算
Out[31]: 2
In [32]: 3 * 4 乘運(yùn)算
Out[32]: 12
In [33]: 4 / 3 除運(yùn)算
Out[33]: 1
In [34]: 4.0 / 3 除運(yùn)算,除數(shù)加上小數(shù)點(diǎn)可以取得一個(gè)精確的浮點(diǎn)結(jié)果
Out[34]: 1.3333333333333333
In [35]: 10 // 3 整除
Out[35]: 3
In [36]: 10 % 3 除運(yùn)算只取余數(shù)
Out[36]: 1
In [37]: 2 ** 3 乘方運(yùn)算,對(duì)結(jié)果反復(fù)乘以3,這里乘運(yùn)算了2次,乘公式為:2*2*2
Out[37]: 8
關(guān)系運(yùn)算符,返回布爾值false 和true結(jié)果。true成立,false不成立
大于
< 小于
= 大于或等于
<= 小于或等于
== 等于
!= 不等于In [38]: 1 > 2 1大于2 Out[38]: False
In [39]: 1 < 2 1小于2
Out[39]: True
In [40]: 1 >= 2 1大于或等于2
Out[40]: False
In [41]: 1 >= 1 1大于或等于1
Out[41]: True
In [42]: 1 <= 2 1小于或等于2
Out[42]: True
In [43]: 1 <= 1 1小于或等于1
Out[43]: True
In [44]: 1 == 1 1等于1
Out[44]: True
In [45]: 1 != 2 1不等于2
Out[45]: True
邏輯運(yùn)算符
and 邏輯與: true and false 兩個(gè)表達(dá)式都成立返回true,有一個(gè)不成立就返回false
or 邏輯或 : false and true 兩個(gè)表達(dá)式只要有一個(gè)成立就返回true
not 邏輯非 : not true 表達(dá)式不成立返回true
運(yùn)算符排列,從下到上優(yōu)先級(jí)越來(lái)越高,一行中越往右優(yōu)先級(jí)越高
![](/upload/otherpic64/39c7eb695db492a4ff9634ca58b5c470.png)
從鍵盤(pán)輸入讀取數(shù)值
input("Please input:")
一個(gè)小的計(jì)算腳本示例:
[root@localhost src]# cat jisuan.py
#!/usr/bin/python
num1 = input("Please a number: ")
num2 = input("Please a number: ")
print "%s + %s = %s" % (num1, num2, num1+num2)
print "%s - %s = %s" % (num1, num2, num1-num2)
print "%s %s = %s" % (num1, num2, num1num2)
print "%s / %s = %s" % (num1, num2, num1/num2)
[root@localhost src]# python jisuan.py
Please a number: 3
Please a number: 4
3 + 4 = 7
3 - 4 = -1
3 * 4 = 12
3 / 4 = 0
1.5 Python的數(shù)值和字串符
python數(shù)據(jù)類(lèi)型
數(shù)值 字串符 列表 元組 字典
數(shù)值類(lèi)型又分為整型 長(zhǎng)整型 浮點(diǎn)型 復(fù)數(shù)型
整數(shù)類(lèi)型:
In [1]: a = 3
In [2]: type(a)
Out[2]: int
整數(shù)類(lèi)型范圍 -2147483648 到 2147483648
長(zhǎng)整型:
In [4]: a = 99999999999999999999999999999999999
In [5]: type (a)
Out[5]: long
浮點(diǎn)數(shù)類(lèi)型:
In [6]: 3e+6
Out[6]: 3000000.0
In [7]: type(3e+6)
Out[7]: float
In [8]: 3.0/2
Out[8]: 1.5
In [9]: type(3.0/2)
Out[9]: float
復(fù)數(shù)類(lèi)型:
In [10]: a = 34j
In [11]: type(a)
Out[11]: complex
字符串類(lèi)型,三種引號(hào)模式:
a = 'abc'
a = "abc"
a = '''abc'''
三引號(hào)有表示注釋的意思,也可以代替\n的換行符
In [18]: a = '''holle
....: wrold'''
In [19]: print a
holle
wrold
索引與切片操作,定義a 的變量為123456,操作取出其中某些單獨(dú)的數(shù)字組合
In [20]: a = '123456'
In [21]: a
Out[21]: '123456'
In [22]: a[1] 從0位開(kāi)始取值,0是數(shù)值1,1是數(shù)值2,以此類(lèi)推
Out[22]: '2'
In [23]: a[0]
Out[23]: '1'
In [24]: a[5]
Out[24]: '6'
In [25]: a[0]+a[3]
Out[25]: '14'
In [26]: a[0:4] 整數(shù)取值跟字符串有些不同
Out[26]: '1234'
字符串引索
In [27]: b = 'abcde'
In [28]: b [0:2] 字符串引索時(shí)0表示開(kāi)始時(shí)的值,引索到結(jié)束時(shí)的值需要+1,如取出abc兩個(gè)字符表示[0:3]
Out[28]: 'ab'
In [29]: b [-1] -1表示字符串中最后一個(gè)值,這里是e
Out[29]: 'e'
In [30]: b [:3] :表示省去左邊的引索匹配,后面跟整數(shù)表示取出多少個(gè)字符串,不寫(xiě)整數(shù)表示取值所有
Out[30]: 'abc'
In [31]: b [1:] :冒號(hào)左邊跟整數(shù)表示省略左邊的多少個(gè)字符,右邊不跟整數(shù)表示取出所有,正整數(shù)和負(fù)整數(shù)表達(dá)的結(jié)果基本一致。如冒號(hào)右邊的-1和1的取出結(jié)果都是bcd
Out[31]: 'bcde'
In [32]: b [1:-1] :冒號(hào)左邊跟整數(shù)表示省略左邊的多少個(gè)字符,右邊不跟負(fù)整數(shù)表示取出所有,這里負(fù)整數(shù)倒敘,這里最大取值-1,如果想得出bcde結(jié)果,則后面不跟任何數(shù)值
Out[32]: 'bcd'
In [36]: b[-4:-2]
Out[36]: 'bc'
In [37]: b[-4:-1]
Out[37]: 'bcd'
In [38]: b[-3:-5:-1] -3從右往左數(shù)實(shí)際上的第二個(gè),-5從右往左數(shù)實(shí)際上的第三個(gè),-1表示從右到左的排序 ,倒敘位置 ed[c][b]a
Out[38]: 'cb'
In [52]: b[::2] 雙冒號(hào)表示步長(zhǎng)值,每隔兩步取一個(gè)值
Out[52]: 'ace'
In [53]: b[::-2] 倒敘的從右往左取值,每隔兩步取出一個(gè)字符串
Out[53]: 'eca'
整理很亂,基本上都是操作一遍的結(jié)果,按操作實(shí)踐下理解一下其中意思