Python Dictionary

Python’s efficient key/value hash table structure is called a “dictionary” or “dict”. Major difference between list and dictionary is that index is always numeric in list whereas in dictionary it can be of any data type.The contents of a dict can be written as a series of key:value pairs within braces { }, e.g. dict = {key1:value1, key2:value2, … }. The “empty dict” is just an empty pair of curly braces {}.

 

>>> dict={}
>>> dict
{}
>>> dict['a']="blue"
>>> dict['b']="sky"
>>> dict['c']="earth"
>>> dict
{'a': 'blue', 'b': 'sky', 'c': 'earth'}
>>> dict['a']
'blue'
>>> type(dict)
<class 'dict'>
>>> dict={'a':'sky','b':'blue'} #other way to create dictionary
>>> dict
{'a': 'sky', 'b': 'blue'}

How to display key and values

>>> for k, v in dict.items(): print(k,'>',v) 
... 
a > blue
b > sky
c > earth
>>> for a in dict.values(): print(a)
... 
blue
sky
earth
>>> for b in dict.keys(): print(b)
... 
a
b
c

changing values and removing values

>>> dict
{'a': 'blue', 'b': 'sky', 'c': 'earth'}
>>> dict['a']="black" 
>>> dict
{'a': 'black', 'b': 'sky', 'c': 'earth'}
>>> dict.pop('a')
'black'
>>> dict
{'b': 'sky', 'c': 'earth'}
>>> del dict['b']
>>> dict
{'c': 'earth'}
>>> dict.clear()
>>> dict
{}

Python Lists

Lists are just like the arrays in other languages. Lists need not be homogeneous A single list may contain different data types such as Integers, Strings, as well as Objects.  List literals are written within square brackets [ ]. Lists work similarly to strings — use the len() function and square brackets [ ] to access data, with the first element at index 0. manjor difference being list is mutable however string is not.

>>> a=[]
>>> type(a)
<class 'list'>
>>> colors = ['red', 'blue', 'green']
>>> colors[0]
'red'
>>> len(colors)
3
>>> colors.append('yellow')
>>> colors
['red', 'blue', 'green', 'yellow']
>>> colors.insert(0,'black')
>>> colors
['black', 'red', 'blue', 'green', 'yellow']
>>> colors.extend(['white','gre'])
>>> colors
['black', 'red', 'blue', 'green', 'yellow', 'white', 'gre']
>>> colors.pop()
'gre'
>>> colors
['black', 'red', 'blue', 'green', 'yellow', 'white']
>>> colors.append('grey')
>>> colors
['black', 'red', 'blue', 'green', 'yellow', 'white', 'grey']
>>>

Slicing

>>> colors[:]
['black', 'red', 'blue', 'green', 'yellow', 'white', 'grey']
>>> colors[1:1]
[]
>>> colors[1:3]
['red', 'blue']
>>> colors[-1:]
['grey']
>>> colors[-1:-1]
[]
>>> colors[-1:-3]
[]
>>> colors[-1:2]
[]
>>> colors[-5:6]
['blue', 'green', 'yellow', 'white']
>>> colors[-5:-1]
['blue', 'green', 'yellow', 'white']
>>> colors[-5::-1]
['blue', 'red', 'black']

You can iterate thru list using following

>>> 'red' in colors
True
for col in colors:
     print(col)
#Output
black
red
blue
green
yellow
white
grey
['sky blue', 'snow white']

To check if element exists in List

>>> 'red' in colors
True

List Methods

Here are some other common list methods.

  • list.append(elem) — adds a single element to the end of the list. Common error: does not return the new list, just modifies the original.
  • list.insert(index, elem) — inserts the element at the given index, shifting elements to the right.
  • list.extend(list2) adds the elements in list2 to the end of the list. Using + or += on a list is similar to using extend().
  • list.index(elem) — searches for the given element from the start of the list and returns its index. Throws a ValueError if the element does not appear (use “in” to check without a ValueError).
  • list.remove(elem) — searches for the first instance of the given element and removes it (throws ValueError if not present)
  • list.sort() — sorts the list in place (does not return it). (The sorted() function shown later is preferred.)
  • list.reverse() — reverses the list in place (does not return it)
  • list.pop(index) — removes and returns the element at the given index. Returns the rightmost element if index is omitted (roughly the opposite of append()).

List Comprehension

 

How to calculate Euclidean and Manhattan distance by using python

