跳轉至內容

Python 程式設計入門/Python 程式設計 - 安裝 Python

來自華夏公益教科書,開放的世界,開放的書籍

第二章:安裝 Python

[編輯 | 編輯原始碼]

本章將介紹如何在本地機器上安裝 Python。 Python 網站上提供了兩個版本的 Python,分別是 Python 2.7.XXXX 和 Python 3.0.XXX。我更喜歡使用和教授 Python 2.7.XXX 版本。在本手冊中,我將討論和編寫有關 Python 2.7.XXX 的內容。有人可能會問,有什麼區別呢?這兩個版本之間在語法和其他一些細微變化方面有所不同。

那麼,哪一個 Python 適合你呢?

Python 像任何其他開源軟體一樣,是一個不斷發展變化的軟體,來自世界各地的使用者都在為其貢獻、開發和更新功能。有一個成員委員會對特定功能進行投票,並決定是否需要整合該功能。作為支援的文件也在不斷發展。因此,您可以根據自己的偏好和評估來決定使用哪個版本。

也就是說,您瞭解其中任何一個版本就足夠了,瞭解一個版本就能輕鬆瞭解其他版本。記住這一點,然後開始學習 Python。

在決定學習哪個版本的 Python 之後,您需要做的第一件事就是安裝它。

2.1. 安裝 Python 版本

[編輯 | 編輯原始碼]

決定學習哪個版本的 Python 後,您需要安裝它。Linux 和 Mac OSX 機器上預裝了 Python 軟體包,因此您無需擔心安裝。執行命令終端並輸入關鍵字 python,機器將告訴您是否安裝了 Python。試試看,祝你好運!

由於我只有一臺 Windows 機器,我將指導您如何在 Windows 計算機上安裝 Python。試試看

事實上,Windows 未預裝 Python。從 Python 官方網站下載您要使用的 Python 軟體版本。

從 Python dot org 安裝 Python

  1. 訪問 https://python.club.tw/ftp/python/,選擇列出的最高版本號,然後下載 .exe 安裝程式,下載最新的 Python Windows 安裝程式。
  2. 雙擊安裝程式,Python-2.xxx.yyy.exe。名稱將取決於您閱讀本文時可用的 Python 版本。
  3. 確保您對要使用的機器具有管理員許可權。
  4. 逐步執行安裝程式。
  5. 如果您硬碟空間不足,可以從安裝中刪除一些功能。
  6. Python 將安裝所有正確的檔案和擴充套件。
  7. 安裝完成後,關閉安裝程式,然後選擇開始->程式->Python 2.7->IDLE(Python GUI)。您將看到類似以下內容,這是我機器上的示例截圖
   Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
   Type "copyright", "credits" or "license ()" for more information.
   >>> 

我假設您在當前計劃使用的機器上擁有標準的 Python 直譯器。

2.2. 互動式 Shell

[編輯 | 編輯原始碼]

安裝 Python 後,您可以透過多種方式執行 Python 程式。在 Shell 直譯器上,您可以執行命令並測試程式碼的流程。您可以儲存編寫的程式碼,然後執行該檔案以獲取編寫的程式碼的輸出。

這裡,我將解釋如何在 Python 直譯器上執行簡單的程式碼片段。

您可以透過以下任何方式啟動 Python Shell 直譯器。

2.2.1. 方法 1

[編輯 | 編輯原始碼]

透過按 Windows 鍵和字母 R 來執行命令直譯器。您將看到一個像這樣的視窗開啟。

在文字框中鍵入字母 cmd,您將看到類似於圖 2 中所示的螢幕。

在命令提示符下,切換到 Python 可執行檔案所在的目錄。例如,在我的本地機器上,Python 可執行檔案位於 C:\Python27\。因此,我將在命令提示符下鍵入“cd C:\Python27”,切換到該特定資料夾。這是我在本地機器上看到的內容。進入 Python27 資料夾後,在命令提示符下鍵入 python,瞧!您將看到圖 3 中所示的 Python Shell 直譯器。

2.2.2. 方法 2

[編輯 | 編輯原始碼]

現在,還有第二種方法可以啟動 Python Shell 直譯器,以下內容將解釋如何做到這一點。單擊開始->所有程式->Python2.7->Python IDLE,您將看到圖 4 中所示的 Shell 直譯器。

熟悉這些啟動 Python Shell 直譯器的方法,我會在需要的時候介紹啟動 Shell 直譯器的其他方法。這裡的 IDE 代表整合開發環境,它是 Python 程式設計正規化的圖形使用者介面 (GUI)。

顯然,有一些使用者將 Python Shell 直譯器用作計算器,但我仍然堅持使用我本地 Windows 機器上的計算器。

Just type in the following command in the Python shell interpreter and you will see the following result. For sake of understanding, the input commands to the shell interpreter are preceded with >>>, while the output from the shell interpreter is in “Burnt Sienna” color. 
   >>> print "Hello World!!!!"
   Hello World!!!!
   >>>

有一個重要的運算子是每個人都需要知道的,那就是“=”運算子,也稱為賦值運算子。可以使用賦值運算子將值賦給變數,Python 將根據賦給變數的值動態地對變數進行型別轉換。

以任何適合您平臺的方式啟動 Python 互動式 Shell,讓我們按照這裡所示的步驟深入瞭解。

