This technical blog is my own collection of notes , articles , implementations and interpretation of referred topics in coding, programming, data analytics , data science , data warehousing , Cloud Applications and Artificial Intelligence . Feel free to explore my blog and articles for reference and downloads . Do subscribe , like , share and comment ---- Vivek Dash
Wednesday, December 30, 2020
Monday, December 28, 2020
Friday, December 25, 2020
Thursday, December 24, 2020
Lists and Tuples in Python
Lists and Tuples in Python
* A sequence is a datatype that represents a group of elements
* The purpose of any sequence is to store and process a group of elements
* In Python ; strings , lists , tuples and dictionaries are very important type of sequence datatypes
* All sequences allow some common operations like indexing and slicing
====================================================================
Q) What is a List ?
* A list is similar to an array that consists of a group of elements or items
* As an array , a list can also store elements
* But there is a major difference between an array and a list
* An array can store only one type of element whereas a list can store different types of elements
* Therefore , lists are more versatile and useful than an array
* Lists are the most used among all the datatypes in Python programs
====================================================================
* Let us consider a case of taking a List in our daily lives ... example , if we take marks of a student in 5
subjects such as :
80 , 85 , 82 , 94 , 86
then one can see that all of these belong to the same datatype that is an integer type . Therefore , one can
represent such elements as an array . But we need more information about the student , like the roll
number of the student , name , gender , marks etc of the student . In totality , the full information about the
student looks such as this :
[ 101,Andy,M,80 , 85 , 82 , 94 , 86 ]
Here .. one can see that there are various types of data . Here , the roll number shown for the person is
an integer i.e. 101 . Gender ('M') is a character and the marks shown are again in the form of integers
[ 80 ,85 , 82 , 94 , 86 ]
In general .. this type of information is stored and processed . This type of information cannot be stored
in an array as the data is heterogeneous in nature and an array stores only homogeneous type of data that
means an array stores only one type of elements .
Therefore , in order to store such type of data one needs to use a list datatype . A list can store different types of elements . In order to store the student's information , one can create a list in the following manner :
student = [101,'Andy','M',80,85,82,94,86]
One may observe that the elements of the 'student' list are stored within square braces [] . One can create
an empty list without any elements by writing empty square braces such as :
empty_list = [ ] # this is an empty list
Therefore , one can create a list by embedding the elements inside a pair of square braces [ ] . The elements in the list are separated by a comma ( , ) . In order to view the elements of a list as a whole , one can pass the list name to the print() function as in the following manner :
print(student)
The list appears in the manner below :
[101,'Andy','M',80 , 85 , 82 , 94 , 86]
Indexing and slicing operations are common operations that can be done over lists . Indexing represents accessing elements by their position number in the list . The position numbers start from 0 onwards and are written inside square braces as :
student[0] , student[1] etc .
This means that student[0] represents 0th element , student[1] represents 1st element and so forth . For example , in order to print the student's name one can write :
print(student[1])
Output
Andy
Slicing represents extracting a piece of the list by mentioning the starting and the ending position numbers . The general format of slicing is [ start : stop : stepsize ]. By default , 'start' will be 0 , 'stop' will be the last element and 'stepsize' value happens to be 1 .
Example - student[0:3:1] represents a piece of the list containing 0th to 2nd element of the list .
print(student[0:3:1])
output
[101,'Andy','M']
The above statement can also be written in another format as given :
print(student[:3:])
output
print(101,'Andy','M')
===================================================================
Python program to create lists with different types of elements
===================================================================
# create lists with integer numbers
num = [ 10 , 20 , 30 , 40 , 50 ]
print(' Total List = ',num)
print(' First = %d , Last = %d ' %(num[0],num[4]))
# create a list with strings
names = ["Andy","Barry","Carrie","Derry"]
print(' Total List =',names)
print(' First = %s , Last =%s' %(names[0],names[3]))
# create a list with different data type of elements
x = [ 10 , 20 , 30 , "Garry" , " Barry " ]
print(' Total List = ', x)
print(' Total List = ', x)
print(' First = %d , Last = %s ' %(x[0],x[5])))
Output
Total List = [10,20,30,40,50]
First = 10 , Last = 50
Total List = ["Andy","Barry","Carrie","Derry" ]
First = Andy , Last = Derry
Total List = [ 10 , 20 , 30 , "Garry" , " Barry " ]
First = 10 , Last = Barry
Lists and Tuples in Python
Lists
and Tuples in Python
* A sequence is a datatype that represents
a group of elements
* The purpose of any sequence is to store
and process a group of elements
* In Python ; strings , lists , tuples and
dictionaries are very important type of sequence datatypes
* All sequences allow some common
operations like indexing and slicing
====================================================================
Q) What is a List ?
* A list is similar to an array that
consists of a group of elements or items
* As an array , a list can also store
elements
* But there is a major difference between
an array and a list
* An array can store only one type of
element whereas a list can store different types of elements
* Therefore , lists are more versatile and
useful than an array
* Lists are the most used among all the
datatypes in Python programs
====================================================================
* Let us consider a case of taking a List
in our daily lives ... example , if we take marks of a student in 5
subjects such as :
80 , 85 , 82 , 94 , 86
then one can see that all of these belong
to the same datatype that is an integer type . Therefore , one can
represent such elements as an array . But
we need more information about the student , like the roll
number of the student , name , gender ,
marks etc of the student . In totality , the full information about the
student looks such as this :
[ 101,Andy,M,80 , 85 , 82 , 94 , 86 ]
Here .. one can see that there are various
types of data . Here , the roll number shown for the person is
an integer i.e. 101 . Gender ('M') is a
character and the marks shown are again in the form of integers
[ 80 ,85 , 82 , 94 , 86 ]
In general .. this type of information is
stored and processed . This type of information cannot be stored
in an array as the data is heterogeneous in
nature and an array stores only homogeneous type of data that
means an array stores only one type of
elements .
Therefore , in order to store such type of
data one needs to use a list datatype . A list can store different types of
elements . In order to store the student's information , one can create a list
in the following manner :
student = [101,'Andy','M',80,85,82,94,86]
One may observe that the elements of the 'student'
list are stored within square braces [] . One can create
an empty list without any elements by
writing empty square braces such as :
empty_list = [ ] # this is an empty list
Therefore , one can create a list by
embedding the elements inside a pair of square braces [ ] . The elements in the
list are separated by a comma ( , ) . In order to view the elements of a list
as a whole , one can pass the list name to the print() function as in the
following manner :
print(student)
The list appears in the manner below :
[101,'Andy','M',80 , 85 , 82 , 94 , 86]
Indexing and slicing operations are common
operations that can be done over lists . Indexing represents accessing elements
by their position number in the list . The position numbers start from 0
onwards and are written inside square braces as :
student[0] , student[1] etc .
This means that student[0] represents 0th
element , student[1] represents 1st element and so forth . For example , in
order to print the student's name one can write :
print(student[1])
Output
Andy
Slicing represents extracting a piece of
the list by mentioning the starting and the ending position numbers . The
general format of slicing is [ start : stop : stepsize ]. By default , 'start'
will be 0 , 'stop' will be the last element and 'stepsize' value happens to be
1 .
Example - student[0:3:1] represents a piece
of the list containing 0th to 2nd element of the list .
print(student[0:3:1])
output
[101,'Andy','M']
The above statement can also be written in
another format as given :
print(student[:3:])
output
print(101,'Andy','M')
===================================================================
Python program to create lists with
different types of elements
===================================================================
# create lists with integer numbers
num = [ 10 , 20 , 30 , 40 , 50 ]
print(' Total List = ',num)
print(' First = %d , Last = %d '
%(num[0],num[4]))
# create a list with strings
names =
["Andy","Barry","Carrie","Derry"]
print(' Total List =',names)
print(' First = %s , Last =%s'
%(names[0],names[3]))
# create a list with different data type of
elements
x = [ 10 , 20 , 30 , "Garry" ,
" Barry " ]
print(' Total List = ', x)
print(' Total List = ', x)
print(' First = %d , Last = %s '
%(x[0],x[5])))
Output
Total List = [10,20,30,40,50]
First = 10 , Last = 50
Total List =
["Andy","Barry","Carrie","Derry" ]
First = Andy , Last = Derry
Total List = [ 10 , 20 , 30 ,
"Garry" , " Barry " ]
First = 10 , Last = Barry
Wednesday, December 23, 2020
Core Python - Python program to create two decorator functions and use them in calling functions
Core Python - Python program to create two decorators
# decorator increments value of function by 5
def decorator1(fun):
def inner():
value = fun()
return value + 5
return inner
# decorator that doubles value of a function
def decorator2(fun):
def inner():
value = fun()
return value*2
return inner
# take a function to which decorator could be applied
def num():
return 10
# call num() function and apply decor1 and then decor
result_fun = decorator1(decorator2(num))
print(result_fun)
print(result_fun)
Output
25
One can apply the decorators to num() function using '@' symbol .
Generators in Python
Generators in Python
Generators are functions that return a sequence of values . A generator function is written like an ordinary function but it uses 'yield' statement . This statement is useful to return a value .
Example , one can write a generator function to return numbers from x to y .
# here , the generator function name is 'mygen'
def mygen(x,y):
while x <= y :
yield x
x = x + 1 # increment value of x by 1
One can call this function by passing the values 5 and 10 as follows :
gen_obj = mygen(5,10)
Here , the mygen() function returns a generator object that contains sequence of numbers as returned by 'yield' statement . So 'gen_obj' refers to the generator object with a sequence of numbers from 5 to 10 . One can display the numbers from the "gen_obj" .One can display the numbers from "gen_obj" using display the numbers from "gen_obj" using a for loop as :
for i in g:
print(i,end=' ')
In the following program , one can create a generator function that generates numbers from x to y and display those numbers .
Python program to create a generator that returns a sequence of numbers from x to y
# generator returns sequence from x to y
def mygen(x,y):
while x <= y:
yield x
x = x + 1
# fill generator object with 5 and 10
gen_obj = mygen(5,10)
# display all numbers in the generator
for i in gen_obj:
print(i,end = ' ')
Output
5 6 7 8 9 10
Once the generator object is created , we can store the elements of the generator into a list and use the list as we want . For example , to store the numbers of generator 'gen_obj' into a list 'lst' one can use the list() function as :
lst = list(gen_obj)
Now , the list 'lst' contains the elements:
[5 6 7 8 9 10]
If we want to retrieve element by element from a generator object , one can use the next() function as :
print(next(gen_obj))
which would display the first element in "gen_obj". And , when we call the above function the very next time , it will display the second element in "gen_obj" and by repeatedly calling the next() function , we would be able to display all the elements of "gen_obj" .
In the following program , a simple generator is created that returns 'A','B','C'
def mygen():
yield 'A'
yield 'B'
yield 'C'
Here , mygen() is returning 'A','B','C' using yield statements . Here , yield statement returns the elements from a generator function into a generator object . And , when we call the mygen() function , it is expressed as follows :
gen_obj = mygen()
Here , the characters 'A','B','C' are present within the generator object "gen_obj" and they can be referred and called into use using the next(gen_obj) function to refer to the elements .
Thursday, December 10, 2020
How Bitcoin Works and why is bitcoin so popular
How Bitcoin Works and why is bitcoin so popular
* Value of Bitcoin in 2010 = 0 INR
* Value of Bitcoin in 2020 = 15 lakhs INR
* Bitcoin has reached its all time highest price margin in the year 2017
* In 2008 , a paper was published by Satoshi Nakamato in which the abstract about Bitcoin was mentioned in which a purely peer-to-peer version of electronic cash that would allow online paymentsto be sent directly from one party to another without going through a financial institutions . In this format , digital signatures provide part of the solution , but the main benefits are lost if a trusted third party is required to prevent double spending
.* Some years before Cryptocurrency was a new concept that was started to undergo financial transactions not controlled by financial institutions around the world but with due course of time
* Crypto-Currency variant Bitcoin doesn't have any central authority or point of control ; the way Indian National Rupee is controlled by RBI , American Dollar is controlled by USA Central bank or Federal Reserve Bank
* Some years before Cryptocurrency like Bitcoin , Ethereum , Litecoin were just an idea but now it has become a global phenomenon with thousands / millions / billions worth of trading over the same .
* In order to understand fully well the concept of Bitcoin and Cryptocurrency one needs to dig deeper into the Economic and Trading System of the past
* The established financial systems in place are based on the premise of trust and cooperation between the public and the government with banks playing the middlemen entrusted with carrying out issuing and authorization of money
* The entire economic system has been built over bullion value of money that is the amount of gold / silver held in reserve with a financial institution of a country . But as because it was impractical and risly to carry gold safely and use it as a medium of exchange over any transaction in a day to day manner , because of which cash as a convenient and eassier medium of transaction was created sothat ease of transaction and business could be carried out .
* When a depositor deposits money in a bank , the bank gets the authority to play around with your deposited money in whichever manner the banks want . They lend the deposited money to companies and individuals in the form of loans because of which these companies put the money into business or developmental activities and because of the profit accrued by the companies , the banks get interest due to the profitable ventures of the companies for which the money had been lent for and because of which the individuals get interest over their invested money .
* Recently it has come to light , how the banks handle the money in very irresponsible ways by lending big big amount of money to industrialists and big companies which swindle the money by not able to do justice to the lent money for which the money becomes bad debt , NPAs for the bank and thus the bank and the investor/depositor both suffer the consequences for having invested in such banks
* For these specific reasons , the people behind the concept of cryptocurrency and bitcoin did not want the financial institutions aided by the governments have so much authority and control over the monetary and financial system and thus crypto-currency was launched as an alternative economic and financial system for people as depositors which would be also out of the purview of control by the authorities .
* How do Crypto-currencies work ? In order to fully understand the working and functioning of the operation behind Crypto-currency , the user has to have a thorough understanding of the concepts of advanced mathematics and computer science theories and concepts . But if someone wants to start trading and investing , then one may not have in-depth understanding of the required paradigms rather a general knowledge of some of the underlying concepts would do
* All the transactions related to these various formats of crypto-currencies is tracked and recorded in a ledger .This ledger is available for viewing to all the people / participants under the bitcoin mining system . The people who run these systems are called as Miners . Their work is to verify the various transactions happening over the entire transaction chain .
* Suppose a Bitcoin transactioner wants to transfer 2 bitcoins to another person in the participative blockchain network , then the duty of the Miner governing the process is to confirm the number of bitcoins held by the operants and check that the transaction gets to successful completion , but for this the miner has to solve a complicated mathematical equation
* The work of the miner is to identify the individual variables of the transaction process . This is not undertaken in a pen and paper equation solving procedure rather this is solved programmatically over computers and servers which have combinations in the form of thousands and millions .And for this process to be effectively carried out , the miners need complex and highly powerful computing systems .Once the equation is solved m the variables for the equation get added to the network and the underlying transaction process are all added to the network in the form of a chain . And when this process of creation and registration of the problem has been carried out effectively the entire module of transactions and its records become a block which is called a blockchain . In the process , miners get bitcoin as a reward of their endeavours .The miners have to do a proof of work in order to complete the process of minting or carrying out the transaction process .
* However , even if the underlying core methodologies and process of Cryptocurrency and Crypto-Technology are not understood .. its important to learn the philosophy , vision and future of crypto-currency .
* So , how to use this Crypto-Currency and Bitcoin technology .. as these days some people use Bitcoin as a form of investment not within the jurisdiction of financial institutions and some people use it as a form of alternate currency and there are some who want to replace the usage of standard currencies such as dollar , pounds , yen , rupee and bring bitcoin into usage .
* So , main usage of Bitcoin and Cryptocurrency is in the form of a store of value where bullion would be kept stored as a digital locker . For this reason , bitcoin is also called as a Digital Gold . And there is always an amount of risk associated with bitcoin , as many people consider Bitcoin as a form of Digital Currency and not inherently as a form of standard currency as everything and all operations over bitcoin is governed and controlled by computers and algorithms , and thus it has not yet gained wide acceptance as a standard form of currency which can be used in general stores .
* However , it is being said and projected that in the very near future Bitcoin could be started as a medium of exchange between various parties as many western countries have started accepting bitcoin as a medium of transaction between different parties .
* Due to many technical challenges associated with Bitcoins , acceptance of Bitcoin as a form of legitimate and easy form of currency has not yet gained much traction . In Bitcoin , all transactions go through time consuming process of verification of transactions post any form of transaction is done because of which acceptability and widespread use has not yet been adopted for it .
* But in some cases , bitcoins have already evolved as a good means of currency exchange like in the case of foregin funds transfer . Here , in this case , many banks often charge a very high fees in the name of foreign funds transfer and in many situations , sometimes a lot of money is charged as processing fee for such kind of transfers when transfer between one country to another country takes place . As there is no processing fee for any such processes and there is not any underlying great amount of time for processing like banks do when they take over 1 to 2 days for a transaction to be over because of which Remittance service companies like Western Union are under a threat of closure and same goes for some banks which process heavy charges for remittances overseas .
* A similar process also exists for credit-card services and credit card handling charges . Because of all these banks have been against standardisation and acceptance of crypto-currencies as standard mediums of exchange and currency transactions as limitations and banking procedures and payment processing were a blocking factor which Crypto-currencies identified and tried to put a stop over it , so that financial institutions such as banks ' hegemony could be broken .Recognising this as a form of threat , Banking systems and institutions did not want regulating banks and governments all over world to accept cryptocurrencies as a standard mode of exchange .
* As per reports , during the covid pandemic situation across the globe , the stock markets have felt shocks as share prices of some stocks dwindled ,mutual funds run by banks got greatly affected but in contrast to this the prices of bitcoin has kept going up and over during the ongoing crisis time as well and it is being forecasted that , the price of bitcoin has surged to an all time high even during the pandemic time and it is also being said that bitcoins price is going to escalate even higher .
* As per Reports , from 1st March 2020 to 30th November 2020 , price of Bitcoin has increased around 150% .World's largest digital payments company Paypal introduced the feature of payment processing through the form of Bitcoin and crypto-currency during november 2020 . World's largest bank J.P. Morgan has also started accepting transactions in the form of Crypto-currency which is a major turn of events as JP Morgan was earlier not in favour of accepting Bitcoin and Crypto-currency as a form of currency exchange across any country or the world .
* So , now it can be observed that there has started a new form of open-mindedness about Crypto-currencies and thus its acceptability has increased across the world and financial institutions as a standard form of monetary and currency exchange
* As per collected reports and experiences , there has been a steadfast increase in the general mindset over usage of Crypto-currency as a standard mode of exchange . In 2018 , the RBI ( Reserve bank of India), the central and regulating bank of India had issued a circular to regulated financial institutions and entities to ban dealing in virtual and crypto-based currencies leading to which Indians in general were restricted the usage of INR - Indian National Rupee as a form of currency for exchange in buying and selling of crypto-currencies leading to which cryptocurrency traders were restricted usage of Indian Banks to take loans and trade in any form of crypto-currency .So all traders running Crypto-currency platforms were not able to run their errands .. like they were not able to pay salaries to their employees , they were not able to do day-to-day transactions with this medium of exchange .
* So the question here that rises is why RBI had issues such a type of restrictive ban on the usage of Crypto-currency as a form of exchange sighting issues such as money laundering and security issues as In the Dark Web form of internet people has started accepting bitcoins as medium of exchange for drugs and ammunitions and as such law enforcement agencies work had become tougher to track these sort of transactions and the parties operating over the same format as these were outside the purview of financial systems as tracking was a problem noticed under these form of exchanges .
* Another main reason why crypto-currency was not taken as an accepted form of monetary exchange was because there were cases of fraud noticed because of such crypto-currencies where public could be swindled by fraud and illegitimate companies as genuine operating platforms and draw in money on the premise of operable trading platforms , where people could get cheated and duped of their money .Such a kind of cheating and fraud case was noticed in a case of a guy named Amit Bharadwaj who duped investors a whopping 2000 crores worth of Indian money by claiming investors in his company that he would pay double or triple the amount invested in his operable trading platform as he had opened bitcoin minting farms across China and was running huge amount of profits due to his bitcoin minting platforms .But at last , all his claims and promises to investors were found out to be false and untrue , and by running this fraud he was successfully able to dupe 2000 crore amount of money from investors and got out of India . Now as per reports , it has come to picture that he had been arrested by Delhi Police and now is out on bail and the case is pending in court . Citing all these reasons , many of the fraudsters involved in exchange of cryptocurrency were forced to shutdown which blamed institutional banking ban on their usage platforms , many of the petition holders sought legal counsel for help but turned down as most of the advocates were also apprehensive taking up the case due to the legalities and sanctions framed by the monetary regulating body of RBI . After all these hiatus , finally in January 2020 , before a three-bench judge seating panel the apex court of India , Supreme Court ruled in favour of legalising Cryptocurrency as a mode of exchange thus lifting the ban imposed by RBI (Reserve Bank of India) , citing RBI's plea for banning Cryptocurrency was a disproportionate plea as their claim that Cryptocurrency would be a affecting factor to Indian Economy and Indian Monetary Control Laws , and thus the case was dropped and ruling in favour of legalising Cryptocurrency as an alternative form of Currency was adopted in the starting of 2020 . Therefore in the direction of adoption of Cryptocurrency as a legal and alternative form of currency was adopted in 4th March 2020 and there are no restrictive laws to be placed by the judiciary body on trade over these forms of currency .
* Because of reasons such as these , and judicious execution of monetary plans by RBI , general mindset about investment in these sort of crypto-currencies was still under the hood as it lacked accountability and proper authority .However , as more and more countries and financial institutions and general population has started adopting to crypto-currency , the picture for it has begun developing traction . So , now Indians can now invest in Crypto-Currency without facing any legal ban .
* But advice to the ones seeking this new concept as a medium of investing is that , invest as much as a person can risk losing and do not take loans from any other party , institution to buy shares of these as cryptocurrencies are still volatile and value of its shares can be seen to surge and fall and there is no guarantee that the price of its shares will keep moving up and investors will keep receiving high returns over this investment . So even if there is an opportunity for making big profits , there are still chances of losing , so it all depends upon the risk taking ability of the investor which is again a similar form of intraday trading as observed in share markets . So again it boils down to risk appetite of the investor aimed towards personal investment and profit goals
Monday, December 7, 2020
Global Keyword in Python and its usage
Global Keyword in Python
Sometimes, the global
variables and the local variables have the same name. In such a case, the
function by default refers to the local variable and ignores the global
variable. Therefore, the global variable is not accessible inside the function
but it is present outside the function where it is globally accessible.
# Python program to demonstrate the concept of global and local variables, same
# name used for global and local variables in the sample code
var = 10 # this is global variable
def somefunction():
var = 20 # this is a local
variable
print(' value of variable =',var) # local var
somefunction()
print(' variable value =',var)
Output
value of variable = 20
variable value = 10
When the programmer/user wants
to use the global variables inside a function, he can use the keyword 'global'
before the variable in the beginning of the function body.
For example :
global var
In the given programmatic
method, the global
variable is made available
to the function and the
programmer can work with it
in whichever manner the programmer/user wishes to work with the provided object
or variable .In the following program, a method to see how to
work with a global variable
inside a function.
==================================
Program
A Python program to access
global variables inside a function and modify the values .
# accessing a global
variable inside a function
var = 10 #
this is a global variable
def somefunction():
global var #
this is a global variable
print(' global variable =
',var) # display global
variable
var = 20 #
modify global var value
print(' modified variable
value = ', var)
somefunction()
print(' global variable
value = ',var) # display
modified value
Output
global variable = 10
modified variable value = 20
global variable value = 20
When an issue arises when the global variable name and local variable names are same , the programmer/reviewer/user will face difficulty to differentiate between them inside a function .
For example, if there is a
global variable 'a' with some value declared above the function.... Then there
is a global variable 'a' with some value declared above the function. Here, the
programmer is writing a local variable
with the same name 'var' with some other values inside the function .
Consider the following code :
var = 10 # this is a
global variable
def myfunction():
var = 20 # this is a local variable
In a scenario, when the programmer wants to work with a global variable, then the programmer would use a 'global' keyword . Then the programmer can access only the global variable and the local variable is no more available .
The globals() function can be used to solve this problem of using
global variable which would solve the problem .This is a built-in function
which returns a table of current global variables in the form of a dictionary.
Program
# Python program to get a
copy of global variable into a
# function and work with it
# same name for global and
local variable
var = 10 #
this is a global variable
def myfunction():
var = 20 # this
var is a local variable
x = globals()['var']
print('global variable var =
', x)
print(' local variable var =
',var)
myfunction()
print(' global variable
value =',var)
Output
global variable var = 10
local variable var = 20
global variable var = 10