Python之路——基礎知識02
一、Hello World!
假設你已經安裝好了Python, 那麽在Linux命令行輸入:
$python
將直接進入python。然後在命令行提示符>>>後面輸入:
>>>print(‘Hello World!‘)
可以看到,隨後在屏幕上輸出:
Hello World!
二、解釋器
如果想要類似於執行shell腳本一樣執行python腳本,例:./hello.py ,那麽就需要在 hello.py 文件的頭部指定解釋器
1 2 3 |
#!/usr/bin/env python
print "hello,world"
|
如此一來,執行: ./hello.py
ps:執行前需給予 hello.py 執行權限,chmod 755 hello.py
三、內容編碼
python解釋器在加載 .py 文件中的代碼時,會對內容進行編碼(默認ascill)
ps:應該顯示的告訴python解釋器,用什麽編碼來執行源代碼。(2.7 # -*- conding:utf- -*- 3.X 默認utf-8)
#!/usr/bin/env python # -*- coding: utf-8 -*- print "你好,世界"
四、註釋
當行註視:# 被註釋內容
多行註釋:""" 被註釋內容 """
五、執行腳本傳入參數
Python有大量的模塊,從而使得開發Python程序非常簡潔。類庫有包括三中:
- Python內部提供的模塊
- 業內開源的模塊
- 程序員自己開發的模塊
Python內部提供一個 sys 的模塊,其中的 sys.argv 用來捕獲執行執行python腳本時傳入的參數
1 2 3 4 5 6 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
print sys.argv
|
六、 pyc 文件
執行Python代碼時,如果導入了其他的 .py 文件,那麽,執行過程中會自動生成一個與其同名的 .pyc 文件,該文件就是Python解釋器編譯之後產生的字節碼。
ps:代碼經過編譯可以產生字節碼;字節碼通過反編譯也可以得到代碼。
七、變量
聲明變量
1 2 3 4 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
name = "wupeiqi"
|
上述代碼聲明了一個變量,變量名為: name,變量name的值為:"wupeiqi"
變量的作用:昵稱,其代指內存裏某個地址中保存的內容
變量定義的規則:
- 變量名只能是 字母、數字或下劃線的任意組合
- 變量名的第一個字符不能是數字
- 以下關鍵字不能聲明為變量名
- [‘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‘]
#一個等號是賦值,兩個等號是比較,!=表示不等於。
八、輸入1 2 3 4 5 6 7 8 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 將用戶輸入的內容賦值給 name 變量
name = raw_input ( "請輸入用戶名:" )
# 打印輸入的內容
print name
2.7中 raw_input |
輸入密碼時,如果想要不可見,需要利用getpass 模塊中的 getpass方法,即:
1 2 3 4 5 6 7 8 9 10 |
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import getpass
# 將用戶輸入的內容賦值給 name 變量
pwd = getpass.getpass( "請輸入密碼:" )
# 打印輸入的內容
print pwd
|
九、流程控制和縮進
需求一、用戶登陸驗證
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#!/usr/bin/env python
# -*- coding: encoding -*-
# 提示輸入用戶名和密碼
# 驗證用戶名和密碼
# 如果錯誤,則輸出用戶名或密碼錯誤
# 如果成功,則輸出 歡迎,XXX!
import getpass
name = raw_input ( ‘請輸入用戶名:‘ )
pwd = getpass.getpass( ‘請輸入密碼:‘ )
if name = = "alex" and pwd = = "cmd" :
print "歡迎,alex!"
else :
print "用戶名和密碼錯誤"
|
需求二、根據用戶輸入內容輸出其權限
1 2 3 4 5 6 |
# 根據用戶輸入內容打印其權限
# alex --> 超級管理員
# eric --> 普通管理員
# tony,rain --> 業務主管
# 其他 --> 普通用戶
|
1 2 3 4 5 6 7 8 9 10 11 |
name = raw_input ( ‘請輸入用戶名:‘ )
if name = = "alex" :
print "超級管理員"
elif name = = "eric" :
print "普通管理員"
elif name = = "tony" or name = = "rain" :
print "業務主管"
else :
print "普通用戶"
|
PS:外層變量,可以被內層變量使用; 內層變量,無法被外層變量使用。
十、while循環
1、基本循環
1 2 3 4 5 |
while 條件:
# 循環體
#當循環到尾部時,判斷條件是否True,如果是繼續執行。
|
2、break
用於跳出當前循環,並且下面的代碼不再執行。
1 2 3 4 |
while True :
print "123"
break
print "456"
|
3、continue
用於跳出本次循環,重新執行下一次循環。
1 2 3 4 |
while True :
print "123"
continue
print "456"
|
十一、編碼、解碼
#py2 # -*- conding:utf-8 -*- temp = "周傑倫" #utf-8 #解碼,需要指定原來是什麽編碼 temp_unicode = temp.decode(‘utf-8‘) #編碼,需要指定要變成什麽編碼 temp_gbk = temp_unicode.encode(‘gbk‘) #py3 自動轉換utf-8 unicode gbk #py3 移除了python的unicode類型 temp = "周傑倫" #utf-8 temp_gbk = temp.encode(‘gbk‘) print(temp_gbk)
十二、運算符
#算術運算 + - * / % 取模運算 可用來判斷奇偶 ** 冪 // 取整除 py2: 9/2 = 4 9/2 = 4.5(導入模塊from __future__ import division) py3: 9/2 = 4.5 #比較運算 == 等於 != 不等於 <> 不等於(2) < > >= <= #賦值運算 = += -= *= /= **= %= //= #邏輯運算 and 與 or 或 not 非 #成員運算 in not in
Python之路——基礎知識02