Euclidean Distance

Euclidean metric is the “ordinary” straight-line distance between two points.

if p = (p1, p2) and q = (q1, q2) then the distance is given by

Euclidean distance
Euclidean distance

For three dimension1, formula is

Euclidean distance
Euclidean distance

################################################################################################
#	name:	eudistance_samples.py
#	desc:	Simple scatter plot 
#	date:	2018-08-28
#	Author:	conquistadorjd
################################################################################################
from scipy import spatial
import numpy
from sklearn.metrics.pairwise import euclidean_distances

import math

print('*** Program started ***')


######################################################################### Calculating distance by using  python math function
############################################################# 2 D array
x1 = [1,1]
x2 = [2,9]
eudistance =math.sqrt(math.pow(x1[0]-x2[0],2) + math.pow(x1[1]-x2[1],2) )
print("eudistance Using math ", eudistance)

############################################################# 3 D array
# x1 = [1,1,4]
# x2 = [10,2,7]
# # Calculating distance by using math
# eudistance = math.sqrt(math.pow(x1[0]-x2[0],2) + math.pow(x1[1]-x2[1],2) + math.pow(x1[2]-x2[2],2) )
# print("eudistance Using math ", eudistance)

######################################################################### Calculating distance by using scipy
eudistance = spatial.distance.euclidean(x1, x2)
print("eudistance Using scipy", eudistance)


######################################################################### Calculating distance by using numpy
x1np=numpy.array(x1)
x2np=numpy.array(x2)
eudistance = numpy.sqrt(numpy.sum((x1np-x2np)**2))
print("eudistance Using numpy", eudistance)

eudistance = numpy.linalg.norm(x1np-x2np)
print("eudistance Using numpy", eudistance)


######################################################################### Calculating distance by using sklearn
eudistance =  euclidean_distances([x1np], [x2np]) # for some strange reasons, values needs be in 2-D array
print("eudistance Using sklearn", eudistance)

print('*** Program ended ***')

Manhattan distance

The distance between two points measured along axes at right angles.The Manhattan distance between two vectors (or points) a and b is defined as ∑i|ai−bi| over the dimensions of the vectors.

Manhattan distance
Manhattan distance

all paths from the bottom left to top right of this idealized city have the same distance.

Manhattan Distance
Manhattan Distance
1.
scipy.spatial.distance.euclidean. scipy.spatial.distance.euclidean. https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.spatial.distance.euclidean.html. Published August 28, 2018.

Multi threading in Python

Multi-threading is a way of achieving multitasking. Many times we run into a situation where do done need to run code sequentially and would like to run two processes (functions ) in parallel so save some time.

Typically python executes code sequentially. For example, if you have called a function, next sentence will be executed only after function in previous row has completed execution.

Here is the sample code for multi-threading. We will be using python module threading


import threading
import time
from threading import Thread

def function1():
	print('Inside Function1. Now going to sleep')
	time.sleep(5)
	print('Function1 woke up from sleep')

def function2():
	print('Inside Function2. Now going to sleep')
	time.sleep(3)
	print('Function2 woke up from sleep')

if __name__ == '__main__':
    Thread(target = function1).start()
    Thread(target = function2).start()

Here is the output


python3.6 multithreading.py 
Inside Function1. Now going to sleep
Inside Function2. Now going to sleep
Function2 woke up from sleep
Function1 woke up from sleep

As you can see function2 and function1 are running in parallel. You can see function2 woke up first from sleep

Returning Multiple Values in Python using function

Functions are use to some repetitive activity to avoid adding same code multiple times. Functions sometime return a value  but sometimes may not even return anything, however, you might run into situations where you may want to return multiple values by using function. When you use return statement, it will return the  only one variable. But if you need multiple values, what can be done ?

This can be achieved by using multiple ways, your creativity is the limit, thats the beauty of any programming language.

Two most easiest ways are as below

  1. Return dictionary, tuple or list
  2. Return multiple variables at the same time

Returning dictionary, tuple or list

Here, you simply create a variable of datatype that can store multiple values such as dictionary, tuple or list.  This will add few lines of coding and code might not look as structured and beautiful as you wish but this works

Return multiple variables at the same time

This is nothing but above mentioned method, except, you delegate variable creation to python. Python will create a return a tuple having required values. You still need to take care while receiving the values.

Here is the sample code

