Skip to content

AI with Python – Getting Started

Why Python for AI

Python is a popular programming language for AI due to its simple syntax, short coding, inbuilt libraries, open source nature, and its ability to be used for a broad range of programming tasks. This makes it an ideal choice for developing AI applications, as it has the necessary tools and features to enable developers to quickly and easily create AI applications. Among them, Python programming language gains a huge popularity and the reasons are as follows.

Simple syntax & less coding

Python has a simple and concise syntax, allowing developers to write fewer lines of code than other programming languages. This makes testing easier, and allows developers to focus more on programming and AI projects.


Inbuilt Libraries Python comes with inbuilt libraries that are useful for AI projects, such as NumPy, SciPy, matplotlib, nltk, and SimpleAI. These libraries provide a wide range of tools that make coding for AI projects easier.


Open Source Python is an open source programming language, which means that it is freely available for anyone to use and modify. This makes it popular among developers and makes it easy for developers to collaborate on AI projects.


Broad Range of Programming Python can be used for a variety of programming tasks, from small shell scripts to enterprise web applications. This makes it a suitable choice for AI projects, as it can be used for a wide range of applications.

Features of Python

Easy-to-Learn

Python is an easy language to learn as it has few keywords, a simple structure, and a clearly defined syntax. This makes it easy for beginners to pick up the language quickly.

Easy-to-Read

Python code is easy to read, as it is written using English keywords. It has fewer syntactical constructions than other languages, making the code clearer and easier to understand.

Easy-to-Maintain
Python’s source code is fairly easy to maintain, making it a good choice for long term projects.

Broad Standard Library

The Python library is portable and compatible on many platforms, such as UNIX, Windows, and Macintosh. This makes it a great choice for developing applications that will be used across different systems.

Interactive Mode

Python supports an interactive mode, which allows users to test and debug snippets of code quickly. This makes it easy to develop applications on the fly.

Portable

Python is a portable language, meaning that the same code can be run on a variety of hardware platforms. This makes it easy to deploy applications on different systems.

Extendable

Python can be extended by adding low-level modules to the Python interpreter. This allows programmers to customize their tools and make them more efficient.

Databases

Python provides interfaces to many commercial databases, making it easy to access data from a variety of sources.

GUI Programming

Python supports GUI applications that can be created and ported to many different system calls, libraries, and window systems, such as Windows MFC, Macintosh, and the X Window system of Unix.

Scalable

Python is a more scalable language than shell scripting, making it better suited for large applications. This makes it a great choice for developing complex applications.

Important features of Python

Functional and Structured Programming: Python supports both functional and structured programming methods as well as object-oriented programming. This makes it a versatile language that can be used for a variety of tasks.


Scripting Language: Python can be used as a scripting language, allowing users to quickly develop applications without having to compile the code.

Dynamic Data Types: Python provides very high-level dynamic data types, making it easy to store and manipulate data. It also supports dynamic type checking, which ensures that the data is valid before it is used.

Automatic Garbage Collection: Python supports automatic garbage collection, which removes unused objects from memory to free up space. This makes it more efficient and helps ensure that applications run smoothly.

Integration: Python can be easily integrated with other languages such as C, C++, COM, ActiveX, CORBA, and Java. This makes it a great choice for developing applications that need to interact with other languages.

Python Installation

Python distributions are available for a variety of platforms. To install Python, you need to download the binary code that is applicable to your platform. If the binary code is not available, you will need a C compiler to compile the source code manually in order to customize the features of your installation.

Unix and Linux Installation

To install Python on a Unix/Linux machine, the following steps should be taken: (1) open a web browser and go to https://www.python.org/downloads; (2) download and extract the zipped source code available for Unix/Linux; (3) edit the Modules/Setup file if you want to customize some options; (4) run the ./configure script; (5) make; and (6) make install. After installation, Python will be located at /usr/local/bin, and its libraries at /usr/local/lib/pythonXX, where XX is the version of Python.

Windows Installation

To install Python on a Windows machine, the following steps should be taken: (1) open a web browser and go to https://www.python.org/downloads; (2) follow the link for the Windows installer python-XYZ.msi file where XYZ is the version you need to install; (3) to use the installer, the Windows system must support Microsoft Installer 2.0; (4) save the installer file to your local machine and then run it to check if your machine supports MSI; and (5) run the downloaded file to open the Python install wizard. Accept the default settings and wait until the installation is complete.

Macintosh Installation

