一、第一句Python代碼
在 /home/dev/ 目錄下創建 hello.py 文件,內容如下:
1 [root@python-3 scripts]# cat hello.py 2 #!/usr/bin/env python 3 4 print("Hello World!")
輸出結果:
1 [root@python-3 scripts]# python hello.py 2 Hello World!
二、解釋器
上一步中執行 python /home/dev/hello.py 時,明確的指出 hello.py 腳本由 python 解釋器來執行。
如果想要類似於執行shell腳本一樣執行python腳本,例: ./hello.py
,那么就需要在 hello.py 文件的頭部指定解釋器,如下:
#!/usr/bin/env python print "hello,world"
如此一來,執行: ./hello.py
即可。
ps:執行前需給予 hello.py 執行權限,chmod 755 hello.py 否則會報錯!
三、內容編碼
python解釋器在加載 .py 文件中的代碼時,會對內容進行編碼(默認ascill)
ASCII(American Standard Code for Information Interchange,美國標准信息交換代碼)是基於拉丁字母的一套電腦編碼系統,主要用於顯示現代英語和其他西歐語言,其最多只能用 8 位來表示(一個字節),即:2**8 = 256,所以,ASCII碼最多只能表示 256 個符號。
顯然ASCII碼無法將世界上的各種文字和符號全部表示,所以,就需要新出一種可以代表所有字符和符號的編碼,即:Unicode
Unicode(統一碼、萬國碼、單一碼)是一種在計算機上使用的字符編碼。Unicode 是為了解決傳統的字符編碼方案的局限而產生的,它為每種語言中的每個字符設定了統一並且唯一的二進制編碼,規定雖有的字符和符號最少由 16 位來表示(2個字節),即:2 **16 = 65536,
注:此處說的的是最少2個字節,可能更多
UTF-8,是對Unicode編碼的壓縮和優化,他不再使用最少使用2個字節,而是將所有的字符和符號進行分類:ascii碼中的內容用1個字節保存、歐洲的字符用2個字節保存,東亞的字符用3個字節保存...
所以,python解釋器在加載 .py 文件中的代碼時,會對內容進行編碼(默認ascill),如果是如下代碼的話:
報錯:ascii碼無法表示中文
1 #!/usr/bin/env python #python3.0的格式 2 3 print "你好,世界"
改正:應該顯示的告訴python解釋器,用什么編碼來執行源代碼,即
1 #!/usr/bin/env python #python2.7的格式 2 # -*- coding: utf-8 -*- #Python2.7 每個文件中只要出現中文,頭部必須加 3 4 print "你好,世界"
編碼解釋如下:
1 8位:所有英文,字符,數字,ASCII 2 3 01001010 - 2**8 =256 4 A 65 '0b1000001' 5 6 7 萬國碼 unicode 8 A 65 '000000000b1000001' 9 最少用2個字節(16): 10 1byte = 8bit =01010101 11 2 16 0101010101010101 =2**16 12 13 漢字占3個字節: 14 劉小明 15 三個漢字=9字節 16 010101010101010101010100 17 18 UTF-8 19 unicode加工 20 英文:8位 21 歐洲:16位 22 中文:24位
四、注釋
當行注視:# 被注釋內容
多行注釋:""" 被注釋內容 """
ps:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: huzhihua 4 5 ''' #這就是注釋 6 7 a = "alex" 8 b = a.capitalize() 9 print(a) 10 print(b) 11 12 ''' #這就是注釋
五、執行腳本傳入參數
Python有大量的模塊,從而使得開發Python程序非常簡潔。類庫有包括三中:
- Python內部提供的模塊
- 業內開源的模塊
- 程序員自己開發的模塊
Python內部提供一個 sys 的模塊,其中的 sys.argv 用來捕獲執行執行python腳本時傳入的參數.
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import sys 5 6 print sys.argv
六、 pyc 文件
執行Python代碼時,如果導入了其他的 .py 文件,那么,執行過程中會自動生成一個與其同名的 .pyc 文件,該文件就是Python解釋器編譯之后產生的字節碼。
ps:代碼經過編譯可以產生字節碼;字節碼通過反編譯也可以得到代碼。
七、變量
1、聲明變量
1 #!/usr/bin/env python #解釋器 2 3 # -*- coding: utf-8 -*- #寫python2.7必須把這段代碼加上,因為2.7默認用的是ASCII碼。如果是python3.0就不需要這段代碼,因為他默認就是utf-8,utf-8默認就支持中文。 4 5 name = "nulige"
上述代碼聲明了一個變量,變量名為: name,變量name的值為:"nulige"
變量的作用:昵稱,其代指內存里某個地址中保存的內容