def get_add_and_multi(num1,num2):
add = num1+num2
mult = num1*num2
return add,mult

result = get_add_and_multi(2,3)

add, mult = get_add_and_multi(2,3)

print("result :", result, "Datatype is :", type(result))
print("add :", add , "Datatype is :", type(add))
print("mult :", mult,"Datatype is :", type(mult))

As you can see, there are two ways of receiving data.

Output of this code is as below. Please note the data types

$ python3.6 returnmultiple.py 
result : (5, 6) Datatype is : <class 'tuple'>
add : 5 Datatype is : <class 'int'>
mult : 6 Datatype is : <class 'int'>

Python : Matplotlib

Matplotlib is a Python library used to create charts and graphs.

Installation
$ sudo python3.6 -m pip install pandas
Simple Line plot
import codecademylib  
from matplotlib import pyplot as plt

days = [0, 1, 2, 3, 4,5,6]  
money_spent = [10, 12, 12, 10, 14,22,24]  
plt.plot(days, money_spent)  
plt.show()  

multiple line plots displayed on the same set of axes.

import codecademylib  
from matplotlib import pyplot as plt

time = [0, 1, 2, 3, 4]  
revenue = [200, 400, 650, 800, 850]  
costs = [150, 500, 550, 550, 560]

plt.plot(time, revenue)  
plt.plot(time, costs)  
plt.show()  

Plot options

import codecademylib  
from matplotlib import pyplot as plt

time = [0, 1, 2, 3, 4]  
revenue = [200, 400, 650, 800, 850]  
costs = [150, 500, 550, 550, 560]

plt.plot(time,revenue,color='purple',linestyle='--')  
# marker='o' is circle; s is squar , * is star
plt.plot(time,costs,color='#82edc9',marker='s')  
plt.show()  
special

zoom in and zoom out

# first wto are x coordinates and last two and y coordinates
plt.axis([0, 12, 2900, 3100])  
#Labelling the axis
plt.xlabel('Time of day')  
plt.ylabel('Happiness Rating (out of 10)')  
plt.title('My Self-Reported Happiness While Awake')  
Creating subplots

he command plt.subplot(2, 3, 4) would create “Subplot 4” from the figure above.

Any plt.plot that comes after plt.subplot will create a line plot in the specified subplot

import codecademylib  
from matplotlib import pyplot as plt

x = range(7)  
straight_line = [0, 1, 2, 3, 4, 5, 6]  
parabola = [0, 1, 4, 9, 16, 25, 36]  
cubic = [0, 1, 8, 27, 64, 125, 216]

# Subplot 1
plt.subplot(2, 1, 1)  
plt.plot(x, straight_line)

# Subplot 2
plt.subplot(2, 2, 3)  
plt.plot(x, parabola)

# Subplot 3
plt.subplot(2, 2, 4)  
plt.plot(x, cubic)

plt.subplots_adjust(wspace=0.35, bottom=0.2)

plt.show()  

Adding legends

plt.legend(['Hyrule', 'Kakariko','Gerudo Valley'],loc=8)  
specific ticks
ax.set_xticks([1, 2, 4])  
ax.set_yticks([0.1, 0.6, 0.8])  
ax.set_yticklabels(['10%', '60%', '80%'])  

Working example

from matplotlib import pyplot as plt

month_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct", "Nov", "Dec"]

months = range(12)  
conversion = [0.05, 0.08, 0.18, 0.28, 0.4, 0.66, 0.74, 0.78, 0.8, 0.81, 0.85, 0.85]

plt.xlabel("Months")  
plt.ylabel("Conversion")

plt.plot(months, conversion)

# Your work here
ax = plt.subplot()  
ax.set_xticks(months)  
ax.set_xticklabels(month_names)

ax.set_yticks([0.10, 0.25, 0.5, 0.75])  
ax.set_yticklabels(['10%', '25%', '50%','75%'])

plt.show()  
Axis lables
plt.xlabel('Time of day')  
plt.ylabel('Happiness Rating (out of 10)')  
plt.title('My Self-Reported Happiness While Awake')  
plt.show()  
Showing legends
plt.legend(['parabola', 'cubic'], loc=6)  
plt.show()  
saving fig
plt.figure(figsize=(7, 3))  
plt.plot(years, power_generated)  
plt.savefig('power_generated.png')  
Different type of graphs
bargraph
from matplotlib import pyplot as plt

drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]  
sales =  [91, 76, 56, 66, 52, 27]

plt.bar(range(len(drinks)), sales)

#create your ax object here
ax = plt.subplot()  
ax.set_xticks([0, 1, 2, 3, 4, 5])  
ax.set_xticklabels(["cappuccino", "latte", "chai", "americano", "mocha", "espresso"],  
rotation=90)  
plt.show()  

adjecent bargraph

from matplotlib import pyplot as plt

drinks = ["cappuccino", "latte", "chai", "americano", "mocha", "espresso"]  
sales1 = [91, 76, 56, 66, 52, 27]  
sales2 = [65, 82, 36, 68, 38, 40]

#Paste the x_values code here
n = 1  # This is our first dataset (out of 2)  
t = 2 # Number of dataset  
d = 6 # Number of sets of bars  
w = 0.8 # Width of each bar  
store1_x = [t*element + w*n for element  
             in range(d)]

plt.bar(store1_x, sales1)  
#Paste the x_values code here
n = 2  # This is our second dataset (out of 2)  
t = 2 # Number of dataset  
d = 6 # Number of sets of bars  
w = 0.8 # Width of each bar  
store2_x = [t*element + w*n for element  
             in range(d)]

plt.bar(store2_x, sales2)

plt.show()  
Creating range
from matplotlib import pyplot as plt

months = range(12)  
month_names = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]  
revenue = [16000, 14000, 17500, 19500, 21500, 21500, 22000, 23000, 20000, 19500, 18000, 16500]

#your work here
ax = plt.subplot()  
ax.set_xticks(months)  
ax.set_xticklabels(month_names)

y_upper = [1.1*i for i in revenue]  
y_lower = [0.9*i for i in revenue]  
plt.fill_between(range(len(months)), y_lower, y_upper, alpha=0.2) 

plt.plot(range(len(months)),revenue)  
plt.show()  
Pie charts
from matplotlib import pyplot as plt

payment_method_names = ["Card Swipe", "Cash", "Apple Pay", "Other"]  
payment_method_freqs = [270, 77, 32, 11]

plt.pie(payment_method_freqs, autopct="%0.1f%%")  
plt.axis('equal')  
plt.legend(payment_method_names)

plt.show()  
Histogram
a = normal(loc=64, scale=2, size=10000)  
b = normal(loc=70, scale=2, size=100000)

plt.hist(a, range=(55, 75), bins=20, alpha=0.5, normed=True)  
plt.hist(b, range=(55, 75), bins=20, alpha=0.5, normed=True)  
plt.show()  

 

Please refer to matplotlib tutorial for further information.

Python : Working with Pandas

Pandas is a Python module for working with tabular data (i.e., data in a table with rows and columns). Tabular data has a lot of the same functionality as SQL or Excel, but Pandas adds the power of Python.

  • A DataFrame is an object that stores data as rows and columns. You can think of a DataFrame as a spreadsheet or as a SQL table
  • You can manually create a DataFrame or fill it with data from a CSV, an Excel spreadsheet, or a SQL query.
  • DataFrames have rows and columns. Each column has a name, which is a string.
  • Each row has an index, which is an integer.
  • DataFrames can contain many different data types: strings, ints, floats, tuples, etc.

Please refer to how to install pandas to install it for python3.6

Pandas have following three data structures

  1. Series
  2. DataFrames
  3. Panels

we mostly will work with DataFrames. series can be considered as subset of DataFrames.

Creating Series and DataFrames

my_list=[1,3,5,6,8,11,18]
s1 = pd.Series(my_list)
s2 = pd.Series([1,3,5,6,8,11,18])
s3 = pd.Series(np.arange(100))

s4=pd.DataFrame(my_list)
df2 = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 4)),columns=['a', 'b', 'c', 'd'])

>>> type(my_list)
<class 'list'>
>>> type(s1)
<class 'pandas.core.series.Series'>
>>> type(s4)
<class 'pandas.core.frame.DataFrame'>
# Creating DataFrame using dict
sales = [{'account': 'Jones LLC', 'Jun': 150, 'Jul': 200, 'Aug': 140},
{'account': 'Alpha Co', 'Jun': 200, 'Jul': 210, 'Aug': 215},
{'account': 'Blue Inc', 'Jun': 50, 'Jul': 90, 'Aug': 95 }]
df = pd.DataFrame(sales)
#
sales = {'account': ['Jones LLC', 'Alpha Co', 'Blue Inc'],
'Jun': [111, 222, 55],
'Jul': [222, 210, 90],
'Aug': [140, 215, 95]}
df = pd.DataFrame.from_dict(sales)

