1、python 簡介 各種網站都有關於 Python 的簡介 Python is powerful... and fast; plays well with others; runs everywhere; is friendly & easy to learn; is Open. 2、pytho ...
1、python 簡介
各種網站都有關於 Python 的簡介
Python is powerful... and fast;
plays well with others;
runs everywhere;
is friendly & easy to learn;
is Open.
2、python 版本區別
Short version: Python 2.x is legacy, Python 3.x is the present and future of the language
Python 2.7 是一個相容版本,but Python 2.7 would be supported until 2020
print 用法不同:
# python 2.x print "Hello World!" print >>sys.stderr, "fatal error"
# python 3.x print("Hello World!") print("fatal error", file=sys.stderr) (first,*middle,last) = range(10)
Python 3.x 預設支持 Unicode,Python 2.x 預設支持 ASCII
Python 2.x 需要支持中文時:
# /usr/bin/env python # -*- coding: utf-8 -*- print "您好!"
Python 3.x 預設支持中文:
# /usr/bin/env python print("您好!")
Python 3.x 某些庫的名稱變更
Old Name |
New Name |
_winreg |
winreg |
ConfigParser |
configparser |
copy_reg |
copyreg |
Queue |
queue |
SocketServer |
socketserver |
markupbase |
_markupbase |
repr |
reprlib |
test.test_support |
test.support |
so,後面的學習選擇 python 3.x 版本
3、安裝
-
Windows 下安裝
-
下載安裝包
https:
/
/
www.python.org
/
downloads
/
-
安裝
預設安裝路徑: C:\Program Files\Python35
-
添加環境變數
右鍵電腦 --> 屬性 --> 高級系統設置 --> 高級 --> 環境變數 --> path --> 添加路徑
如:添加 ;C:\Program Files\Python35;C:\Program Files\Python35\Scripts
-
Linux 下安裝
-
- 下載安裝包
-
- 安裝
root@localhost tmp]# tar zxf Python-3.5.2.tgz [root@localhost tmp]# cd Python-3.5.2 # 預設安裝在 /usr/loca/ 目錄下 [root@localhost Python-3.5.2]# ./configure [root@localhost Python-3.5.2]# make && make install [root@localhost Python-3.5.2]# python3.5 Python 3.5.2 (default, Aug 13 2016, 19:07:36) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
4、第一個程式
在 Linux 的目錄中創建 hello.py 文件,並賦予許可權
[root@localhost learning]# cat hello.py #!/usr/bin/env python print("Hello World!") [root@localhost learning]# chmod +x hello.py [root@localhost learning]# ./hello.py Hello World!
也可以在解釋器中執行
[root@localhost learning]# python Python 3.5.2 (default, Aug 13 2016, 19:07:36) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print("Hello World!") Hello World! >>>
5、變數
Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.
-
變數命名的規則
-
變數名只能是大小寫字母,數字以及下劃線的組合
-
變數名不能以數字開頭
-
變數名不能為以下關鍵字
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
-
變數的聲明與賦值
聲明一個變數名為 name,變數 name 的值為 wenchong
>>> name = "wenchong" >>> print(name) wenchong >>> # 當變數 name1 賦值為 name 時,是將變數 name 的值(即記憶體中的值)賦值給 name1,當 name 再次變化時,不會影響 name1 的值 >>> name1 = name >>> id(name) 139659587567344 >>> id(name1) 139659587567344 >>> name = "Jack" >>> id(name) 139659587549592 >>> id(name1) 139659587567344 >>> print(name,name1) Jack wenchong
6、用戶輸入
1 #!/usr/bin/env python 2 # Get User Input String 3 name = input("Please Input Name:") 4 """ 5 Python 2.x 6 name = raw_input("Please Input Name:") 7 """ 8 print(name)
第1行:指定解釋器
第2行:單行註釋
第3-7行:多行註釋
第8行:輸入用戶輸入的內容
當輸入密碼時,希望輸入的密碼不可見,該模塊在 windows 的 pycharm 中不生效
#!/usr/bin/env python import getpass passwd = getpass.getpass("Please Input Passwd: ") print(passwd) [root@localhost learning]# python hello.py Please Input Passwd: password [root@localhost learning]#
7、模塊
在捕獲輸入密碼的 python 程式中,其中一行為 import getpass,即為導入模塊
-
os 模塊
os.mkdir() 創建目錄
os.system() 執行系統命令,命令結果無法賦值給變數,只能講返回碼賦值給變數
os.popen() 執行系統命令,命令結果可以賦值給變數
# 導入模塊 os >>> import os >>> os.mkdir("testDir") >>> os.system("ls -l") total 4 -rwxr-xr-x 1 root root 238 Aug 13 20:17 hello.py drwxr-xr-x 2 root root 6 Aug 13 20:23 testDir 0 >>> command_res = os.system("ls -l") total 4 -rwxr-xr-x 1 root root 238 Aug 13 20:17 hello.py drwxr-xr-x 2 root root 6 Aug 13 20:23 testDir >>> print(command_res) 0 >>> command_res = os.popen("ls -l").read() >>> print(command_res) total 4 -rwxr-xr-x 1 root root 238 Aug 13 20:17 hello.py drwxr-xr-x 2 root root 6 Aug 13 20:23 testDir
-
sys 模塊
sys.path python 解釋器查找模塊的路徑列表
>>> import sys >>> print(sys.path) ['', '/usr/local/lib/python35.zip', '/usr/local/lib/python3.5', '/usr/local/lib/python3.5/plat-linux', '/usr/local/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/site-packages'] >>>
-
編寫模塊
python 的所有文件都可以當做模塊使用
[root@localhost learning]# cat mymodel.py #!/usr/bin/env python myname = "Test Model" [root@localhost learning]# python Python 3.5.2 (default, Aug 13 2016, 19:07:36) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import mymodel >>> print(mymodel.myname) Test Model >>>
8、流程式控制制
-
場景1:用戶密碼驗證
#!/usr/bin/env python import getpass username = "wenchong" passwprd = "pwd123" input_username = input("請輸入用戶名: ") input_password = getpass.getpass("請輸入密碼: ") if username == input_username and password == input_password: print("歡迎登陸到天堂...") else: print("無效的用戶名或密碼")
-
場景2:猜數字小游戲
#!/usr/bin/env python number = 20 input_num = int( input("請猜一個數字: ") ) if input_num == number: print("恭喜您,猜對了") elif input_num > number: print("請猜一個更小的數字") else: print("請猜一個更大的數字")
9、迴圈
-
場景1:
之前猜數字的小游戲猜一次之後程式就會自動退出,那麼有什麼辦法可以猜一次之後繼續猜測,直到猜測正確
#!/usr/bin/env python number = 20 for i in range(10): input_num = int(input("請猜一個數字: ")) if input_num == number: print("恭喜您,猜對了") # 如果猜對了,則退出整個迴圈 break elif input_num > number: print("請猜一個更小的數字") else: print("請猜一個更大的數字")
-
場景2:
當猜測三個之後,程式會提示是否仍然要繼續
#!/usr/bin/env python number = 20 # 定義一個計數器變數 count = 0 for i in range(10): # 當計數器的值大於2時,提示是否繼續,如果選擇繼續,則將計數器重置為0,否則退出整個迴圈 if count > 2: user_input = input("請問是否仍然要繼續?(y/n): ") if user_input == "y": count = 0 continue else: break input_num = int(input("請猜一個數字: ")) if input_num == number: print("恭喜您,猜對了") break elif input_num > number: print("請猜一個更小的數字") else: print("請猜一個更大的數字") # 迴圈執行一次,即猜測一次數字,計數器加1 # count += 1 等價於 count = count + 1 count += 1