AI with Python - Getting Started



In this section, we will figure out how to get started with Python. We will also understand how Python helps for Artificial Intelligence.

Why Python for AI

Artificial intelligence is viewed as the the trending technology of the future. Already there are a number of applications made on it. Because of 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 different 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 per the following −

Simple syntax & less coding

Python involves very less coding and simple syntax among other programming languages which can be utilized for developing AI applications. Because of this feature, the testing can be simpler and we can focus more on programming.

Inbuilt libraries for AI projects

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 significant inbuilt libraries of Python.

  • Open source − Python is an open source programming language. This makes it broadly popular in the community.

  • Can be utilized for broad range of programming − Python can be utilized 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 is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It utilizes English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages. Python's features incorporate the following −

  • Easy-to-learn − Python has few keywords, basic structure, and a clearly defined syntax. This permits the student to pick up the language quickly.

  • Easy-to-read − Python code is more clearly characterized and visible to the eyes.

  • Easy-to-maintain − Python's source code is genuinely 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 permits interactive testing and debugging of snippets of code.

  • Portable − Python can run on a wide variety of hardware platforms and has the similar interface on all platforms.

  • Extendable − We can add low-level modules to the Python interpreter. These modules enable programmers to add to or modify their tools to be more efficient.

  • Databases − Python gives interfaces to all significant commercial databases.

  • GUI Programming − Python supports GUI applications that can be made and ported to many system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window system of Unix.

  • Scalable − Python gives a better structure and support for large programs than shell scripting.

Important features of Python

Let us now consider the following important features of Python −

  • It supports functional and organized programming methods as well as OOP.

  • It can be utilized as a scripting language or can be compiled to byte-code for building large applications.

  • It gives very high-level dynamic data types and supports dynamic type checking.

  • It supports automatic garbage collection.

  • It can be handily integrated with C, C++, COM, ActiveX, CORBA, and Java.

Installing Python

Python distribution is accessible for a large number of platforms. You have to download only the binary code applicable for your platform and install Python.

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 adaptability in terms of choice of features that you require in your installation.

Here is a quick overview of installing Python on various platforms −

Unix and Linux Installation

Below given steps to install Python on Unix/Linux machine.

  • Open a Web browser and go to https://www.python.org/downloads

  • Follow the link to download zipped source code accessible 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

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

Below given steps to install Python on Windows machine.

  • Open a Web browser and go to https://www.python.org/downloads

  • Follow the link for the Windows installer python-XYZ.msi file where XYZ is the version you have to install.

  • To utilize 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 see whether if your machine supports MSI.

  • Run the downloaded file. This brings up the Python install wizard, which is truly simple to use. Just accept the default settings and wait until the install is finished.

Macintosh Installation

If you are on Mac OS X, it is prescribed that you use Homebrew to install Python 3. It is a great package installer for Mac OS X and it is truly 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

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 looks for executables.

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 different programs.

The path variable is named as PATH in Unix or Path in Windows (Unix is case-sensitive; Windows isn't).

In Mac OS, the installer handles the path details. To invoke the Python interpreter from any particular directory, you should add the Python directory to your path.

Setting Path at Unix/Linux

To add the Python directory to the path for a specific 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 is the path of the Python directory.

Setting Path at Windows

To add the Python directory to the path for a specific session in Windows −

  • At the command prompt − type path %path%;C:Python and press Enter.

Note − C:Python is the path of the Python directory.

Running Python

Let us now observe the different ways to run Python. The ways are described below −

Interactive Interpreter

We can begin 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 search 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 beginning 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

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 − Be certain the file permission mode permits execution.

Integrated Development Environment

You can run Python from a GUI (Graphical User Interface) 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 site, downloadable as either MacBinary or BinHex'd files.

If you are not able to set up the environment appropriately, then you can take help from your system admin. Make sure the Python environment is appropriately set up and working entirely fine.

We can also use another Python platform called Anaconda. It incorporates 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/.

For this study notes we are using Python 3.6.3 version on MS Windows.





Input your Topic Name and press Enter.