Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Getting Started with Python!

Python is a popular programming language that is reliable, flexible, easy to learn, free to use on all operating systems, and supported by both a strong developer community and many free libraries (Libraries are pre-written codes that developers use to solve common programming tasks).

Python supports all manners of development, including web applications, web services, desktop apps, scripting, data science, scientific computing, and Jupyter notebooks. Python is a language used by many universities, scientists, casual developers, and professional developers alike.

Python is a high level programming Language. This simply means that the commands used in Python are understandable by humans. These commands are then converted into machine code by using an interpreter.

You can learn more about the language on Python Official Site

Why use Python for learning AI ?

Python is more often than not the top most choice for Machine Learning Applications. The main reason for this is the amount of simplicity, consistency and stability Python code can bring to AI workflows. The simplest of AI workflows require extensive mathematical functions running in the background. The wide variety of stable and user friendly libraries like Scikit-Learn (used for handling basic Machine Learning algorithms like clustering, linear and logistic regressions, etc.), Matplotlib (used for creating bar charts, histograms and other data visualizations) etc provide great support for building scalable and efficient code. We shall cover more on these in the later sessions.

Another reason why python is easy to use is that you do not need to declare variable types unlike other programming languages. A variable is the name you give to a value you want to store and use later.

For example, let’s say you would like to create a variable called money to store some numerical value of money, in Python you would only need to assign a value to the word ‘money’ to use it, in other programming languages, this requires a variable type declaration before use.

Now that we have understood why we have chosen Python as our programming language, let’s get started with basic commands we will come across frequently in our AI journey!

Comments

Many of the examples in this notebook include comments. Comments in Python start with the hash character, #, and extend to the end of the physical line. A comment may appear at the start of a line or following whitespace or code, but not within the quotation marks “” or ‘’ of a String itself. A hash character between quotation marks is just a hash character. Since comments are to clarify code and are not interpreted by Python, they may be omitted when typing in examples.

You can learn more about the comments in python on Python Official Site

Data Types in Python

Although we do not have to declare a type for python variables, a value does have a type. These are data types we will be covering in this Notebook:

Boolean

Numbers

Strings

Lists

Tuples

Dictionary

You can learn more about the data types in python on Python Official Site

BOOLEAN
Numbers

The Python interpreter can act as a simple calculator: type an expression at it outputs the value.

Expression syntax is straightforward: the operators +, -, * and / work just like in most other programming languages (such as Pascal or C); parentheses (()) can be used for grouping and order or precedence. For example:

A floating-point number, or simply a float, is a data type that represents decimal numbers with a fractional part. Floats are used to represent real numbers, including numbers with decimal points, such as 3.14 or -2.5.

Arithmetic Operations

We then add these two numbers using the + operator and store the result in a variable called sum

Division (/) always returns a float. To do floor division and get an integer result (discarding any fractional result) you can use the // operator; to calculate the remainder you can use %:

Use the ** operator to calculate powers:

The equal sign (=) is used for assigning values to a variable

STRING

Single (’ ') or double (" ") quotes are used to separate a group of characters into a string. One of the fundamental data types in Python, strings are used to represent text.

LIST

A list is an ordered sequence of items, meaning items in the list are stored in the order of which it was input. Remember, a list may contain different types of items. To define a list, you must put values separated with commas in square brackets.

Lists are mutable(can be changed or updated) unlike Strings. Like strings (and all other built-in sequence types), lists can be indexed and sliced:

Lists also support concatenation with the + operator:

As mentioned at the start, unlike strings, which are immutable, lists are a mutable type, which means you can change any value in the list:

Use the list’s append() method to add new items to the end of the list:

Tuples

A tuple is an ordered sequence of items. You might be wondering now then how is it different from lists?

The main differences between tuples and lists are :

Tuples cannot be modified element wise like lists (Objects which cannot be modified in such a manner are described as immutable objects. Tuples use parentheses, whereas lists use square brackets.

SET

Sets are used to store multiple items in a single variable.A set is a collection which is unordered, unchangeable*, and unindexed.

True and 1 is considered the same value

Dictionary

Dictionary is an unordered collection of key-value pairs.

In Python, dictionaries are defined within braces {} with each item being a pair in the form key:value. Key and value can be of any type.

A Dictionary in Python works similar to the Dictionary in a real world. Keys of a Dictionary must be unique and of immutable data type such as Strings, Integers and tuples, but the key-values can be repeated and be of any type.

You might want to use a Dictionary over Tuples as it is quicker to access data if the data source is substantially large as finding data in Tuples requires looping which we will go through later

Flow Control in Python

In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There may be a situation when you need to execute a block of code several number of times.

Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times.

For more infortmation click here

IF-ELSE STATEMENT

Depending on whether a condition is True or False, you can run several blocks of code using the if-else statement, which is a conditional statement. It gives you a mechanism to base judgements in your code on specific circumstances.

LOOP STATEMENTS

While Loop

A while loop in python iterates till its condition becomes False. In other words, it executes the statements under itself while the condition it takes is True. When the program control reaches the while loop, the condition is checked. If the condition is true, the block of code under it is executed. Remember to indent all statements under the loop equally. After that, the condition is checked again. This continues until the condition becomes false. Then, the first statement, if any, after the loop is executed.

For Loop

A for loop does the same thing as the example in the while loop, but makes it simpler to write and understand.

Functions in Python

In programming we often come across situations where a particular block of code needs to be repeated or reused. Thus the concept of functions comes into play. A function is basically a block of code which performs a specific task.

Functions make the entire code look more organised and manageable. They allow reusability of code and organise the program into small modular chunks. For more information click here

Write a function in Python to convert celsius Scale to Fahrenheit Scale

NumPy

NumPy arrays in python are basically grids of values of the same data type. Each element of a NumPy array has a non-negative index associated with it. When dealing with arrays, we come across a useful term called shape which gives the size of the array along each dimension. For more information click here

Array Indexing
NumPy DataTypes
Array Mathematics
Sorting NumPy Arrays
Manipulating NumPy Arrays

Pandas

Data Frame

A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns.

DataFrames accepts many different kinds of inputs, if you would like to learn more, feel free to search online or go to pandas’ documentation. For the purpose of this example, we shall demonstrate a useful way you can use DataFrames.For information click here

Load Files Into a DataFrame If your data sets are stored in a file, Pandas can load them into a DataFrame.

matplotlib

Matplotlib is a Python 2D plotting library that we can use to produce high quality data visualization. It is highly usable (as you will soon find out), you can create simple and complex graphs with just a few lines of codes!

For more information click here

Scatter plot

Scatter plots use a collection of points on a graph to display values from two variables. This allow us to see if there is any relationship or correlation between the two variables.

These are some basic examples of how matplotlib can be used, the plots, labels, etc can be manipulated and adjusted to be displayed however you would like using functionality provided by matplotlib.

Do you know of any more graphs? Name them below! Go online and look for the types of graphs matplotlib offers

matplotlib’s tutorial website

Scikit Learn

scikit-learn is an open source machine learning library that supports supervised and unsupervised learning. It also provides various tools for model fitting, data preprocessing, model selection and evaluation, and many other utilities.

scikit-learn.org

Here we will walk through the basics of how to get started with scikit learn and some terminologies you will be coming across in the later sessions. Give the codes below a run through! Don’t worry if you do not understand the exact workings of scikit-learn or a machine learning workflow at present, these codes below just demonstrate everything you have come across in the notebooks so far! In the later sessions we will go through the process together step by step in more detail. For more information click here

Dataset loading from Sklearn