示例 1. 互動式 Shell 中的第一步

   >>> print "Hello World, my first Python Program"
   Hello World, my first Python Program
   >>> 6+7
   13 
   >>> x = 4               
   >>> y = 6
   >>> x + y
   10

歡迎來到 Python 派對!讓我們像以前從未 Python 過一樣 Python。

2.3. Python 入門

[編輯 | 編輯原始碼]

還有另一種方法可以實現前面部分中所示的列印“Hello World”。將值“Hello World”儲存在一個變數中,然後列印該變數。列印語句的輸出將緊接在它們提供的輸出下方。

   >>> s="Hello World!"
   >>> s
   'Hello World!'
   >>> print (s)
   Hello World!
   >>> print s
   Hello World!

要編寫多行,只需在您要換行的地方新增“\n”字元。只需按照以下示例操作即可。

   >>> print "This is the first line\nAnd this is the second line"
   This is the first line
   And this is the second line
   >>> mystring="This is the first line\nAnd this is the second line"
   >>> mystring
   'This is the first line\nAnd this is the second line'
   >>> print mystring
   This is the first line
   And this is the second line

2.3.1. 列印變數

[編輯 | 編輯原始碼]

可以使用 print 命令列印變數及其型別。要列印變數,

   >>> x=3
   >>> x
   3
   >>> print x
   3
   >>> print (x)
   3
   >>> print (str(x))
   3
   >>> print type(x)
   <type 'int'>

上述列印語句側重於列印單個變數。但是,可以使用同一個 print 命令列印不同變數的組合。

   >>> x=5
   >>> y=7
   >>> print x
   5
   >>> print y
   7
   >>> print x,y
   5 7
   >>> print (x,y)
   (5, 7)

在列印整數變數之前,必須在使用字串變數一起列印時,使用 str 將其轉換為字串型別。

   >>> print str(x) + str(y)
   57
   >>> print str(x) + " " + str(y)
   5 7
   >>> print x+7
   12
   >>> print x+y
   12
   >>> print (x+y)
   12
   >>> print "the sum of %d and %d is %d" %(x,y,x+y)
   the sum of 5 and 7 is 12

2.3.2. 獲取文字輸入

[編輯 | 編輯原始碼]

可以使用可用的 input 命令從直譯器獲取輸入到 Shell 中。語法和以下語句突出顯示了 input 命令的使用。字串輸入必須用引號括起來。錯誤以紅色突出顯示。

   >>> input("what is the number")
   what is the number3
   3
   >>> input("whats my name!:")
   whats my name!:Some
   Traceback (most recent call last):
     File "<pyshell#52>", line 1, in <module>
       input("whats my name!:")
     File "<string>", line 1, in <module>
   NameError: name 'Some' is not defined
   >>> input("whats my name!:")
   whats my name!:"New Name"
   'New Name'
   >>> name=input("whats my name!:")
   whats my name!:"New Name"
   >>> print name
   New Name
   >>> print (name)
   New Name
   >>> print "My name is " + name
   My name is New Name
   Python typecasts the variable based on the input you provide the Python interpreter. 
   >>> print "My name is " + name
   My name is New Name
   >>> age=input("whats my name!:")
   whats my name!:34
   >>> print age
   34
   >>> print type(name)
   <type 'str'>
   >>> print type(age)
   <type 'int'>
   >>> print type(name), type(age)
   <type 'str'> <type 'int'>
   >>> print "The data type of "+ name +" and "+str(age)+"is "+ str(type(name)) + "and " + str(type(age))
   The data type of New Name and 34is <type 'str'>and <type 'int'>

但是,為了便於使用,您可以明確指示 Python 直譯器將輸入型別轉換為要使用變數的型別。如果您沒有明確提供型別轉換,Python 直譯器將完成型別轉換工作。

   >>> x=int(input("enter a integer number"))
   enter a integer number3
   >>> x
   3
   >>> x=float(input("enter a integer number"))
   enter a integer number4
   >>> print x
   4.0
   >>> x
   4.0
   >>> x=str(input("enter a integer number"))
   enter a integer number4
   >>> x
   '4'
   >>> x=input("enter a integer number")
   enter a integer number5
   >>> x
   5
   >>> print type(x)
   <type 'int'>

現在我們對 print 和 input 命令有了一定的瞭解,讓我們編寫一個簡單的 Python 程式,該程式將詢問您的姓名、年齡、地點和工作詳細資訊,並列印這些資訊。在 Python 直譯器中,我打開了一個新檔案,並編寫了以下程式碼片段,並將其儲存為 file.py。

   name=input("Enter your name: ")
   age=input("Enter a two digit number as age: ")
   location=input("Enter your current location: ")
   job=input("Enter your job details: ")
   print "Hi There!!! \nMy name is  %s,\nMy age is %d,\nI am located at %s and currently working as %s" %(name,age,location,job)

此外,我在 Python Shell 上運行了該程式,並獲得了第一個輸出。

   >>>  RESTART:C:/Users/Desktop/nameagelocation.py 
   Enter your name: "Suzaane"
   Enter a two digit number as age: 37
   Enter your current location: "Buffalo"
   Enter your job details: "Research Programmer"
   Hi There!!! 
   My name is  Suzaane,
   My age is 37,
   I am located at Buffalo and currently working as Research Programmer
   >>>

好了,您剛剛編寫了第一個 Python 程式,並在 Python 直譯器上運行了它。

華夏公益教科書