(二)、Python 基礎

來源:互聯網
上載者:User

標籤:chm   port   顯示   流程   其他   continue   col   簡潔   輸入密碼   

Python入門

一、第一句Python

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

print  "hello,world"

執行 hello.py 檔案,即: python /home/dev/hello.py

python內部執行過程如下:

  python檔案的尾碼名可以是任意的,但是當需要匯入模組時候,如果不是 .py結尾的檔案,會報錯,所以以後都寫成 .py 結尾的檔案就好了

 

二、解譯器

上一步中執行 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 檔案中的代碼時,會對內容進行編碼。

註:python3 預設 utf-8 , python2 預設是 ASCII

ASCII(American Standard Code for Information Interchange,美國標準資訊交換代碼)是基於拉丁字母的一套電腦編碼系統,主要用於顯示現代英語和其他西歐語言,其最多隻能用 8 位來表示(一個位元組),即:2**8 = 256,所以,ASCII碼最多隻能表示 256 個符號。

顯然ASCII碼無法將世界上的各種文字和符號全部表示,所以,就需要新出一種可以代表所有字元和符號的編碼,即:Unicode

 

Unicode(統一碼、萬國碼、單一碼)是一種在電腦上使用的字元編碼。Unicode 是為瞭解決傳統的字元編碼方案的局限而產生的,它為每種語言中的每個字元設定了統一併且唯一的二進位編碼,規定雖有的字元和符號最少由 16 位來表示(2個位元組),即:2 **16 = 65536,
註:此處說的的是最少2個位元組,可能更多

 

gbk,gb2312 ,只適用於中國,支援繁體,中文需要2個位元組表示

 

UTF-8,是對Unicode編碼的壓縮和最佳化,他不再使用最少使用2個位元組,而是將所有的字元和符號進行分類:ascii碼中的內容用1個位元組儲存、歐洲的字元用2個位元組儲存,東亞的字元用3個位元組儲存...

 

所以,在寫代碼時,為了不出現亂碼,推薦使用UTF-8,會加入 # -*- coding: utf-8 -*-

#!/usr/bin/env python# -*- coding: utf-8 -*-  print "你好,世界"

Python3 無需關注
Python2 每個檔案中只要出現中文,頭部必須加

 

四、注釋

  當行注視:# 被注釋內容

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

 

五、執行指令碼傳入參數

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

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

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

#!/usr/bin/env python# -*- coding: utf-8 -*-  import sys  print sys.argv

 

六、 pyc 檔案

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

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

 

七、變數

#!/usr/bin/env python# -*- coding: utf-8 -*-  name = "liyongjian5179"

