第一節 python

來源:互聯網
上載者:User

標籤:組合   post   使用   編碼   通過   中文   字母   轉變   規則   

在linux 下建立一個檔案叫hello.py,並輸入
1 print("Hello World!")
然後執行命令:python hello.py ,輸出
123 localhost:~ jieli$ vim hello.pylocalhost:~ jieli$ python hello.pyHello World!
指定解譯器上一步中執行 python hello.py 時,明確的指出 hello.py 指令碼由 python 解譯器來執行。如果想要類似於執行shell指令碼一樣執行python指令碼,例: ./hello.py ,那麼就需要在 hello.py 檔案的頭部指定解譯器,如下:
123 #!/usr/bin/env python  print "hello,world"
如此一來,執行: ./hello.py 即可。ps:執行前需給予 hello.py 執行許可權,chmod 755 hello.py聲明變數
123 #_*_coding:utf-8_*_ (聲明字型為utf-8) name = "Alex Li"
上述代碼聲明了一個變數,變數名為: name,變數name的值為:"Alex Li" 變數定義的規則:
  • 變數名只能是 字母、數字或底線的任意組合
  • 變數名的第一個字元不能是數字
  • 以下關鍵字不能聲明為變數名
[‘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‘]變數的賦值
12345678 name = "Alex Li" name2 = nameprint(name,name2) name = "Jack" print(name,name2)
如上執行結果是 Alex Li Alex Li / Jack Alex Li 注釋 單行注釋用#,多行注釋用"""注釋內容"""應該顯示的告訴python解譯器,用什麼編碼來執行原始碼,即:
1234 #!/usr/bin/env python(使用python解譯器)# -*- coding: utf-8 -*-(使用utf-8編碼來處理這個程式,否則打不出來漢字)  print "你好,世界"
input,提供輸入入口,以下會提示,what is your name ,如填下xuweiwie,最終列印 hello xuweiweiname = input("What is your name?")print("Hello " + name )編譯型&解釋型編譯型語言在程式執行之前,先會通過編譯器對程式執行一個編譯的過程,把程式轉變成機器語言。運行時就不需要翻譯,而直接執行就可以了。最典型的例子就是C語言。解釋型語言就沒有這個編譯的過程,而是在程式啟動並執行時候,通過解譯器對程式逐行作出解釋,然後直接運行,最典型的例子是Rubypython是先編譯後解釋字串格式化輸出
1234 name = "alex"print ("i am %s " % name)  #輸出: i am alex
 
1234 name = 234print ("i am %d " % name)  #輸出: i am 234
 PS: 字串是 %s;整數 %d;浮點數%fpython3比python2多了一個預設的中文字元編碼

第一節 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.