Day1,day

來源:互聯網
上載者:User

Day1,day

一、第一句Python代碼

在 /home/dev/ 目錄下建立 hello.py 檔案,內容如下:

1 [root@python-3 scripts]# cat hello.py 2 #!/usr/bin/env python3 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 python2   3 print "你好,世界"

改正:應該顯示的告訴python解譯器,用什麼編碼來執行原始碼,即

1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3   4 print "你好,世界"

四、注釋

  當行注視:# 被注釋內容

  多行注釋:""" 被注釋內容 """

五、執行指令碼傳入參數

Python有大量的模組,從而使得開發Python程式非常簡潔。類庫有包括三中:

  • Python內部提供的模組
  • 業內開源的模組
  • 程式員自己開發的模組

Python內部提供一個 sys 的模組,其中的 sys.argv 用來捕獲執行執行python指令碼時傳入的參數.

1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3   4 import sys5   6 print sys.argv

六、 pyc 檔案

執行Python代碼時,如果匯入了其他的 .py 檔案,那麼,執行過程中會自動產生一個與其同名的 .pyc 檔案,該檔案就是Python解譯器編譯之後產生的位元組碼。

ps:代碼經過編譯可以產生位元組碼;位元組碼通過反編譯也可以得到代碼。

 

七、變數

1、聲明變數

1 #!/usr/bin/env python2 3 # -*- coding: 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']

2、變數的賦值

1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3 4 name1 = "nulige"5 name2 = "alex"

1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3 4 name1 = "nulige"5 name2 = name1

 

3、變數的賦值樣本

1 #Author: huzhihua2 name = "Alex li"3 name2 = name4 print("My name is " ,name,name2)5 6 name = "PaoChe Ge"7 print(name,name2)

執行結果:

1 My name is Alex li Alex li2 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"

八、輸入

1 #!/usr/bin/env python2 # -*- coding: utf-8 -*-3   4 # 將使用者輸入的內容賦值給 name 變數5 name = raw_input("請輸入使用者名稱:")6   7 # 列印輸入的內容8 print name

輸入密碼時,如果想要不可見,需要利用getpass 模組中的 getpass方法,即:

 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

九、流程式控制制和縮排

需求一、使用者登陸驗證

 1 #!/usr/bin/env python 2 # -*- coding: encoding -*- 3    4 # 提示輸入使用者名稱和密碼 5    6 # 驗證使用者名稱和密碼 7 #     如果錯誤,則輸出使用者名稱或密碼錯誤 8 #     如果成功,則輸出 歡迎,XXX! 9  10  11 import getpass12   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:nulige2 password:1234563 Welcome user nulige login...

輸入錯誤的使用者名稱和密碼,提示:Invalid username or password!

1 username:nulige2 password:3212113 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:232 think bigger!
1 guess age:582 think smaller... 
1 guess age:562 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         break13     elif guess_age > age_of_oldboy:14         print("think smaller... ")15     else:16         print("think bigger!")17     count +=118 if count == 3:19     print("you have tried too many times..fuck off")

執行結果:

1 guess age:12 think bigger!3 guess age:24 think bigger!5 guess age:36 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         break13     elif guess_age > age_of_oldboy:14         print("think smaller... ")15     else:16         print("think bigger!")17     count +=118 else:19     print("you have tried too many times..fuck off")

執行結果:

1 guess age:232 think bigger!3 guess age:454 think bigger!5 guess age:676 think smaller... 7 you have tried too many times..fuck off

 原理圖:

 

十、while迴圈

1、基本迴圈

1 while 條件:2      3     # 迴圈體4  5     # 如果條件為真,那麼迴圈體則執行6     # 如果條件為假,那麼迴圈體不執行

樣本

1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 #Author: huzhihua4 5 count = 06 while True:7     print("count:",count)8     count = count +1  #count +=19     

 

2、break

break用於退出所有迴圈

樣本1:

1 while True:2     print ("123")3     break4     print ("456")

執行結果:

1 123

樣本2:

1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 #Author: nulige4 5 count = 06 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: 60546010 count: 605461後面省略.....

樣本3:

意思是:執行到10就中間(從0-9)

1 count = 02 while True:3     print("count:",count)4     count = count +1  #count +=15     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: 810 count: 9
練習題

1、使用while迴圈輸入 1 2 3 4 5 6     8 9 10

2、求1-100的所有數的和

3、輸出 1-100 內的所有奇數

4、輸出 1-100 內的所有偶數

5、求1-2+3-4+5 ... 99的所有數的和

6、使用者登陸(三次機會重試)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.