Follow these steps to install Python on Mac. Open a Web browser and go to https://www.python.org/downloads Follow the link for the Mac OS X installer python-XYZ-macosx10.3.dmg file where XYZ is the version you need to install. Double-click the file and it will be mounted as a disk image on your desktop. Open the disk image and double-click the Python.mpkg file. Follow the instructions given by the installer.

This command installs Homebrew, a package manager for MacOS. Homebrew simplifies the installation, upgrade, and management of software packages on MacOS. It is the recommended way to install and manage packages on MacOS, and can be done using the following command:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"


This command updates Homebrew, the package manager for MacOS. It will check for any new versions of packages and their dependencies that are available, download them, and install them. It will also notify you if any packages need to be upgraded or uninstalled. The alternate command for this is:

$ brew update

This command installs Python 3 using Homebrew, the package manager for MacOS. The alternate command for this is:

$ brew install python3

Setting up PATH

The PATH environment variable is used by operating systems to store a list of directories that the OS will search for executable files. This variable is named PATH in Unix and Path in Windows (Unix is case-sensitive; Windows is not). The path is stored in an environment variable, which is a named string maintained by the operating system and can be accessed by the command shell and other programs. In Mac OS, the installer automatically sets up the path. To make sure the Python interpreter can be invoked from any directory, the Python directory must be added to the path.

Setting Path at Unix/Linux

– PATH is an environment variable in operating systems used to store a list of directories for searching executable files. – In Unix, the variable is named PATH and in Windows, it is named Path. – In Mac OS, the installer automatically sets up the path. – To add the Python directory to the path for a particular session in Unix, type setenv PATH “$PATH:/usr/local/bin/python” (csh shell), export ATH = “$PATH:/usr/local/bin/python” (bash shell), or PATH = “$PATH:/usr/local/bin/python” (sh or ksh shell). – /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 particular session in Windows, type path %path%;C:Python at the command prompt. – C:Python is the path of the Python directory.

Interactive Interpreter

An interactive interpreter for Python is a programming environment that allows users to write, execute and test code snippets in an interactive manner. It provides a command-line environment where users can type in code, and the interpreter will immediately execute it and show the output. It can also be used to debug code and to explore the Python language. The interpreter is often used by beginners to learn the language and by experienced developers to quickly try out ideas.

To use Python’s interactive interpreter, one can start the program from Unix, DOS, or any other system that provides a command-line interpreter or shell window. Once the program has been started, one can begin coding right away in the interpreter by simply typing in the code. The interpreter will then immediately execute the code and show the output. This is a great way for beginners to learn the language as well as for experienced developers to quickly try out ideas.

$python # Unix/Linux

This command will start the Python interpreter. It will open a command-line environment where you can type in code and the interpreter will immediately execute it and show the output.

C:> python # Windows/DOS

This command will start the Python interpreter. It will open a command-line environment where you can type in code and the interpreter will immediately execute it and show the output.

Following is the list available command line

Command Description
$python Start the Python interpreter
-d Provide debug output
-o Generate optimized bytecode (resulting in .pyo files)
-S Do not run import site to look for Python paths on startup
-v Verbose output (detailed trace on import statements)
-x Disables class-based built-in exceptions (just use strings); obsolete starting with version 1.6.
-c cmd Runs Python script sent in as cmd string
File Run Python script from given file

Script from the Command-line

A Python script can be run from the command line by typing “python script.py” (for Unix/Linux) or “python% script.py” (for Unix/Linux) or “C:> python script.py” (for Windows/DOS). Make sure the file has the right permissions to be executed.

Integrated Development Environment

Unix – IDLE is the first Integrated Development Environment (IDE) for Python on Unix. It is designed to be simple and intuitive to use, providing a graphical user interface to the Python programming language.

Windows – PythonWin is the first Windows IDE for Python. It provides a graphical user interface to the Python programming language. It features syntax highlighting, auto indentation, code completion, and other features to make coding easier and more efficient.

Macintosh – The Macintosh version of Python along with the IDLE IDE is available from the main website. It can be downloaded in either MacBinary or BinHex’d format. The IDE includes syntax highlighting, auto indentation, code completion, and other features to make coding easier and more efficient.

Anaconda is another Python platform which includes hundreds of popular data science packages and the conda package and virtual environment manager for Windows, Linux and MacOS. It is available for download on the link https://www.anaconda.com/download/.

For this tutorial, we are using Python 3.6.3 version on MS Windows.

Leave a Reply

Your email address will not be published. Required fields are marked *