>>> df.dtypes
Feb int64
Jan int64
Mar int64
account object
dtype: object

 

df2 = pd.DataFrame(data,columns=['Store ID','Location','Number of Employees'])  
print df2  
Reading from and writing into CSV files
df = pd.read_csv('my-csv-file.csv')
df.to_csv('new-csv-file.csv')
#displaying top 5 rows
df.head()
#displaying top 10 rows
df.head(10)
#statistical information about df
df.info()

df columns can be accessed by two ways

df['age']  
df.age  

type of this data

print(type(df['age'])
print(type(df.age)
<class 'pandas.core.series.Series'>

Accessing rows

print(df.loc[2]) #Python is zero indexed  
df.loc[3:7]  
df.loc[:4]  
df.loc[2:]  

Creating sub dataframe

# create using specific columns
df2 =df[['clinic_north','clinic_south']]  
# create using certian conditon
df2=df[df.month == 'january']  
df2=df[df.age > 27]  
df2=df[df.city != 'Mumbai']  
# using multyiple conditions / In Python, | means "or" and & means "and".
df2 = df[(df.age < 30) | (df.name == 'Martha Jones')] # (curly brackets are must)  
df2 = df[df.month.isin(['January','February','March'])]  
# Reset index to fetch correct row number
df2.reset_index(drop=True)  
Modifying DataFrames
#adding a column
df['height']=[1,2,3,4,5,5]  
#adding same value column
df['In Stock?'] = True  
df['In Stock?'] = 'Yes'  
#New column derived from other columns
df['Revenue'] = df.Price - df['Cost to Manufacture']  
#changing cap
from strings import lower  
df['Lowercase Name'] = df['Name'].apply(lower)  

Using lambda function to modify

get_last_name = lambda x: x.split(' ')[-1]  
df['last_name'] = df.name.apply(get_last_name)  

Columns can be renamed using

df.columns = ['ID', 'Title','Category','Year Released','Rating']  
#selective renaming
df.rename(columns={'name': 'First Name','age': 'Age'},inplace=True)  

Using rename with only the columns keyword will create a new DataFrame, leaving your original DataFrame unchanged. That’s why we also passed in the keyword argument inplace=True. Using inplace=True lets us edit the original DataFrame.

wokring with rows amd columns

import pandas as pd

orders = pd.read_csv('shoefly.csv')

print orders.head(5)

orders['shoe_source'] = orders.shoe_material.apply(lambda x:   
                            'animal' if x == 'leather'else 'vegan')

orders['salutation'] = orders.apply(lambda row:   
                                    'Dear Mr. ' + row['last_name']
                                    if row['gender'] == 'male'
                                    else 'Dear Ms. ' + row['last_name'],
                                    axis=1)

Aggregations

  • mean Average of all values in column
  • std Standard deviation
  • median Median
  • max Maximum value in column
  • min Minimum value in column
  • count Number of values in column
  • nunique Number of unique values in column
  • unique List of unique values in column
num_colors =orders.price.max()  
num_colors =orders.shoe_color.nunique()  
#groupby
df.groupby('column1').column2.measurement()  
#example
grades = df.groupby('student').grade.mean()  
pricey_shoes =orders.groupby('shoe_type').price.max()  
#groupby with muiple columns
shoe_counts = orders.groupby(['shoe_type','shoe_color']).id.count().reset_index()  

Sometimes, the operation that you want to perform is more complicated than mean or count. In those cases, you can use the apply method and lambda functions, just like we did for individual column operations. Note that the input to our lambda function will always be a list of values.

cheap_shoes = orders.groupby('shoe_color').price.apply(lambda x: np.percentile(x,25)).reset_index()  
print(cheap_shoes)

Pivots using dataframe

import pandas as pd

user_visits = pd.read_csv('page_visits.csv')  
print(user_visits.head())

click_source=user_visits.groupby('utm_source').id.count().reset_index()  
print(click_source)

click_source_by_month=user_visits.groupby(['utm_source','month']).id.count().reset_index()  
print(click_source_by_month)

click_source_by_month_pivot = click_source_by_month.pivot(index='utm_source',columns='month',values='id').reset_index()

print(click_source_by_month_pivot)  
Merge two dataframes
#this will happen if both dataframes have one column common
sales_vs_targets = pd.merge(sales,targets)  
#another way to merge
new_df = orders.merge(customers)  
#multile merge
new_df = orders.merge(customers).merge(products)  
#merge of columns name do not match
orders_products = pd.merge(orders,products.rename(columns={'id': 'product_id'}))  
#another way to merge if columns name do not match
orders_products = pd.merge(orders,products, left_on='product_id', right_on='id', suffixes=['_orders','_products'])  
#outermerge
pd.merge(company_a, company_b, how='outer')  
#left
store_a_b_left = pd.merge(store_a, store_b, how='left')  
#right
store_a_b_right = pd.merge(store_a, store_b, how='right')  
#concatenation
pd.concat([df1, df2])  
Query DataFrame
#simple condition
crushing_it = sales_vs_targets[sales_vs_targets.revenue > sales_vs_targets.target]  
#multiple conditions
results= all_data[(all_data.revenue > all_data.target) &(all_data.women>all_data.men)]  
print(results)  

Python : NumPy Basics

NumPy, which stands for Numerical Python.

NumPy has many uses including:

  • Efficiently working with many numbers at once
  • Generating random numbers
  • Performing many different numerical functions (i.e., calculating sin, cos, tan, mean, median, etc.)
Importing numpy

This is an universal way of importing NumPy and using np

import numpy as np  
NumPy Arrays

A NumPy array is a special type of list.
Each item can be of any type (strings, numbers, or even other arrays). You can even have different types of items in the same array.
Its best suited for numbers as it gives extra power for mathematical operations

import numpy as np

test_1 = np.array([92, 94, 88, 91, 87])  
print(test_1)  
print(type(test_1))

my_list = [1, 2, 3, 4, 5, 6]  
my_array = np.array(my_list)

print(my_list)  
print(type(my_list))  
print(my_array)  
print(type(my_array))  

Output is as bellow

[92 94 88 91 87]
<type 'numpy.ndarray'>  
[1, 2, 3, 4, 5, 6]
<type 'list'>  
[1 2 3 4 5 6]
<type 'numpy.ndarray'>  
reading from file
csv_array = np.genfromtxt('sample.csv', delimiter=',')  

NumPy arrays are more efficient than lists. One reason is that they allow you to do element-wise operations.

# With a list
l = [1, 2, 3, 4, 5]  
l_plus_3 = []  
for i in range(len(l)):  
    l_plus_3.append(l[i] + 3)
# With an array
a = np.array(l)  
a_plus_3 = a + 3  
>>> np.sqrt(a)
array([ 1, 1.41421356, 1.73205081, 2, 2.23606798, 2.44948974])  
Statistics with NumPy

Lets is consider following data set

import numpy as np

water_height = np.array([4.01, 4.03, 4.27, 4.29, 4.19,  
                         4.15, 4.16, 4.23, 4.29, 4.19,
                         4.00, 4.22, 4.25, 4.19, 4.10,
                         4.14, 4.03, 4.23, 4.08, 14.20,
                         14.03, 11.20, 8.19, 6.18, 4.04,
                         4.08, 4.11, 4.23, 3.99, 4.23])
# Calculate mean
np.mean(water_height)  
# Sort np array
np.sort(water_height)  
# Find median
np.median(water_height)  
# Find percentile value
np.percentile(water_height, 75)  
# Find standard Deviation
np.std(water_height)  
# Percentage of values greater than or equal to 4
np.mean(water_height >= 4)  

This works well for single dimensional array. Lets look at it how it looks for two dimensional array

ring_toss = np.array([[1, 0, 0],  
                          [0, 0, 1], 
                          [1, 0, 1]])
np.mean(ring_toss)  
0.44444444444444442  
# To find the means of each interior array, we specify axis 1 (the "rows"):
np.mean(ring_toss, axis=1)  
# To find the means of each index position, we specify axis 0 (the "columns"):
np.mean(ring_toss, axis=0)  

Python : Loops and iteration

In python loops or iteration can be achieved using following methods

  • while statement
  • for statement
While statement
  • Loop will run till condition is satisfied, once condition is satisfied, it will not execute the loop.
  • Values are checked before execution of block of code
count = 3

print('Starting while loop') 
while count < 5: 
      print('count is : ', count)
      count =count + 1

output

$ python3.6 loop02-while.py 
Starting while loop 
count is : 3 
count is : 4

For statement

following are different combination of for statement

cities = ['Pune','Mumbai','Hyderabad','Delhi','Bangalore']

for city in cities: 
    print('Current city is : ', city)

fname = 'Jasaon'

for char in fname: 
    print('char is : ', char)

for number in range(0, 10): 
    print('Current number is : ', number)

for x in range(2, 20, 3):
    print('Output is : ',x)

And output is

$ python3.6 loop02-for.py 
Current city is : Pune 
Current city is : Mumbai 
Current city is : Hyderabad 
Current city is : Delhi 
Current city is : Bangalore 
char is : J 
char is : a 
char is : s 
char is : a 
char is : o 
char is : n 
Current number is : 0 
Current number is : 1 
Current number is : 2 
Current number is : 3 
Current number is : 4 
Current number is : 5 
Current number is : 6 
Current number is : 7 
Current number is : 8 
Current number is : 9 
output is : 2
output is : 5
output is : 8
output is : 11
output is : 14
output is : 17
Control statements

pass

The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute

continue

Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

break

Terminates the loop statement and transfers execution to the statement immediately following the loop.

Sample program
rollNumbers = [0,1,2,3,4]


print('For loop with pass statemend started')  
for x in rollNumbers:  
    print('Current Number is :', x)
    if x == 2:
        pass
    print('This is inside loop for pass', x)
print('For loop with pass statemend ended')        

print('For loop with continue statemend started')  
for x in rollNumbers:  
    print('Current Number is :', x)
    if x == 2:
        continue
    print('This is inside loop for continue', x)
print('For loop with continue statemend ended')        

print('For loop with break statemend started')  
for x in rollNumbers:  
    print('Current Number is :', x)
    if x == 2:
        break
    print('This is inside loop for break', x)
print('For loop with break statemend ended')  

output is as below

$ python3.6 loop02-pass-continue-break.py 
For loop with pass statemend started  
Current Number is : 0  
This is inside loop for pass 0  
Current Number is : 1  
This is inside loop for pass 1  
Current Number is : 2  
This is inside loop for pass 2  
Current Number is : 3  
This is inside loop for pass 3  
Current Number is : 4  
This is inside loop for pass 4  
For loop with pass statemend ended  
For loop with continue statemend started  
Current Number is : 0  
This is inside loop for continue 0  
Current Number is : 1  
This is inside loop for continue 1  
Current Number is : 2  
Current Number is : 3  
This is inside loop for continue 3  
Current Number is : 4  
This is inside loop for continue 4  
For loop with continue statemend ended  
For loop with break statemend started  
Current Number is : 0  
This is inside loop for break 0  
Current Number is : 1  
This is inside loop for break 1  
Current Number is : 2  
For loop with break statemend ended  

Please note:
pass : nothing changes, pass statement is nothing but filler used for syntactical requirement

continue: This skips further processing in loop for current iteration only and it jumps to next iteration. Please note, second print statement is not printed for number 2

break: This terminates current iteration as well as whole loop and goes to next statement in python program

Python: Conditional statements

Python support singular IF,  If-ELSE, IF-ELIF-ELIF-ELSE statements. Sample formats are as below.

if b > a:
    print("b is greater than a")

if b > a:
    print("b is greater than a")
else:
    print("A is greater than b")

if A == 10:
    print("A is 10")
elif A == 20:
    print("A is 20")
else:
    print("A is neither 10 nor 20")

Conditional statements can be used as nested statements.

Boolean Expressions

Boolean expression is an expression that is either True or False. Python supports usual Boolean expressions. examples as below

Test Symbol Example
Equality == a == b
Inequality != a != b
Less than < a < b
Less than or equal <= a <= b
Greater than > a >b
Greater than or equa >= a >= b

Logical Operatiors

Python supports usual logical operators. examples as below. Symantic of these operator is similar to their English language meaning.

 

Operator Symbol Example
and and (a and b)
or or (a or b)

Try and Except

Since python is runtime programming language, many times errors are detected while running the job and whole program might fail due to any error. In such cases, you can use try..except.

Try .. except statement is like insurance in python

 

input_age =input(" Enter your age : ")

# days=int(input_age)*365

try:
days= int(input_age)*365
print("days inside try and except :" , days)
except Exception as e:
print(" Error :", e)