變量定義的規則:
- 變量名只能是 字母、數字或下划線的任意組合
- 變量名的第一個字符不能是數字
- 以下關鍵字不能聲明為變量名
['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'] - 最好不好和python內置的東西重復
2、變量的賦值
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 name1 = "nulige" 5 name2 = "alex"
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 name1 = "nulige" 5 name2 = name1
3、變量的賦值示例
1 #Author: huzhihua 2 name = "Alex li" 3 name2 = name 4 print("My name is " ,name,name2) 5 6 name = "PaoChe Ge" 7 print(name,name2)
執行結果:
1 My name is Alex li Alex li 2 PaoChe Ge Alex li
4、變量的幾種常用用法
1 1) 2 _name = "Alex li" 3 4 2) 5 name = "Alex li" 6 7 3) 8 gf_gf_oldboy ="Chen rong hua" 9 10 4) 11 GFOfOldboy = "Chen rong hua"
八、輸入
執行一個操作
提醒用戶輸入:用戶和密碼
獲取用戶名和密碼,檢測:用戶名=root 密碼=root
正確:登錄成功
錯誤:登陸失敗
input的用法,永遠等待,直到用戶輸入了值,就會將輸入的值賦值給一個東西
1 n1 = input('請輸入用戶名:') 2 n1 = input('請輸入密碼:') 3 4 print(n1) 5 print(n2)
判斷用戶輸入用戶名和密碼
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 n1 = input("請輸入用戶名:") 6 n2 = input("請輸入密碼:") 7 if n1 == "root" and n2 == "root!23": 8 print("登錄成功") 9 else: 10 print("登錄失敗")
執行結果:
1 請輸入用戶名:root 2 請輸入密碼:root!23 3 登錄成功
python2.7 和3.0 inpu用法的區別
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 # 將用戶輸入的內容賦值給 name 變量 5 name = raw_input("請輸入用戶名:") #python2.7的用法 6 #name = input("請輸入用戶名:") #python3.0的用法
7 # 打印輸入的內容 8 print name
輸入密碼時,如果想要不可見,需要利用getpass 模塊中的 getpass方法,即:
ps1:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import getpass 5 6 # 將用戶輸入的內容賦值給 name 變量 7 pwd = getpass.getpass("請輸入密碼:") 8 9 # 打印輸入的內容 10 print pwd
ps2:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 #寫各種各樣的功能 6 #找到lib.py,將文件內容替換 import lib 7 #import lib 8 9 #print('index') 10 import getpass 11 12 #等待用戶輸入用戶名,用戶輸入之后 13 #將輸入的用戶名 賦值給i1,即:i1代指用戶名 14 15 i1 = raw_input("UserName:") #python2.7 16 17 #i2 =raw_input("請輸入密碼:") 18 #等待用戶輸入密碼,用戶輸入之后 19 #將輸入的密碼,賦值給i2,即:i2代指密碼, 20 i2 = getpass.getpass("PassWord:") 21 print(i1) 22 print(i2)
創建一個基本的py文件的流程如下:
1、創建xxx.py 文件
ps:不要有中文路徑
2、寫代碼
a、頭部兩行
b、寫功能代碼
3、執行代碼
a、打開終端
功能鍵+R
b、python 代碼文件的路徑
九、流程控制和縮進
1 條件語句 2 縮進用4個空格 3 a. 4 n1 = input('>>>') 5 6 if "alex" == "alex": 7 n2 = input('>>>') 8 if n2 == "確認": 9 print('alex SB') 10 else: 11 print('alex DB') 12 else: 13 print('error') 14 15 注意: 16 n1 = "alex" 賦值 17 n1 == 'alex' 比較, 18 19 b. 20 if 條件1: 21 pass 22 elif 條件2: 23 pass 24 elif 條件3: 25 pass 26 else: 27 pass 28 29 print('end') 30 31 c.條件1 32 and or 33 34 if n1 == "alex" or n2 == "alex!23": 35 print('OK') 36 else: 37 print('OK') 38 39 PS: 40 pass 代指空代碼,無意義,僅僅用於表示代碼塊
需求一、用戶登陸驗證
1 #!/usr/bin/env python 2 # -*- coding: encoding -*- 3 4 # 提示輸入用戶名和密碼 5 6 # 驗證用戶名和密碼 7 # 如果錯誤,則輸出用戶名或密碼錯誤 8 # 如果成功,則輸出 歡迎,XXX! 9 10 11 import getpass 12 13 14 name = raw_input('請輸入用戶名:') 15 pwd = getpass.getpass('請輸入密碼:') 16 17 if name == "alex" and pwd == "cmd": 18 print "歡迎,alex!" 19 else: 20 print "用戶名和密碼錯誤"
需求二、根據用戶輸入內容輸出其權限
1 # 根據用戶輸入內容打印其權限 2 3 # alex --> 超級管理員 4 # eric --> 普通管理員 5 # tony,rain --> 業務主管 6 # 其他 --> 普通用戶 7 8 name = raw_input('請輸入用戶名:') 9 10 if name == "alex": 11 print "超級管理員" 12 elif name == "eric": 13 print "普通管理員" 14 elif name == "tony" or name == "rain": 15 print "業務主管" 16 else: 17 print "普通用戶"
需求三: 判斷輸入用戶和密碼是否正確
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: huzhihua 4 #import getpass 5 6 _username = 'nulige' 7 _password = '123456' 8 username = input("username:") 9 #password = getpass.getpass("password:") 10 password = input("password:") 11 if _username == username and _password == password: 12 print("Welcome user {name} login...".format(name=username)) 13 else: 14 print("Invalid username or password!")
執行結果:
輸入正確的用戶名和密碼,提示:Welcome user nulige login...
1 username:nulige 2 password:123456 3 Welcome user nulige login...
輸入錯誤的用戶名和密碼,提示:Invalid username or password!
1 username:nulige 2 password:321211 3 Invalid username or password!
需求四:判斷年齡
示例1
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: huzhihua 4 5 age_of_oldboy = 56 6 guess_age = int(input("guess age:")) 7 if guess_age == age_of_oldboy: 8 print("yes, you got it. ") 9 elif guess_age > age_of_oldboy: 10 print("think smaller... ") 11 else: 12 print("think bigger!")
執行結果:
1 guess age:23 2 think bigger!
1 guess age:58 2 think smaller...
1 guess age:56 2 yes, you got it.
示例2
輸入三次,就會打印出 you have tried too many times..fuck off 這段話
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 age_of_oldboy = 56 6 count = 0 7 8 while count <3: 9 guess_age = int(input("guess age:")) 10 if guess_age == age_of_oldboy : 11 print("yes, you got it. ") 12 break 13 elif guess_age > age_of_oldboy: 14 print("think smaller... ") 15 else: 16 print("think bigger!") 17 count +=1 18 if count == 3: 19 print("you have tried too many times..fuck off")
執行結果:
1 guess age:1 2 think bigger! 3 guess age:2 4 think bigger! 5 guess age:3 6 think bigger! 7 you have tried too many times..fuck off
示例3
輸入三次進行條件判斷
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 age_of_oldboy = 56 6 count = 0 7 8 while count <3: 9 guess_age = int(input("guess age:")) 10 if guess_age == age_of_oldboy : 11 print("yes, you got it. ") 12 break 13 elif guess_age > age_of_oldboy: 14 print("think smaller... ") 15 else: 16 print("think bigger!") 17 count +=1 18 else: 19 print("you have tried too many times..fuck off")
執行結果:
1 guess age:23 2 think bigger! 3 guess age:45 4 think bigger! 5 guess age:67 6 think smaller... 7 you have tried too many times..fuck off
原理圖:
十、while循環
1、基本循環
1 while 條件: 2 3 # 循環體 4 5 # 如果條件為真,那么循環體則執行 6 # 如果條件為假,那么循環體不執行
示例
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: huzhihua 4 5 count = 0 6 while True: 7 print("count:",count) 8 count = count +1 #count +=1 9
2、break
break用於退出所有循環
示例1:
1 while True: 2 print ("123") 3 break 4 print ("456")
執行結果:
1 123
示例2:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 count = 0 6 while True: 7 print("count:",count) 8 count = count +1 #count +=1
執行結果:
1 count: 605452 2 count: 605453 3 count: 605454 4 count: 605455 5 count: 605456 6 count: 605457 7 count: 605458 8 count: 605459 9 count: 605460 10 count: 605461 后面省略.....
示例3:
意思是:執行到10就中間(從0-9)
1 count = 0 2 while True: 3 print("count:",count) 4 count = count +1 #count +=1 5 if count == 10: 6 break
執行結果:
1 count: 0 2 count: 1 3 count: 2 4 count: 3 5 count: 4 6 count: 5 7 count: 6 8 count: 7 9 count: 8 10 count: 9
練習題
1、使用while循環輸入 1 2 3 4 5 6 8 9 10
法一:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 n = 1 6 while n < 11: 7 if n == 7: 8 pass 9 else: 10 print(n) 11 n = n + 1
法二:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 i = 1 6 while i<=10: 7 if i==7: 8 i += 1 9 else: 10 print(i) 11 i+=1
執行結果:
1 1 2 2 3 3 4 4 5 5 6 6 7 8 8 9 9 10
2、求1-100的所有數的和
法一:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 n = 1 6 s = 0 7 while n < 101: 8 s = s + n 9 n = n + 1 10 11 print(s)
法二:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 i = 1 6 sum = 0 7 while i<=100: 8 sum += i 9 i += 1 10 print(sum)
執行結果:
1 5050
法三:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 n = 0 6 for x in range(101): 7 n = n + x 8 print(n)
執行結果:
1 5050
法四:
用求和函數方法實現
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 print (sum(range(1, 101)))
執行結果:
1 5050
Linux的實現方法:
1 [root@python-3 scripts]# awk 'BEGIN{i=1;do{sum+=i;i++}while (i<=100);print sum}' 2 5050 3 [root@python-3 scripts]# awk 'BEGIN{i=1;while (i<=100){sum+=i;i++};print sum}' 4 5050 5 [root@python-3 scripts]# awk 'BEGIN{sum=0;for (i=1;i<=100;i++)sum+=i;print sum}' 6 5050 7 [root@python-3 scripts]# declare sum=0;for i in `seq 1 100`;do let sum+=$i;done;echo $sum 8 5050
3、輸出 1-100 內的所有奇數
知識點:整數中,能被2整除的數是偶數,不能被2整除的數是奇數
ps:
奇數就是單數、如:1,3,5,7,9,11等。
法一:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 n = 1 6 while n < 101: 7 temp = n % 2 8 if temp == 0: 9 pass 10 else: 11 print(n) 12 n = n + 1
法二:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 count = 1 6 while count < 101: 7 num=count%2 8 if num == 1: 9 print(count) 10 count += 1
執行結果:
1 1 2 3 3 5 4 7 5 9 6 11 7 13 8 15 9 17 10 19 11 21 12 23 13 25 14 27 15 29 16 31 17 33 18 35 19 37 20 39 21 41 22 43 23 45 24 47 25 49 26 51 27 53 28 55 29 57 30 59 31 61 32 63 33 65 34 67 35 69 36 71 37 73 38 75 39 77 40 79 41 81 42 83 43 85 44 87 45 89 46 91 47 93 48 95 49 97 50 99
法三:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 inte = 1 6 #輸出1到100間的奇數 7 while inte <= 100: 8 if inte % 2 == 1: 9 print (inte) 10 inte = inte + 1
執行結果:
1 1 2 3 3 5 4 7 5 9 6 11 7 13 8 15 9 17 10 19 11 21 12 23 13 25 14 27 15 29 16 31 17 33 18 35 19 37 20 39 21 41 22 43 23 45 24 47 25 49 26 51 27 53 28 55 29 57 30 59 31 61 32 63 33 65 34 67 35 69 36 71 37 73 38 75 39 77 40 79 41 81 42 83 43 85 44 87 45 89 46 91 47 93 48 95 49 97 50 99
4、輸出 1-100 內的所有偶數
知識點:整數中,能被2整除的數是偶數,不能被2整除的數是奇數
ps:
雙數就是偶數、如:0,2,4,6,8,10等。
法一:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 n = 1 6 while n < 101: 7 temp = n % 2 8 if temp == 0: 9 print(n) 10 else: 11 pass 12 n = n + 1
法二:
1 count = 1 2 while count < 101: 3 num=count%2 4 if num == 0: 5 print(count) 6 count += 1
執行結果:
1 2 2 4 3 6 4 8 5 10 6 12 7 14 8 16 9 18 10 20 11 22 12 24 13 26 14 28 15 30 16 32 17 34 18 36 19 38 20 40 21 42 22 44 23 46 24 48 25 50 26 52 27 54 28 56 29 58 30 60 31 62 32 64 33 66 34 68 35 70 36 72 37 74 38 76 39 78 40 80 41 82 42 84 43 86 44 88 45 90 46 92 47 94 48 96 49 98 50 100
5、求1-2+3-4+5 ... 99的所有數的和
法一:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 n = 1 6 s = 0 # s是之前所有數的總和 7 while n < 100: 8 temp = n % 2 9 if temp == 0: 10 s = s - n 11 else: 12 s = s + n 13 n = n + 1 14 15 print(s)
法二:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 count = 1 6 while count < 100: 7 if count == 1: 8 num = count 9 elif count%2 == 1: 10 num = num + count 11 elif count%2 == 0: 12 num = num - count 13 count += 1 14 print(num)
執行結果:
1 50
6、用戶登陸(三次機會重試)
流程圖
法一:
1 count = 0 2 while count < 3: 3 user = input('>>>') 4 pwd = input('>>>') 5 if user == 'alex' and pwd == '123': 6 print('歡迎登陸') 7 print('..........') 8 break 9 else: 10 print('用戶名或者密碼錯誤') 11 count = count + 1
法二:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author: nulige 4 5 _username,_password,i = "nulige","123456",1 6 while i < 4: 7 input_name = input("Username:") 8 input_passwd = input("Password:") 9 if input_name == _username and input_passwd == _password: 10 print("login is successful!") 11 break 12 else: 13 print("The user name or password you entered is incorrect. Please enter again.") 14 i += 1 15 continue 16 print("Enter more than 3 times, This program will exit")
執行結果:
輸入錯誤的用戶名和密碼
1 Username:111 2 Password:2222 3 The user name or password you entered is incorrect. Please enter again. 4 Username:2222 5 Password:2222 6 The user name or password you entered is incorrect. Please enter again. 7 Username:2222 8 Password:222 9 The user name or password you entered is incorrect. Please enter again. 10 Enter more than 3 times, This program will exit
輸入正確的用戶名和密碼
1 Username:nulige 2 Password:123456 3 login is successful! 4 Enter more than 3 times, This program will exit
法三:
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author: nulige 4 5 count = 3 6 while count > 0: 7 username = input("Please enter your username:") 8 pwd = input("Please enter your password:") 9 if username == "nulige" and pwd == "123456": 10 print("User:",username,",Login successful ! ") 11 break 12 else: 13 count -= 1 14 if count != 0: 15 print("Login failed",count) 16 else: 17 print("Enter more than 3 times, This program will exit")
執行結果:
輸入錯誤的用戶名和密碼
1 Please enter your username:111 2 Please enter your password:222 3 Login failed 2 4 Please enter your username:111 5 Please enter your password:222 6 Login failed 1 7 Please enter your username:111 8 Please enter your password:222 9 Enter more than 3 times, This program will exit
輸入正確的用戶名和密碼
1 Please enter your username:nulige 2 Please enter your password:123456 3 User: nulige ,Login successful !