[[email protected] ~]# yum groupinstall "Development Tools"
[[email protected] ~]# yum -y install zlib-devel bzip2-devel openssl-devel sqlite-devel readline-devel libffi-devel
#3.7需要安裝最後一個,3.6不需要
可以直接點選下載,也可以右鍵 選擇複製鏈接地址。
[[email protected] ~]# wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz
[[email protected] ~]# tar -xf Python-3.7.6.tar.xz
[[email protected] ~]# cd Python-3.7.6
可以選擇如下兩種方式之一
修改檔案 Python-3.7.6/Modules/Setup.dist
, 去掉如下幾行的註釋 :
readline readline.c -lreadline -ltermcap
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
shell
命令提示字元下執行如下命令:sed -ri 's/^#readline/readline/' Modules/Setup.dist
sed -ri 's/^#(SSL=)/\1/' Modules/Setup.dist
sed -ri 's/^#(_ssl)/\1/' Modules/Setup.dist
sed -ri 's/^#([\t]*-DUSE)/\1/' Modules/Setup.dist
sed -ri 's/^#([\t]*-L\$\(SSL\))/\1/' Modules/Setup.dist
[[email protected] ~]# ./configure --enable-shared
[[email protected] ~]# make -j 1 && make install
# -j 當前主機的 cpu 核心數
–enable-shared 指定安裝共用庫,共用庫在使用其他需呼叫python的軟體時會用到,比如使用
mod_wgsi
連線Apache與python時需要。
執行如下命令
[[email protected] ~]# cmd1='export LD_LIBRARY_PATH='
[[email protected] ~]# cmd2='\$LD_LIBRARY_PATH:/usr/local/lib'
[[email protected] ~]# file="/etc/profile.d/python3_lib.sh"
[[email protected] ~]# echo "${cmd1}${cmd2}" >$file
[[email protected] ~]# path="/usr/local/lib"
[[email protected] ~]# file2="/etc/ld.so.conf.d/python3.conf"
[[email protected] ~]# echo ${path} > $file2
接下來,執行如下命令使設定好的環境資訊生效
[[email protected] ~]# ldconfig
[[email protected] ~]# source /etc/profile
[[email protected] ~]# python3 -V
Python 3.7.6
[[email protected] ~]#
假如上面顯示的含有 Python3.7.6 就沒問題了
[[email protected] ~]# pip3 -V
pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
輸出的資訊中的目錄
/usr/local/lib/python3.7/site-packages/
是用於存放 安裝的第三方模組的
.pip
[[email protected] ~]# mkdir ~/.pip
[[email protected] ~]# echo '[global]' >> ~/.pip/pip.conf
[[email protected] ~]# c1="index-url=https://"
[[email protected] ~]# c2="mirrors.aliyun.com/pypi/simple"
[[email protected] ~]# echo "${c1}${c2}" >> ~/.pip/pip.conf
豆瓣源:
https://pypi.douban.com/simple/
阿裡源:https://mirrors.aliyun.com/pypi/simple
可以安裝一個增強版的直譯器 ipython
用於測試後面也會用的這個模組
[[email protected] ~]# pip3 install ipython
#pip3 install --upgrade pip'可以更新到最新版
yum groupinstall "Development Tools"
yum -y install zlib-devel bzip2-devel openssl-devel sqlite-devel readline-devel libffi-devel
wget https://www.python.org/ftp/python/3.7.6/Python-3.7.6.tar.xz
if [ $? -ne 0 ];then
echo "網路故障"
exit
fi
tar -xf Python-3.7.6.tar.xz
cd Python-3.7.6/
sed -ri 's/^#readline/readline/' Modules/Setup.dist
sed -ri 's/^#(SSL=)/\1/' Modules/Setup.dist
sed -ri 's/^#(_ssl)/\1/' Modules/Setup.dist
sed -ri 's/^#([\t]*-DUSE)/\1/' Modules/Setup.dist
sed -ri 's/^#([\t]*-L\$\(SSL\))/\1/' Modules/Setup.dist
./configure --enable-shared && make -j 2 && make install || echo "編譯失敗";exit
ldconfig
[[email protected] ~]# pip3 install ipython
[[email protected] ~]# ipython
支援 Tab 鍵補全
連續按下兩次 Tab 鍵即可
可以檢視函數的原始碼
方法:??函數名
In [5]: ??open # 按下回車即可檢視 open 函數的原始碼,輸入 q 退出原始碼
可以執行系統命令
方法: !命令
In [6]: !vi hello.py
輸入如下內容,並儲存退出
執行指令碼程式
In [8]: %run hello.py
hello
In [9]:
假設目前需要寫一個小的程式,程式的功能非常簡單,就叫猜數遊戲吧。
思考一下,如何實現?
我們可以分析一下
給提示資訊,讓其輸入一個數字
這裏會用的和使用者的互動,就是程式和使用者的互動。
python 中 使用 input
函數實現
input("這裏寫提示資訊, 必須使用引號引起來")
那使用者的輸入可以使用一個變數接收
n = input("請輸入一個數字")
>>> import keyword #匯入模組
>>> keyword.kwlist #.kwlist是一個屬性,可以列出關鍵字
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
在 python 中究竟該如何正確理解變數的賦值過程呢?
s = 'hello'
以上的變數賦值,應該說成把變數名 s
分配給 hello
這個物件更合理。
hello
這個物件會在記憶體中先被建立,之後再把變數名 s
分配給這個物件。
所以要理解 Python 中的變數賦值,應該始終先看
等號右邊
。
物件是在右邊先被建立或者被獲取
,在此之後左邊的變數名纔會被系結到物件上,這就像是爲物件貼上了一個標籤。
一個物件可以有多個標籤或者名字
。
比如: 我們自己就有很多名字,身份證上的名字,網路暱稱等。
請看下面 下麪的程式碼範例:
a = 1
b = a
a = 2
print(b) # b 會是 ?
a = 1
時如下圖:
b = a
時如下圖:
a = 2
時如下圖:
上面的 b = a
我們稱它爲 傳遞參照,此時物件會擁有兩個名稱(標籤) ,分別是 a
和 b
字串以及後面講的列表、元組都支援這種操作,也要元組解包
In [9]: n1, n2 = 1, 2
In [10]: n1
Out[10]: 1
#n1, n2 = 12,會報錯,但是給12加上引號,使其成爲字串,就可以分割
In [11]: n2
Out[11]: 2
In [75]: s1, s2 = '12'
In [76]: s1
Out[76]: '1'
In [77]: s2
Out[77]: '2'
In [78]: num, s = [10, 'hello'] # 這個是列表, 後面會講到
In [79]: num
Out[79]: 10
In [80]: s
Out[80]: 'hello'
字串可以分割
>>> n = 10
>>> n == 10 # 等於
True # 條件爲真,則返回 True
>>> n != 10 # 不等於
False # 條件爲假,則返回 False
>>> n > 10 # 大於
False
>>> n < 10 # 小於
False
>>> n >= 10 # 大於等於
True
>>> n <= 10 # 小於等於
True
>>>
在 Ipython 中來實際操作一下
In [1]: n = input("請輸入一個數字>>:")
請輸入一個數字>>:10
In [2]: n == 10
Out[2]: False
In [3]:
會發現返回 False
在程式語言中 ,數據是有型別之分的。
input()
接收到的任何數據都會成爲 字串型別(str),就是普通的字串
而 我們等號 右邊的10
是整型(int)
type
In [3]: type(n) # 檢視 input 接收到的數據型別
Out[3]: str
In [4]: type(10)
Out[4]: int
In [5]:
In [11]: type(0)
Out[11]: int
In [12]: type(-1)
Out[12]: int
In [13]: type(1)
Out[13]: int
In [17]: type(1.1)
Out[17]: float
In [18]: type(-0.1)
Out[18]: float
In [19]: type(True)
Out[19]: bool
In [20]: type(False)
Out[20]: bool
In [14]: type('10')
Out[14]: str
In [15]: type('hello')
Out[15]: str
In [16]: type('-1.1')
Out[16]: str
In [17]:
In [18]: type(b'hello')
Out[18]: bytes
我們來驗證一下 input 接收的數據的型別
In [36]: n = input("請輸入一個數字>>:")
請輸入一個數字>>:10
In [37]: n
Out[37]: '10'
In [38]: type(n)
Out[38]: str
要想把使用者的輸入(str)和整型(int)進行正確的比較大小,就需要把 字串型別的數據轉換整型。
這種把一個數據從一個型別轉換爲另外一個型別的操作叫型別裝換
In [21]: int('10')
Out[21]: 10
In [22]: int('-10')
Out[22]: -10
In [23]: int(1.9)
Out[23]: 1
In [25]: float(1)
Out[25]: 1.0
In [26]: float(-1)
Out[26]: -1.0
In [27]: float('1.1')
Out[27]: 1.1
In [28]: str(111)
Out[28]: '111'
In [29]: str(-111)
Out[29]: '-111'
In [30]: str(-11.1)
Out[30]: '-11.1'
In [31]: str(b'hello', encoding='utf-8')
Out[31]: 'hello'
二進制轉換字串的時候,需要指定字元編碼
In [32]: bytes('千鋒', encoding=('utf-8'))
Out[32]: b'\xe5\x8d\x83\xe9\x94\x8b'
In [58]: b = bytes('千鋒', encoding=('utf-8'))
In [59]: b
Out[59]: b'\xe5\x8d\x83\xe9\x94\x8b'
In [60]: str(b, encoding='utf-8')
Out[60]: '千鋒'
In [61]: s= str(b, encoding='utf-8')
In [62]: s
Out[62]: '千鋒'
In [63]: type(s)
Out[63]: str
注意字串轉二進制時候,需要指定字元編碼
判斷條件可以用在 if 判斷語句中
語法結構是這樣的:
if 判斷條件: # 冒號必須的
如果判斷條件爲真,執行這裏的程式碼,這裏的程式碼必須縮排4個空格
並且每一行程式碼的縮排要一致
範例:
In [39]: n = input("請輸入一個數字>>:")
請輸入一個數字>>:18
In [40]: n = int(n)
In [41]: if n == 18:
...: print("相等")
...:
相等
if 判斷條件:
如果判斷條件爲真,執行這裏的程式碼
else: # 這裏的冒號也是必須的
如果判斷條件爲假,執行這裏的程式碼,這裏的程式碼必須縮排4個空格
並且每一行程式碼的縮排都要一致
範例:
In [44]: if n == 10:
...: print("相等")
...: else:
...: print("不相等")
...:
不相等
if 判斷條件:
如果判斷條件新增爲真,執行這裏的程式碼,這裏的程式碼必須縮排4個空格
並且每一行程式碼的縮排要一致
elif 判斷條件: # 這裏同樣需要加條件
如果判斷條件新增爲真,執行這裏的程式碼,這裏的程式碼必須縮排4個空格
並且每一行程式碼的縮排要一致
else: # 這裏的冒號也是必須的
如果判斷條件爲假,執行這裏的程式碼,這裏的程式碼必須縮排4個空格
並且每一行程式碼的縮排都要一致
elif
根據需求可以出現多個
範例:
In [51]: n = 20
In [52]: if n == 10:
...: print("相等")
...: elif n > 10:
...: print("大了")
...: else:
...: print("小了")
...:
大了
在生產中,通常我們會把程式的程式碼寫的一個檔案種,這個檔案就成爲 Python 的一個程式檔案,檔名一般都是以 .py
爲結尾,有時候也成爲 一個 python 的程式。
使用 vi
編輯器,寫一下這個程式
#!/usr/bin/env python3
# file name hello.py
print("猜數遊戲開始")
n = input("請輸入一個數字")
n = int(n)
if n == 18:
print("猜對了")
elif n > 18:
print("大了")
else:
print("小了")
第一行不是註釋,和 shell 指令碼一樣,是在宣告這個指令碼預設使用的直譯器
執行 python 程式
[[email protected] ~]# ./hello.py
猜數遊戲開始
請輸入一個數字8
小了
語法:
while 條件表達式:
條件表達示爲真,就執行這裏的程式碼,必須縮排 4 個空格
多行程式碼保持縮排一致
條件表達式可以是:
True
# 布爾值的 True1 < 10
# 凡是在 if 語句中使用的判斷表達示,這裏都可以使用
猜數遊戲優化版本
#!/usr/bin/env python3
print("猜數遊戲開始")
while True:
n = input("請輸入一個數字")
# 如果輸入空,就重新開始新一輪的回圈
if not n:
continue
# 如果輸入 q 就是跳出回圈
if n == 'q':
break
n = int(n)
if n == 18:
print("猜對了")
elif n > 18:
print("大了")
else:
print("小了")
# 退出回圈後,程式繼續執行下面 下麪的程式碼
exit("退出程式..")
def 函數名():
"""函數的說明,主要是說明一下函數的主要功能,這是可選的"""
函數體,就是程式碼
縮排 4 個空格,多行縮排保持一致
函數名的規則和變數名的命名規則一致
呼叫方式:
函數名()
python 屬於解釋性語言,就是程式碼需要讀一行,直譯器解釋一行。
因此,函數就像是 定義一個變數,必須先定義函數,才能 纔能呼叫函數。
def foo():
print("我是函數體,只有在呼叫函數時,這裏的程式碼纔會被執行")
foo()
執行後會輸出:
我是函數體,只有在呼叫函數時,這裏的程式碼纔會被執行
那我們現在可以把之前寫的猜數遊戲,編寫函數
#!/usr/bin/env python3
def guess_number():
"""輸入一個數字,和18 比較大小"""
print("猜數遊戲開始")
while True:
n = input("請輸入一個數字")
# 如果輸入空,就重新開始新一輪的回圈
if not n:
continue
# 如果輸入 q 就是跳出回圈
if n == 'q':
break
n = int(n)
if n == 18:
print("猜對了")
elif n > 18:
print("大了")
else:
print("小了")
# 呼叫函數
guess_number()
exit("退出程式..")
擴充套件1:
猜錯三次退出
a=0
while True:
n = input("請輸入一個數字")
if not n:
continue
if n == 'q':
break
n = int(n)
if n == 20:
print("猜對了")
else:
a += 1 #a=a+1
print('錯了%d次' % a)
if a == 3:
break
擴充套件2:
輸錯3次,使用者可選擇是否繼續
思路
在上一個基礎上,加一個爲空或者輸入其他字元時,輸三次也退出
思路
輸錯三次數位時,提示是否繼續;如果輸入爲空或者其他字元滿三次時,直接continue退出回圈
#!/usr/bin/env python3
print("ready go")
n = 0
while n < 3:
i = input("輸入一個字元")
if i == 'q':
break
if not i:
n = n + 1
continue
elif not i.isdigit():
n = n + 1
continue
i = int(i)
if i == 18:
print("猜對了")
elif i < 18:
print("小了")
n = n + 1
else:
print("大了")
n = n + 1
if n == 3:
m = input("輸入一個字元y|n,y會繼續執行,n退出")
if m == "y":
n = 0
elif m == "n":
break
或者用
- 輸錯三次數位,提示是否繼續,輸空或其他字元,continue本次回圈
m = 0 while m < 3: n = input(">>:") if n == 'q': break if not (n and n.isdigit()): continue n = int(n) if n == 18: print("ok") elif n > 18: print("大了") m = m + 1 else: print("小了") m = m + 1 if m == 3: inp = input("是否繼續:") if inp == 'y': m = 0
注意:位置不一樣,執行結果不一樣
#!/usr/bin/env python3
print("ready go")
n = 0
while n < 3:
i = input("輸入一個字元")
if i == 'q':
break
if not i:
n = n + 1
if n == 3:
m = input("輸入一個字元y|n,y會繼續執行,n退出")
if m == "y":
n = 0
elif m == "n":
break
continue
elif not i.isdigit():
n = n + 1
if n == 3:
m = input("輸入一個字元y|n,y會繼續執行,n退出")
if m == "y":
n = 0
elif m == "n":
break
continue
i = int(i)
if i == 18:
print("猜對了")
elif i < 18:
print("小了")
n = n + 1
else:
print("大了")
n = n + 1
if n == 3:
m = input("輸入一個字元y|n,y會繼續執行,n退出")
if m == "y":
n = 0
elif m == "n":
break
編寫一個小程式,實現如下效果
使用者輸入一個數字,返回對應的服務名稱,加上沒有對應的服務,就返回未知的服務。
輸入 q 退出。
常用埠-->查詢程式
請輸入一個常用的服務預設埠號:80
HTTP 服務
請輸入一個常用的服務預設埠號:22
SSHD 服務
請輸入一個常用的服務預設埠號:21
FTP 服務
請輸入一個常用的服務預設埠號:3306
Mysql 服務
請輸入一個常用的服務預設埠號:9080
未知服務
請輸入一個常用的服務預設埠號:
#!/usr/bin/env python3
while Ture:
n = input("please input a port:")
if not n:
continue
elif n == 'q':
break
n = int(n)
if n == 80:
print("httpd")
elif n == 3306:
print("mysqld")
elif n == 22:
print("ftp")
else:
print("unknow")
擴充套件1:
若使用者輸入三次錯誤的埠,就退出
#!/usr/bin/env python3
i=0
while i < 3:
n = input("please input a port:")
if not n:
continue
elif n == 'q':
break
n = int(n)
if n == 80:
print("httpd")
elif n == 3306:
print("mysqld")
elif n == 22:
print("ftp")
else:
print("unknow")
i = i+1