Artificial Intelligence with Python Getting Started
Why Python for AI?
Python has a standard library in development, and a few for AI. It has an intuitive syntax, basic control flow, and data structures. It also supports interpretive run-time, without standard compiler languages. This makes Python especially useful for prototyping algorithms for AI.
Python programming language gains a huge popularity and the reasons are as follows −
Simple syntax & less coding
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
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
- Easy to learn — Python's syntax is simple and includes English words that make it easy to read and understand. Beginners can learn a lot just by looking at existing code.
- Fast development cycle — developers can write Python apps very quickly. This is ideal for teams needing to do a lot of prototyping before settling on a final design.
- Extensive standard library — this standard library is constantly evolving with modules for regular expression matching, standard mathematical functions, threads, operating systems interfaces, network programming, email handling, HTML parsing, and more.
- Extensive list of third party modules — most of these are open source, and include web frameworks, DB interfaces, GUI toolkits, and more.
- Scales well — basic Python applications can be scaled up to complex applications quite easily.
- Relatively concise — a lot can be achieved with a small amount of code.
- Versatile — Python can be used in almost any type of application.
- Active community — Python has a large community of fellow developers writing software modules and providing help to newbies and other developers.
- Python is Interactive — You can actually sit at a Python prompt and interact with the interpreter directly to write your programs.
- Enhanced Readability — It provides enhanced readability. For that purpose, uniform indents are used to delimit blocks of statements instead of curly brackets, like in many languages such as C, C++ and Java.
- Standard distribution — Standard distribution of Python contains the Tkinter GUI toolkit, which is the implementation of popular GUI library called Tcl/Tk. An attractive GUI can be constructed using Tkinter. Many other GUI libraries like Qt, GTK, WxWidgets etc. are also ported to Python.
- Standard DB-API — A standard DB-API for database connectivity has been defined in Python. It can be enabled using any data source (Oracle, MySQL, SQLite etc.) as a backend to the Python program for storage, retrieval and processing of data.
- Integrated — It can be integrated with other popular programming technologies like C, C++, Java, ActiveX and CORBA.
There are many ways to download and install Python. This can depend on your operating system, and whether you use a package manager or not.
And if you're running a Linux or Mac machine, you may already have Python installed. However, it's a good idea to install the most recent version (see below for more info about this).
To keep things simple, here are the basic steps involved in downloading and installing Python (without using a package manager). If you prefer to use a package manager, skip down to Installation with a Package Manager.
You can download Python from the official Python download page.
Choose the latest version (3.x) unless you have a reason to use an earlier version. This tutorial uses version 3.6.1.
The exact steps here will depend on your operating system.
Windows and macOS
In Windows and macOS, double-click on the installer and follow the prompts.
In Linux, you can use the terminal to extract the installation files and install Python. Something like this:
- Untar the .tgz file:
- Change to the directory to where the files were untarred to. For example:
- Install Python:
- Untar the .tgz file:
Check your Installation
To check that Python 3 was installed, open your terminal (or command prompt on Windows), and type the following:
If this doesn't work, try it without the 3:
Anyway, running this code will launch Python. So if Python 3 was successfully installed, you'll see something like this:
Python has now been installed, and it is now running on your machine. You can now start writing Python programs immediately!
Installation with a Package Manager
Some developers use a package manager to download and install their software. If your system has a package manager, you can use it to install Python instead of using the above steps.
A package manager enables you to download and install Python with one or two commands. For example, on a Mac you can install Python using Homebrew using the following commands (assumes you have Homebrew installed):
On Linux machines the exact commands you use will depend on your Linux flavor and its package manager.
Here's the command you can use with Ubuntu 16.10 and later:
Of course, you'll need to ensure you're installing the most recent version of Python.
Isn't Python Preinstalled?
Some operating systems come preinstalled with Python. In particular, most Macs and Linux machines will already have some version of Python installed.
However, even if your computer already has Python, you might want to install the latest copy before you continue with this tutorial. In particular, if you have Python 2 installed, you might want to install Python 3.
The reason I say that is, when Python version 3 came along, it was significantly different to its previous version. It was basically re-written to address many issues that had been identified in version 2 over the years. As a result, some code written using Python 2 won't work in Python 3.
When I refer to Python 2 and 3, I mean any version of those major releases (eg, Python 2.7, Python 3.6.1, etc).