# What is Python?

Today being a very first day of my study for computer language I would like to ensure everybody whoever wants to lean coding in computer languages that coding is easy , provided you should identify the techniques and pattern of a given command,rest the software will proceed with the entered command.

Being a beginner in data science you should first know the different types of software and the main purpose to use them.With purpose i mean why that particular software is used to manage your data?

Python is easy (for the beginners also) programming language with a general purpose to build anything.Its open source and free.Nowadays there are 2 versions are available Version 2 and Versions 3. Since Version 3 is relatively new,we will continue with this version only.

**Where to Download Version 3 of Python: https://www.python.org/downloads/**

## Python Shell

Pthon shell is place where you can type Python code and immediately see the results.Python Script is place where you write the Python command and then you enter “Run the solution”.You see the results (output) in Python Shell along with your input command

### Python as a “calculator”

What is **4+3? ; 10 /2? ; 5-3? ; 2*6; 4 ^5? in Python**

**Commands in Python**

- print(4+3)
- print(10/2)
- print(5-3)
- print(2*6)
- print(4**5)

Note: print here is a clause that needs to be entered in the Python Script to see your output in the Python Shell

**Variables in Python**

Suppose I have stored a number to a variable such that **Y= 2**

In Python you can get the number simply by setting the command

**INPUT: print(Y)**

**OUTPUT: 2**

Now Suppose X=3 then to get the Product of 2 and 3 put the following input command in the Python Script:

**INPUT: print(Y*X)**

**OUTPUT: 6**

**Data Types In Python**

There are main 4 datatypes in Python

- String – str()
- Boolian-bool()
- Float – float()
- Integer-int()

Strings can be a type to represent text. You can use single or double quotes to build a string.

Boolean can be a type to represent logical values. Can only be True Or False.

Float is a number that has both an integer and fractional part, separated by a point.

Integer is a number without a fractional part.

**Note: **

- Determining the type of the variable can be done by running
**type(variable) command in Python Script. Python Shell**will tell whether the variable is string or boolian or float or integer. - You cannot solve variable with different data types,you have to convert them in to single data type. For example X=”interest” and Y=20 .Y is in integer form and X in string .You have to convert integer in to string to solve for X*Y. Commands are as follows:
**INPUT**: X*str(Y).

## List in Python

A list stores elements one after another.

**for example : areas=[hall,kit,liv,bed,bath]**

**hall = 11.25**

**kit = 18.0**

**liv = 20.0**

**bed = 10.75**

**bath = 9.50**

Set the input as:

**INPUT : print(areas)**

**OUTPUT: [11.25, 18.0, 20.0, 10.75, 9.5]**

A list can contain any Python type. Although it’s not really common, a list can also contain a mix of Python types including strings, floats, booleans, etc.

In the above example suppose the given list:areas contain Python Type – String,then to create a list with its string following command is entered:

**INPUT:areas = [“hallway”,hall, “kitchen”,kit, “living room”, liv,”bedroom”,bed, “bathroom”, bath]**

**print(areas)**

**OUTPUT: [‘hallway’, 11.25, ‘kitchen’, 18.0, ‘living room’, 20.0, ‘bedroom’, 10.75, ‘bathroom’, 9.5]**

Instead of creating a flat list containing strings and floats, representing the names and areas of the rooms in your house, you can create a list of lists.for example:

**INPUT:house = [[“hallway”, hall], [“kitchen”, kit], [“living room”, liv],[“bedroom”,bed], [“bathroom”,bath]]**

**print(house)**

**OUTPUT:[[‘hallway’, 11.25], [‘kitchen’, 18.0], [‘living room’, 20.0], [‘bedroom’, 10.75], [‘bathroom’, 9.5]**

**SUBSETTING IN PYTHON**

Lets see this with our previous example

**0 1 2 3 4 5 6 7**

**areas = [“hallway”, 11.25, “kitchen”, 18.0, “living room”, 20.0, “bedroom”, 10.75,**

** ****8** ** 9**

**“bathroom”, 9.50]**

**OR** -10** -9 – 8 – 7 -6 -5 -4 -3**

**areas = [“hallway”, 11.25, “kitchen”, 18.0, “living room”, 20.0, “bedroom”, 10.75,**

** -2 -1**

**“bathroom”, 9.50]**

**INPUT: print(areas[1])**

** print(areas[-1])**

** print(areas[5])**

**OUTPUT: 11.25**

** 9.5**

** 20.0**

##### Inversely, to know the index of an element in the list use print(areas.index(11.25)) ,the output shows 1 .

### Slicing and dicing

Selecting single values from a list is just one part of the story. It’s also possible to *slice* your list, which means selecting multiple elements from your list.for example:

areas = [“hallway”, 11.25, “kitchen”, 18.0, “living room”, 20.0, “bedroom”, 10.75, “bathroom”, 9.50]

Use slicing to create a list,downstairs and Upstairs, that contains the first 6 elements of areas and last 4 elements of the areas respectively.

**INPUT: downstairs=areas[0:6]⇐ List does not include the element indexed 6 but not 0) **

** upstairs=areas[6:] ⇐ (all elements are included from 7th to 10th)**

** print(downstairs)**

** print(upstairs)**

**OUTPUT: [‘hallway’, 11.25, ‘kitchen’, 18.0, ‘living room’, 20.0] [‘bedroom’, 10.75, **

**‘bathroom’, 9.5]**

## Manipulating List

In Python you can select single elements or you can change entire list slices at once.

