跳转到内容

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://www.pythonlang.cn/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 解释器上运行了它。

华夏公益教科书