變數定義的規則:

    • 變數名只能是 字母、數字或底線的任意組合
    • 變數名的第一個字元不能是數字
    • 以下關鍵字不能聲明為變數名
      [‘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 內建的東西重複,使用 Pycharm 編程來避免吧

變數名避免出現駝峰式的命名,例如:userId  Java和C#中會使用這種駝峰式變數名 python中不要使用

 

八、輸入

執行一個操作
提醒使用者輸入:使用者和密碼
擷取使用者名稱和密碼,檢測:使用者名稱=root 密碼=root
正確:登陸成功
錯誤:登陸失敗

a. input的用法,永遠等待,直到使用者輸入了值,就會將輸入的值賦值給一個東西

n1 = input(‘請輸入使用者名稱:‘)
n2 = input(‘請輸入密碼:‘)
print(n1)
print(n2)

#!/usr/bin/env python# -*- coding: utf-8 -*-  # 將使用者輸入的內容賦值給 name 變數name = input("請輸入使用者名稱:")  # 列印輸入的內容print name

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

#!/usr/bin/env python# -*- coding: utf-8 -*-  import getpass  # 將使用者輸入的內容賦值給 name 變數pwd = getpass.getpass("請輸入密碼:")  # 列印輸入的內容print pwd

 

九、流程式控制制和縮排

  條件陳述式

1. if基本語句                    if 條件:                        內部代碼塊                        內部代碼塊                    else:                        ...                                        print(‘....‘)                                        if 1 == 1:                        print("歡迎進入第一會所1")                        print("歡迎進入第一會所2")                        # TAB 鍵                    else:                        print("歡迎進入一本道")                    2. if 支援嵌套                                        if 1 == 1:                        if 2 == 2:                            print("歡迎進入第一會所1")                            print("歡迎進入第一會所2")                        else:                            print(‘歡迎進入東京特‘)                    else:                        print("歡迎進入一本道")

3. if elif inp = input(‘請輸入會員層級:‘) if inp == "進階會員": print(‘美女‘) elif inp == "白金會員": print(‘大摩‘) elif inp == "鉑金會員": print(‘一線小明星‘) else: print(‘城管‘) print(‘開始服務把....‘) 補充:pass 代指空代碼,無意義,僅僅用於表示代碼塊 if 1==1: pass else: print(‘sb‘)

4. 條件  and or

              if n1 == "alex" or n2 == "alex!23":
                 print(‘OK‘)
              else:
                 print(‘OK‘)

  縮排用4個空格

 

十、while迴圈

1、基本迴圈

while 條件:         # 迴圈體     # 如果條件為真,那麼迴圈體則執行    # 如果條件為假,那麼迴圈體不執行

2、break

break用於退出所有迴圈

while True:    print "123"    break    print "456"

3、continue

continue用於退出當前迴圈,繼續下一次迴圈

while True:    print "123"    continue    print "456"

 

十一、基礎資料型別 (Elementary Data Type)

  字串 - n1 = "alex" n2 = ‘root‘ n3 = """eric""" n4=‘‘‘tony‘‘‘

  帶引號的就是字串:

      name = "我是是徵文" 引號引起來的就叫字串
      name = ‘pp‘
      name = """pp"""
      name = ‘‘‘我是是徵文‘‘‘

  加法:
      n1 = "alex"
      n2 = "sb"
      n4 = "db"

      n3 = n1 + n2 + n4

  乘法:
      n1 = "alex"
      n2 = n1 * 10


  數字 - age=21 weight = 64 fight = 5

  加減乘除次方餘:
      a1 = 10
      a2 = 20

      a3 = a1 + a2

      a3 = a1 - a2

      a3 = a1 * a2

      a3 = 100 / 10

      a3 = 4**4

      a3 = 39 % 8 # 擷取39除以8得到的餘數

  補充:
      a3 = 39 // 8 拿那個商 等於4 4*8=32


      a = 13
      temp = a % 2
      if temp == 0:
        print("偶數")
      else:
        print(‘奇數‘)

 

十二、練習題

  if條件陳述式
  while迴圈
  奇數偶數

                1、使用while迴圈輸入 1 2 3 4 5 6     8 9 10                    n = 1                    while n < 11:                        if n == 7:                            pass                        else:                            print(n)                        n = n + 1                                        print(‘----end----‘)                                    2、求1-100的所有數的和                    n = 1                    s = 0                    while n < 101:                        s = s + n                                                n = n + 1                                        print(s)                                3、輸出 1-100 內的所有奇數                                        n = 1                    while n < 101:                        temp = n % 2                        if temp == 0:                            pass                        else:                            print(n)                        n = n + 1                                        print(‘----end----‘)                                        4、輸出 1-100 內的所有偶數                                    n = 1                    while n < 101:                        temp = n % 2                        if temp == 0:                            print(n)                        else:                            pass                        n = n + 1                                        print(‘----end----‘)                5、求1-2+3-4+5 ... 99的所有數的和                    n = 1                    s = 0 # s是之前所有數的總和                    while n < 100:                        temp = n % 2                        if temp == 0:                            s = s - n                            else:                            s = s + n                                                n = n + 1                                        print(s)

 

 

(二)、Python 基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.