For Example: areas = [“hallway”, 11.25, “kitchen”, 18.0, “living room”, 20.0, “bedroom”, 10.75, “bathroom”, 9.50] .You want to correct the bathroom area which is 10.50 instead of 9.50.

** INPUT:areas[9]=10.50**

** print(areas)**

**OUTPUT:[‘hallway’, 11.25, ‘kitchen’, 18.0, ‘living room’, 20.0, ‘bedroom’, 10.75, ‘bathroom’, 10.5]**

With ‘+’ operator you can change elements in a list or want to add elements to it.

for example: areas = [“hallway”, 11.25, “kitchen”, 18.0, “chill zone”, 20.0,

“bedroom”, 10.75, “bathroom”, 10.50]

If want to add poolhouse data to areas, then new list will be generated with name areas_1

areas_1=areas+[“poolhouse”, 24.5]

**INPUT: areas = [“hallway”, 11.25, “kitchen”, 18.0, “chill zone”, 20.0, **

**“bedroom”, 10.75, “bathroom”, 10.50]**

** areas_1=areas+[“poolhouse”, 24.5]**

** print(areas_1)**

**OUTPUT: [‘hallway’, 11.25, ‘kitchen’, 18.0, ‘chill zone’, 20.0, ‘bedroom’, 10.75, ‘bathroom’, 10.5, ‘poolhouse’, 24.5]**

### Coping List

Suppose Y is a copy of list X then any change in Y led to the changes in X as well.To avoid this instead of using command Y=X,use Y=list(X) .Y becomes a copied list of list X and any further changes in Y will Not affect list X.

### String Methods

Strings come with a bunch of methods.

Suppose X= “room”

to convert X in to upper letter use **room.upper() **and generate a new variable

to count the number of ‘o’ in room use **room.count(‘o’)** followed by **print(room.count(‘o’))**

To add an element in the list use **append()** and use** reverse()** that reverses the order of the elements in the list it is called on.

**Importing Packages**

For the purpose of computation we import packages in data .Suppose to calculate the circumference of a sphere which is 2∏ r,we import math package in python.

Given r=.43

We import math package by using the command **import math** hence we calculate Circumference we use **2*math.pi*r . **Let **2*math.pi*r = C .**

By print(C) we get the circumference.

**NUMPY**

Here come the most important topic: “NUMPY”.

**DIFFERENCE BETWEEN LIST AND NUMPY ARRAY**: To carry out complex calculations using list is highly inefficient ,we use numpy in this case.

Suppose Price of commodities are given as

price=([5,2,6,9]) and units is given as

unit=([2,6,8,1])

then to calculate total revenue using list method we have to separately write

price_1 = 5

unit_1=2

total revenue_1= price_1* unit_1

print(total revenue_1) gives output as 10

The above method is inefficient and tired-some.

Here comes Numeric Python which an alternative to Python List – Numpy Array.

Numpy do calculation over entire array.It is fast and easy.

To Use Numpy first import Numpy by

import numpy as np.

convert price and units in to numpy array

**np_price=np.array(price)**

**np_units=np.array(units)**

total revenue=np_price*np_units

**Input : total revenue**

**Output: array([10,12,48,9])⇐ Easy and efficient.**

**Assumption: All the elements in the array are of same data type**

**DIFFERENCE BETWEEN LIST AND NUMPY ARRAY REVISITED**

**If python_list = [1,2,3]**

**INPUT : python_list + python_list**

**OUTPUT:[1,2,3,1,2,3]**

**BUT in case of Numpy Array given np_array=np.array([1,2,3])**

**INPUT : np_array + np_array**

**OUTPUT : array([2,4,6])**

Here if you want total revenue greater than 3 given the array of boolian then

**INPUT: total revenue_1=total revenue>3**

**print**(**total revenue_1)**

**Output: array([False,True,True;];dtype=bool)**

**INPUT :print(total revenue[total revenue>3])**

**OUTPUT: array([4,6])**

## 2 D Numpy Array

Suppose area is list of list such that:

areas:[[180, 78.4],

[215, 102.7],

[210, 98.5],

[188, 75.2]]

Here we use np.array() to create a 2D Numpy array from areas,naming it np_areas and then printing the output.

**INPUT: import numpy as np**

** np_areas=np.array(areas)**

** print(np_areas)**

**OUTPUT:[[ 180. 78.4]**

** [ 215. 102.7]**

** [ 210. 98.5]**

** [ 188. 75.2]]**

To know the shape (no.of rows and column)of np_areas we use **print(np_areas.shape) which gives output (4,2).**

**Statistics Using Numpy**

Suppose X=[1, 4, 8, 10, 12]

**Mean** can be calculates as **print(np.mean(x)).**

**Median** can be calculated as **print(np.median(x)).**

Standard Deviation can be calculated as** print( np.std(X))**

**In case of 2D **

Suppose np_baseball=[ 74. 180. 22.99]

[ 74. 215. 34.69]

[ 72. 210. 30.78]

…,

[ 75. 205. 25.19]

[ 75. 190. 31.01]

[ 73. 195. 27.92]]

To find out correlation coefficient between first and second column we use

**corr = np.corrcoef(np_baseball[:,0], np_baseball[:,1]) along with**

**print(corr) gives output [[ 1. 0.53153932]**

** [ 0.53153932 1. ]**

So that is intro to the very basic and easy form of Python. Here we have seen Python datatypes,List,Numpy,Manipulations,changes,Statistics,etc.Its a programming language which is adapted by many companies now a days because of its efficiency and is easy to learn as well.