# Basics of Python for Data Science

**Introduction to Python**

Python is an open-source, high-level and general-purpose programming language which was created by Guido Van Rossum and released in 1991. Python 3.7 was released on 27.06.2018. It is very powerful, fast, friendly and easy to learn programming language. From here to onward we will use Python 3.

**Jupyter**

The Jupyter Notebook is an open-source web application. It permits you to create and share documents which contain live code, visualizations, etc. Jupyter used for data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and so on.

**Jupyter’s Interface – Dashboard:** Snap of Jupyter’s Interface – Dashboard.

You can see in below picture the interface is very simple and easy to understands.

We will click on ‘New’ button to create a new Notebook Python 3, a new text file, folder or terminal. Therefore, we click on Notebook Python 3 to create it.

To execute the code press *Shift + Enter*

Generally, code option is selected but if you intend to write comments/message or documentation then you will select **markdown cell option,** this text will not be executed as shown in the figure.

From here to onward we mostly focused on practical examples to understand the basic concepts.

### Variables

Variables declaration in python is very simple. Look at the figure that will show you how to declare variables in python.

‘x’ is declared with initial value of 12. When we run the code, the output value ’12’ is printed.

#### Numbers and Boolean Values:

**Integer**

Positive or negative whole numbers but without decimal points e.g. 3, 6, -4, -12, etc.

**Float**

Real numbers having decimal point like 3.6, -4.12, etc.

**Boolean Value**

It is two values ‘true’ or ‘false’, 0 or 1, on or off.

**Strings**

Strings consist of a sequence of characters like ‘test’, ‘book’, etc. Firstly, we have written Karachi without any quotes or print statement. The result is error.

Now put the same string in quotes * ‘Karachi’,* or by using

*output success.*

**print (‘Karachi’)**Example of the concatenation of two strings is shown next. ‘+’ sign is used to concatenate the two strings.

**Arithmetic Operators**

Arithmetic operations are as simple as shown here. Simple operations of addition, multiplication, division, and multiplication are given. You may have noticed x = 3 **2. The out is 9. What does it mean? It means that power of 3 is raised to 2.

The last operation with % operator gives use remainder 2.

**SHORT KEYS:**

Few important shortcuts are given as under: –

Key | Purpose |

Ctrl + Enter | Code execution |

Shift + Enter | Code execution with new line |

Double click on D | Select cell or input field and then double click on D to delete the cell or input field |

\ (back slash) | Line continuation |

**Comparison, Logical and Identity Operators:**

**Comparison Operators **

Operator | Description |

= = | Double equal signs verify the equality on both left and right sides |

! = | It verify left and right sides are not equal |

> | Greater than sign |

< | Less than sign |

> = | Greater than and equal to |

< = | Less than and equal to |

**Logical Operators**

Operator | Description |

And | Checks whether the two statements around it are ‘True’ |

Or | Checks whether at least one out of two statements is ‘True’ |

Not | It leads to the opposite of the given statement if ‘true’ leads to ‘false’ and vice versa. |

**Identity Operators**

Word | Description |

Is | Checks whether the two statements around it are ‘True’ |

Is not | Checks whether the two statements around it are ‘True’ |

**Operator precedence:**

Not, And & then OR operator. A practical example of these operators are shown below,

5 == 6 4 == 4 4 != 5 4 is 5 4 is not 5

**Adding Comments**

Adding comments is an essential part of any programming language that will be helpful during coding and for future utilization of code. For commenting, hashtag # is used, see this example.

# This is just comment that will not effect on coding at all x = 3 * 12 x

**Indexing Elements:**

Syntax: “name of variable”[index of element]. In indexing, square brackets [] must be used instead of parentheses () and braces {}.

It is pertinent to note that Python starts counting from 0 instead of 1. In our above example, the 4^{th} letter is ‘s’.

**Conditional Statements (If, Elseif):**

Basic Syntax of 'if' statement if condition conditional code

if 5 == 5: print ('Right')

syntax of 'if else statement if condition condition code else else code

if 5 == 6: print ('Right') else: print ('Wrong')

syntax of "if elif' statement if condition conditional code elif conditional code else else code

if 6 > 5: print ('Yes') elif 6 < 5: print ('No') else: print ('Blank')

**While Loop:**

a = 0 while a <= 10: print (a) a = a + 2

**Creating a Function with a parameter:**

def sample(x): return x + 5 sample(3)

**Creating multiple functions with a few parameters:**

def sample(x,y,z): result = x + y + z print ('parameter x equals', x) print ('parameter y equals', y) print ('parameter z equals', z) return result sample (2,4,6)

**Built-in Functions in Python:**

Function | Description |

int() | Convert its arguments in integer data type |

float() | Convert its argument in float data type |

str() | Convert its argument in string data type |

abs() | Get the absolute value of its argument |

sum() | Calculate the sum of all the elements |

max() | Give the highest value from a sequence of a numbers |

min() | Give the minimum value from a sequence of a numbers |

len() | It return the number of elements in an object |

round(x,y) | It returns the float of its argument(x), rounded to a specified number of digits(y) after the decimal point |

pow(x,y) | It return x to the power of y |

**Lists:**

It is a type of sequence of data points like, integers, strings or floats.

students = ['Shahid', 'Khalid', 'Ali'] students students[1] students[-1]

**Tuples:**

It is also a type of sequence of data points but tuples cannot be modified.

a = (12, 4, 6) a x, y, z = 2, 4, 6 z

**Dictionaries:**

It is another way of storing data.

dict = {'d1': "Apple", 'd2': "Mango"} dict['d1']

**Built-in Methods (Append and Extend):**

Passengers =['Shahid', 'Khalid', 'Ali'] Passengers Passengers.append("Rizwan") Passengers Passengers.extend(["Jan"]) Passengers

Syntax: object.method()