Web tool: (Install and run from here (http://jupyter.org/install.html))
jupyter notebook
———————————————————————————-
pip install -r requirements.txt for python 3.x
—————————————————————————————————
Ipython – interactive python
—————————————————————————————————
Comments:
Single line or multi-line:
# Hi this is single line comment
“””HI, this is multiline comment”””
—————————————————————————————————
Operators:
Exponentiation: **. This operator raises the number to its left to the power of the number to its right. For example 4**2 will give 16.
—————————————————————————————————
Print Statement:
print (‘Hello’, name) # Output will be — “Hello World” world is the value of variable name
—————————————————————————————————
Variables:
Python type : to get the type of the variable
eg: day_of_week = 5
type(day_of_week ) # output : int
String: can be wrapped in any single quote or double quote.
eg. ‘I am a string’
“I am a string”
+ is used to concatenate strings.
‘ab’+’cd’ #output : ‘abcd’
“a”*5 #Output: ‘aaaaa’
Boolean : store true or false
eg. flag = True
type(flag) #output: bool
—————————————————————————————————
Type Conversion:
str(), int(), float(), bool()
saving = 100
now we can convert integer variable saving in string type using below code.
str(saving)
—————————————————————————————————
**Python Lists:
fam = [1.73, 1.68, 1.71, 1.89]
A list in python can contain values from different data types.
eg. fam = [“liz”, 1.73, “emma”, 1.68, “mom”, 1.71, “dad”, 1.89]
A list can have list also,
eg. fam2 = [[“liz”, 1.73], [“emma”, 1.68], [“mom”, 1.71], [“dad”, 1.89]]
type(fam) #Output: list
We can use variables in the list also.
eg. bed = 10.75
bath = 9.50
# Create list areas
areas = [“hallway”, hall, kit, liv, bed, bath, 20, 76]
Access List Elements:
– It has zero-based indexing
[‘liz’, 1.73, ’emma’, 1.68, ‘mom’, 1.71, ‘dad’, 1.89]
index: 0 1 2 3 4 5 6 7
-v index: -8 -7 -6 -5 -4 -3 -2 -1
“zero-based indexing”
fam[3] #Output : 1.68
-ve index is used to access elements from last.
fam[-1] #output : 1.89
fam[3:5] #output : [1.68, ‘mom’]
[ start : end ]
inclusive exclusive
fam[:4] #Output: [‘liz’, 1.73, ’emma’, 1.68]
fam[5:] #Output: [1.71, ‘dad’, 1.89]
Manipulating Lists:
Changing list elements:
fam = [“liz”, 1.73, “emma”, 1.68, “mom”, 1.71, “dad”, 1.89]
fam[7] = 1.86
or
fam[0:2] = [“lisa”, 1.74]
Adding and removing elements:
fam + [“me”, 1.79]
del(fam[2]) # to delete element
x = [“a”, “b”, “c”]
y = x
here reference of x is stored in y like in array in C. so basically x and y both are pointing to the same list.
To make a different list with the copy of existing list use the below code.
x = [“a”, “b”, “c”]
y = list(x) OR y = x[:]
y[1] = “z” # here change is in the list of y, x list is still holding the previous values.
—————————————————————————————————
Import Module
import sys
—————————————————————————————————
Define Function
def main(): -or- def repeat(s, exclaim):
—————————————————————————————————
If-Else
if len(sys.argv) >=2 :
name = sys.argv[1]
else:
name = ‘world’
—————————————————————————————————
Example for whatever we learned so far:
# Simple hello world program
import sys
def main():
if len(sys.argv) >=2 :
name = sys.argv[1]
else:
name = ‘world’
print(‘Hello’,name)
if __name__ == ‘__main__’:
main()
—————————————————————————————————
Ubuntu Commands check the version of Python 3
python3 -V
—————————————————————————————————
To manage software packages for Python, let’s install pip:
sudo apt-get install -y python3-pip
pip3 install package_name
Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command
pip3 install numpy
There are a few more packages and development tools to install to ensure that we have a robust set-up for our programming environment:
sudo apt-get install build-essential libssl-dev libffi-dev python-dev
—————————————————————————————————
Use specific module from the package:
Numpy (Numeric Python): alternate to python list
– List fail to perform operation on complete list.
eg.
height = [1.73, 1.68, 1.71, 1.89, 1.79]
weight = [65.4, 59.2, 63.6, 88.4, 68.7]
weight / height ** 2 TypeError: unsupported operand type(s) for **: ‘list’ and ‘int’
– But numpy array has power of calculations over entire arrays
import numpy as np
np_height = np.array(height)
np_weight = np.array(weight)
bmi = np_weight / np_height ** 2
In [12]: bmi
Out[12]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
Comparison list vs numpy
In [13]: height = [1.73, 1.68, 1.71, 1.89, 1.79]
In [14]: weight = [65.4, 59.2, 63.6, 88.4, 68.7]
In [15]: weight / height ** 2 TypeError: unsupported operand type(s) for **: ‘list’ and ‘int’
In [16]: np_height = np.array(height)
In [17]: np_weight = np.array(weight)
In [18]: np_weight / np_height ** 2
Out[18]: array([ 21.852, 20.975, 21.75 , 24.747, 21.441])
NumPy arrays: contain only one type.
In [19]: np.array([1.0, “is”, True])
Out[19]: array([‘1.0’, ‘is’, ‘True’],
dtype='<U32′) Here converted float and boolean into string
Different types: different behavior!
In [20]: python_list = [1, 2, 3]
In [21]: numpy_array = np.array([1, 2, 3])
In [22]: python_list + python_list
Out[22]: [1, 2, 3, 1, 2, 3] In list + operator appended one list into another
In [23]: numpy_array + numpy_array
Out[23]: array([2, 4, 6]) In array + operator performed additon on array elements
All the bmi greater than 23
bmi[bmi > 23]
Out[27]: array([ 24.747])
2D NumPy Arrays
np_2d = np.array([[1.73, 1.68, 1.71, 1.89, 1.79], [65.4, 59.2, 63.6, 88.4, 68.7]])
In [8]: np_2d.shape
Out[8]: (2, 5) 2 rows, 5 columns
In [10]: np_2d[0]
Out[10]: array([ 1.73, 1.68, 1.71, 1.89, 1.79])
In [11]: np_2d[0][2]
Out[11]: 1.71
OR
In [12]: np_2d[0,2]
Out[12]: 1.71
In [13]: np_2d[:,1:3]
Out[13]: array([[ 1.68, 1.71], [ 59.2 , 63.6 ]])
import numpy as np
np.array([1, 2, 3])
or
from numpy import array
array([1, 2, 3])
In the second case where we are importing specific module, we need not to specify package name before using its functions.
Create array in regular numpy array function:
import numpy as np
—————————————————————————————————
Package list and their uses:
#package for regular expression
import re
# Powerful data structures for data analysis, time series,and statistics, Tabular data with heterogeneously-typed columns, as in an SQL table or Excel spreadsheet
import pandas
#matplotlib.pyplot is a collection of command style functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.
import matplotlib.pyplot
# To generate wordcloud from wordcloud
import WordCloud
or
import WordCloud as wc
Data Science package:
Numpy
Matplotlib
Scikit-learn
Leave a Reply