Python之路,Day1 - Python基礎1

來源:互聯網
上載者:User

標籤:python

四、Python安裝

windows

1 、下載安裝包      https: / / www.python.org / downloads / 2 、安裝      預設安裝路徑:C:\python27 3 、配置環境變數      【右鍵電腦】 - - 》【屬性】 - - 》【進階系統設定】 - - 》【進階】 - - 》【環境變數】 - - 》【在第二個內容框中找到 變數名為Path 的一行,雙擊】 - - > 【Python安裝目錄追加到變值值中,用 ; 分割】

    如:原來的值;C:\python27,切記前面有分號

linux、Mac


無需安裝,原裝Python環境    ps:如果內建 2.6 ,請更新至 2.7

五、Hello World程式

在linux 下建立一個檔案叫hello.py,並輸入

print("Hello World!")

然後執行命令:python hello.py ,輸出

localhost:~ jieli$ vim hello.py localhost:~ jieli$ python hello.py Hello World!

指定解譯器

上一步中執行 python 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內建的互動器運行代碼,

localhost:~ jieli$ python Python 2.7.10 (default, Oct 23 2015, 18:05:06) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> print("Hello World!")Hello World!

六、變數\字元編碼 

聲明變數

#_*_coding: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‘]

變數的賦值

name = "Alex Li"name2 = name print(name,name2)name = "Jack"print("What is the value of name2 now?")

注釋

  當行注視:# 被注釋內容

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


本文出自 “sandshell” 部落格,請務必保留此出處http://sandshell.blog.51cto.com/9055959/1974951

Python之路,Day1 - Python基礎1

聯繫我們

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