跳轉到內容

Python 程式設計入門/安裝 Python

來自華夏公益教科書,開放的書籍,面向開放的世界
   Here in this chapter, I will tell you about installing Python on your local machine. There are two versions of Python available from the main Python website, www.python.com namely the Python 2.7.XXXX and Python 3.0.XXX. I prefer to use, teach Python 2.7.XXX versions. In this book, I will be talking, and writing about Python 2.7.XXX. What’s the difference, one may questions. There are differences between the two versions with syntax and a few other minor changes. 
   Coming to the question, which Python is right for you?
   Python like any other open source software is a constantly evolving where the users from all over the world are contributing, making and updating features. There is a board of members that vote on a particular feature and decide whether the feature needs to be integrated or not. The documentation that acts as the supporting is constantly evolving at the same time. So decide on which one you want to use depending on your preferences and evaluations. 
   That said, it’s more than enough that you know one of these either versions, knowing one will make it a breeze to know the other versions. Just keep that in mind, and start talking Python. 
   After deciding which version of Python to learn, the first thing you need to do with Python is install it. 

'安裝 Python 版本'

   After you have decided which version of Python to learn, you need to install it. Linux and Mac OSX machines come with preinstalled Python packages so you need not worry about installing. Run the command terminal and type in the keyword python, and the machine will tell you if you have Python installed or not. Go figure that, Good luck with that!
   Since I have only a windows machine, I will guide you through installing Python on windows machine. Go figure 
   As a matter of fact, Windows does not come with Python preinstalled. Go ahead and download the version of Python software that you want to use from the Python official website, www.python.org. 
   Installing Python from Python.org (Python website link here)
  1. 透過訪問 https://python.club.tw/ftp/python/ 下載最新的 Python Windows 安裝程式,選擇列出的最高版本號,然後下載 .exe 安裝程式。
  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.
   >>> 
   I am assuming that you have the standard Python interpreter on the machine that you are currently planning to work on with.

'互動式 Shell'

   Once you have Python installed, there are different ways to run Python programs. On the shell interpreter, you can run commands and test how the flow of code goes. You can save the code that you have written and then run the file to obtain the output for the code that you have written. 
   Here, I will explain how to run simple code snippets on the Python interpreter. 
   You can fire up the Python shell interpreter in any of the following ways.

方法 1

   Run the command interpreter by pressing the windows and the letter R. You will see a window open up like this.
   Type in the letters cmd in the text box and you will see a screen like the one shown below in 
Windows 7 命令提示符 2016 年 5 月 11 日

圖 2.

   Once at the command prompt, change into the directory where the Python executable resides. For example, on my local machine, I have the Python executable residing on C:\Python27\. So, I will go ahead and change into that particular folder by typing “cd C:\Python27” on the command prompt. Here’s what I see on my local machine. Once inside the folder Python27, type in python at the command prompt and voila! You’ll see the Python shell interpreter as illustrated in 
Windows 7 Python Shell 直譯器提示 2016 年 5 月 11 日

圖 3.

方法 2

   Now there is a second way on firing the Python shell interpreter and the following will explain how to do the same.

單擊開始->所有程式->Python2.7->Python IDLE,您將看到以下所示的 shell 直譯器,如

Windows 7 Python IDLE Shell 直譯器提示 2016 年 5 月 11 日

圖 4.

   Feel comfortable with these methods of firing up Python shell interpreter, I will talk about different other methods of firing the shell interpreter as and when the situation rises for the occasion. Here IDE stands for Integrated Development Editor, this is the Graphical User Interface (GUI) of the Python programming paradigm.

顯然,有些使用者將 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!!!!
   There is one important operator that anyone needs to know which is the “=” operator also called the assignment operator. Variables can be assigned values using the assignment operator and Python will dynamically typecast the variable based on the value assigned to the variable.
   Launch the Python interactive shell in whatever way works on your platform, and let's dive in with the steps shown here:

示例 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。

用 Python 啟動

還有另一種方法可以實現與前面部分中所示的“Hello World”列印類似的效果。將值“Hello World”儲存在變數中,然後列印變數。列印語句的輸出緊隨其下方提供的輸出。

   >>> s="Hello World!"
   >>> s
   'Hello World!'
   >>> print (s)
   Hello World!
   >>> print s
   Hello World!
   To write multiple lines, just add ‘\n’ character where you want to introduce the new line. Just follow the following example.
   >>> 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
   >>> print (mystring)
   This is the first line
   And this is the second line

列印變數

   Variables can be printed in their forms and also the types they are cast into can also be printed using the print command. To print variables, 
   >>> x=3
   >>> x
   3
   >>> print x
   3
   >>> print (x)
   3
   >>> print (str(x))
   3
   >>> print type(x)
   <type 'int'>

上面的列印語句側重於列印單個變數。但是,可以使用同一個列印命令列印不同變數的組合。 >>> 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

獲取文字輸入

從直譯器獲取到 shell 的輸入是使用可用的 input 命令完成的。語法和以下語句突出顯示了 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 根據您提供的 Python 直譯器的輸入型別轉換變數。

>>> 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/PyladiesBangaloreFoundation/PythonProgramming/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 直譯器上運行了它。

華夏公益教科書