Artificial Intelligence With Python 简明教程
AI with Python – Getting Started
在本教程中,我们将学习如何开始使用 Python。我们也会理解 Python 如何帮助人工智能。
In this chapter, we will learn how to get started with Python. We will also understand how Python helps for Artificial Intelligence.
Why Python for AI
人工智能被认为是未来的趋势技术。已经有一些应用程序是基于人工智能。因此,许多公司和研究人员对此产生了兴趣。但这里出现的一个主要问题是,这些 AI 应用程序可以使用哪种编程语言来开发?有各种编程语言,比如 Lisp、Prolog、C++、Java和 Python,它们可以用于开发 AI 应用程序。在这些语言中,Python 编程语言获得了巨大的普及,原因如下:
Artificial intelligence is considered to be the trending technology of the future. Already there are a number of applications made on it. Due to this, many companies and researchers are taking interest in it. But the main question that arises here is that in which programming language can these AI applications be developed? There are various programming languages like Lisp, Prolog, C++, Java and Python, which can be used for developing applications of AI. Among them, Python programming language gains a huge popularity and the reasons are as follows −
Simple syntax & less coding
在用于开发 AI 应用程序的其他编程语言中,Python 的编码量非常少且语法简单。由于这一特点,测试可以更容易,并且我们可以在编程上投入更多的精力。
Python involves very less coding and simple syntax among other programming languages which can be used for developing AI applications. Due to this feature, the testing can be easier and we can focus more on programming.
Inbuilt libraries for AI projects
使用 Python 进行人工智能的一个主要优势在于它内置了大量的库。Python 几乎为各种类型的 AI 项目都备有库。例如, NumPy, SciPy, matplotlib, nltk, SimpleAI 是 Python 的一些重要的内置库。
A major advantage for using Python for AI is that it comes with inbuilt libraries. Python has libraries for almost all kinds of AI projects. For example, NumPy, SciPy, matplotlib, nltk, SimpleAI are some the important inbuilt libraries of Python.
-
Open source − Python is an open source programming language. This makes it widely popular in the community.
-
Can be used for broad range of programming − Python can be used for a broad range of programming tasks like small shell script to enterprise web applications. This is another reason Python is suitable for AI projects.
Features of Python
Python 是一种高级的、解释型的、交互式的、面向对象化的脚本语言。Python 被设计为高度可读。它经常使用英语关键词,而其他语言使用标点符号,且它的语法构造比其他语言更少。Python 的特性包括以下内容:
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages. Python’s features include the following −
-
Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language quickly.
-
Easy-to-read − Python code is more clearly defined and visible to the eyes.
-
Easy-to-maintain − Python’s source code is fairly easy-to-maintain.
-
A broad standard library − Python’s bulk of the library is very portable and cross-platform compatible on UNIX, Windows, and Macintosh.
-
Interactive Mode − Python has support for an interactive mode which allows interactive testing and debugging of snippets of code.
-
Portable − Python can run on a wide variety of hardware platforms and has the same interface on all platforms.
-
Extendable − We can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize their tools to be more efficient.
-
Databases − Python provides interfaces to all major commercial databases.
-
GUI Programming − Python supports GUI applications that can be created and ported to many system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window system of Unix.
-
Scalable − Python provides a better structure and support for large programs than shell scripting.
Important features of Python
让我们现在考虑 Python 的以下重要特性:
Let us now consider the following important features of Python −
-
It supports functional and structured programming methods as well as OOP.
-
It can be used as a scripting language or can be compiled to byte-code for building large applications.
-
It provides very high-level dynamic data types and supports dynamic type checking.
-
It supports automatic garbage collection.
-
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
Installing Python
Python 发行版本适用于大量平台。您只需要下载适用于您的平台的二进制代码然后安装 Python。
Python distribution is available for a large number of platforms. You need to download only the binary code applicable for your platform and install Python.
如果你的平台没有二进制代码,你需要 C 编译器来手动编译源代码。编译源代码在安装中所需特性的选择方面提供了更大的灵活性。
If the binary code for your platform is not available, you need a C compiler to compile the source code manually. Compiling the source code offers more flexibility in terms of choice of features that you require in your installation.
以下是对在各种平台上安装 Python 的快速概述 −
Here is a quick overview of installing Python on various platforms −
Unix and Linux Installation
按照以下步骤在 Unix/Linux 机器上安装 Python。
Follow these steps to install Python on Unix/Linux machine.
-
Open a Web browser and go to [role="bare"]https://www.python.org/downloadshttps://www.python.org/downloads
-
Follow the link to download zipped source code available for Unix/Linux.
-
Download and extract files.
-
Editing the Modules/Setup file if you want to customize some options.
-
run ./configure script
-
make
-
make install
这会将 Python 安装在标准位置 /usr/local/bin 及其库的安装位置 /usr/local/lib/pythonXX,其中 XX 是 Python 的版本。
This installs Python at the standard location /usr/local/bin and its libraries at /usr/local/lib/pythonXX where XX is the version of Python.
Windows Installation
按照以下步骤在 Windows 机器上安装 Python。
Follow these steps to install Python on Windows machine.
-
Open a Web browser and go to [role="bare"]https://www.python.org/downloadshttps://www.python.org/downloads
-
Follow the link for the Windows installer python-XYZ.msi file where XYZ is the version you need to install.
-
To use this installer python-XYZ.msi, the Windows system must support Microsoft Installer 2.0. Save the installer file to your local machine and then run it to find out if your machine supports MSI.
-
Run the downloaded file. This brings up the Python install wizard, which is really easy to use. Just accept the default settings and wait until the install is finished.
Macintosh Installation
如果您使用 Mac OS X,建议您使用 Homebrew 来安装 Python 3。它是一个非常棒的 Mac OS X 软件包安装程序,且非常容易使用。如果您没有 Homebrew,可以使用以下命令来安装它:
If you are on Mac OS X, it is recommended that you use Homebrew to install Python 3. It is a great package installer for Mac OS X and it is really easy to use. If you don’t have Homebrew, you can install it using the following command −
$ ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew/install/master/install)"
我们可以使用以下命令更新软件包管理器:
We can update the package manager with the command below −
$ brew update
现在运行以下命令在你的系统上安装 Python3 -
Now run the following command to install Python3 on your system −
$ brew install python3
Setting up PATH
程序和其他可执行文件可以位于许多目录中,因此操作系统会提供一个搜索路径,其中列出了操作系统搜索可执行文件的目录。
Programs and other executable files can be in many directories, so operating systems provide a search path that lists the directories that the OS searches for executables.
路径存储在环境变量中,该变量是由操作系统维护的一个已命名字符串。此变量包含可供命令 shell 和其他程序使用的信息。
The path is stored in an environment variable, which is a named string maintained by the operating system. This variable contains information available to the command shell and other programs.
路径变量在 Unix 中命名为 PATH 或在 Windows 中命名为 Path(Unix 区分大小写;Windows 不区分)。
The path variable is named as PATH in Unix or Path in Windows (Unix is case-sensitive; Windows is not).
在 Mac OS 中,安装程序会处理路径详细信息。要从任何特定目录调用 Python 解释器,您必须将 Python 目录添加到您的路径中。
In Mac OS, the installer handles the path details. To invoke the Python interpreter from any particular directory, you must add the Python directory to your path.
Setting Path at Unix/Linux
要在 Unix 中为特定会话将 Python 目录添加到路径中 −
To add the Python directory to the path for a particular session in Unix −
-
In the csh shell Type setenv PATH "$PATH:/usr/local/bin/python" and press Enter.
-
In the bash shell (Linux) Type export ATH = "$PATH:/usr/local/bin/python" and press Enter.
-
In the sh or ksh shell Type PATH = "$PATH:/usr/local/bin/python" and press Enter.
Note - /usr/local/bin/python 是 Python 目录的路径。
Note − /usr/local/bin/python is the path of the Python directory.
Running Python
让我们现在看看运行 Python 的不同方法。这些方法如下所述 -
Let us now see the different ways to run Python. The ways are described below −
Interactive Interpreter
我们可以从 Unix、DOS 或任何其他为你提供命令行解释器或 shell 窗口的系统启动 Python。
We can start Python from Unix, DOS, or any other system that provides you a command-line interpreter or shell window.
-
Enter python at the command line.
-
Start coding right away in the interactive interpreter.
$python # Unix/Linux
或
or
python% # Unix/Linux
或
or
C:> python # Windows/DOS
以下是所有可用命令行选项的列表:
Here is the list of all the available command line options −
S.No. |
Option & Description |
1 |
-d It provides debug output. |
2 |
-o It generates optimized bytecode (resulting in .pyo files). |
3 |
-S Do not run import site to look for Python paths on startup. |
4 |
-v Verbose output (detailed trace on import statements). |
5 |
-x Disables class-based built-in exceptions (just use strings); obsolete starting with version 1.6. |
6 |
-c cmd Runs Python script sent in as cmd string. |
7 |
File Run Python script from given file. |
Script from the Command-line
Python 脚本可以在命令行执行,方法是在你的应用程序上调用解释器,如下所示 -
A Python script can be executed at the command line by invoking the interpreter on your application, as in the following −
$python script.py # Unix/Linux
或,
or,
python% script.py # Unix/Linux
或,
or,
C:> python script.py # Windows/DOS
Note − 确保文件权限模式允许执行。
Note − Be sure the file permission mode allows execution.
Integrated Development Environment
如果系统上的 GUI 应用程序支持 Python,您还可以从图形用户界面 (GUI) 环境中运行 Python。
You can run Python from a Graphical User Interface (GUI) environment as well, if you have a GUI application on your system that supports Python.
-
Unix − IDLE is the very first Unix IDE for Python.
-
Windows − PythonWin is the first Windows interface for Python and is an IDE with a GUI.
-
Macintosh − The Macintosh version of Python along with the IDLE IDE is available from the main website, downloadable as either MacBinary or BinHex’d files.
如果您无法正确设置环境,则可以向系统管理员寻求帮助。确保 Python 环境已正确设置,并且运行正常。
If you are not able to set up the environment properly, then you can take help from your system admin. Make sure the Python environment is properly set up and working perfectly fine.
我们还可以使用另一个名为 Anaconda 的 Python 平台。它包括数百个流行的数据科学包以及适用于 Windows、Linux 和 MacOS 的 conda 包和虚拟环境管理器。你可以从链接 https://www.anaconda.com/download/ 根据你的操作系统下载它。
We can also use another Python platform called Anaconda. It includes hundreds of popular data science packages and the conda package and virtual environment manager for Windows, Linux and MacOS. You can download it as per your operating system from the link https://www.anaconda.com/download/.
对于本教程,我们在 Windows MS 中使用 Python 3.6.3 版本。
For this tutorial we are using Python 3.6.3 version on MS Windows.