Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

Thursday, July 8, 2021

Infographic Note on Numpy Arrays in Python with Questions and Answers on procedure to handle random numbers in an Array or Matrix Data Structure

 

Discussed the following questions in the above post :

Q1)If a programmer does not know the last number or position , then how can a programmer show the last position element of an array in Numpy ?

Q2) Using Slicing mechanism , retrieve the first two elements of the numpy array ?

Q3) Using Slicing mechanism , retrieve the last two elements of the numpy array ?

Infographic Note on Numpy Arrays in Python with Questions and Answers on Random Numeric Data Creation and Data Retrieval upon positional indexes

 


Discussed the following questions in the above post :

Q1) Give an example of SEED function and how to use it .

Q2) What is One-Dimensional indexing ?

Q3) Print first , last position elements of a Numpy Array

Q4) Print seond , third position elements of a Numpy Array

Q5) How to identify last number of numpy array ?

Infographic Note on Numpy Arrays in Python with Questions and Answers on Array and Matrix Creation and Data Retrieval

 


Q1) Print range between 1 to 20 and show 5 integer random numbers

Q2) Print range between 1 to 100 and show 10 random integer numbers

Q3) Print range between 1 to 1000 and show 20 random integer numbers

Q4) Print a matrix with range between random number - 5 rows and 5 cols integer random numbers

Q5) Print a matrix range between random number - 10 rows and 6 cols integer random numbers in a             matrix format

Tuesday, July 6, 2021

An Infographic Note on Strip() Function over a Complex String in Python with syntax , usage and examples

 


An Infographic Note on RStrip() Function in Python with syntax , usage and examples

 


An Infographic Note on LStrip() Function in Python with syntax , usage and examples

 


An Infographic Note on LStrip() Function in Python with syntax , usage and examples

 


An Infographic Note on Strip() Function in Python with syntax , usage and examples

 


An Infographic Note on SwapCase() Function in Python with syntax and usage and examples

 


Friday, April 16, 2021

Class Methods in Python - An example of creation of a Class Method in Python along with sample code

 


    Class Methods in Python


* These are the set of Methods are act on class level . Class Methods are the methods which act on the class variables or static variables .

 

* The Class Methods can be written using @classmethod decorator above them .

 

* For example , 'cls.var' is the format to refer to the class variable which includes methods which can be generally called using the classname.method()

 

* The process which is commonly needed by all the instances of the class is handled by the class methods

 

* In the given example below, one can see the instance of the class which is handled by the class methods . The same program can be developed using an example class which can be used in the following manner .

 

* In the example , one can refer to a sample Bird Class for more insight into the description and elaboration of a Method Class . All the birds in nature have only 2 wings (as we mostly see , but there are abberations ofcourse ). Here , one can take an instance of a Bird Class . All the Birds in Nature have 2 wings , therefore one can take 'wings' as a class variable and a copy of this class variable is

available to all the instances of the Bird Class . In this Bird class , we will create a hypothetical method which applies to the functions that a Bird can operate upon and thus will make use of this method that is "fly" method (... to fly above the sky ... fly rhymes good with sky .. please accept from me a satirical High of Hi ... I am poetic too you know :P)

 

* So where was I .. ya I was at the forefront of creation of a class which would take into its ambit a Bird which would have some generic applicable class variables all applicable to the organism class of Birds like all Birds have a pair of wings .. that makes the count to 2 . And birds fly .. which is a method attributed to birds . These two class variables and class methods would be made use of to instantiate a generic sample class of a Bird

 

* So lets create a Bird with two wings and this flies too (I am sorry to know that when God created a Bird like Penguin , God forgot to add the the instance function "fly" to its class genus ... therefore I shall also keep this off the charts for penguins,kiwis and take only those birds which can fly ... up above the sky )

 

* Without further ado .. lets get to this class creation which would take into effect all the common features of all the instances of a Bird Class

  

==================================

==================================

# understanding class methods

class Bird:

     # calling a class variable

     wings = 2

     # creating a class method @classmethod

     def fly(cls,name):

     print('{} flies with {} wings'.format(name,cls.wings))

 

#display information of 2 birds

Bird.fly('Garuda')

Bird.fly('Pigeon')

Bird.fly('Crow')

Bird.fly('HummingBird')

Bird.fly('Eagle')

 

==================================

==================================

Output

Garuda flies with 2 wings

Pigeon flies with 2 wings

Crow flies with 2 wings

HummingBird flies with 2 wings

Eagle flies with 2 wings

==================================

==================================

 

Wednesday, April 7, 2021

MapReduce Overview - with Example and Architecture ( an analysis and interpretation )

 


MapReduce Overview

 

* MapReduce is a parallel programming framework for speeding up large scale data processing for computation tasks

 

* MapReduce achieves its performance with minimal movement of data on distributed file systems on Hadoop Clusters to achieve real-time results

 

* There are two major pre-requisites for MapReduce Programming

(1) The first pre-requisite of MapReduce Programming is that tha application must lend itself to parallel programming

(2) The data for the applications can be expressed in terms of key-value pairs

 

* MapReduce Processing is similar to UNIX sequence which is can be expressed in the form of pipe separated values data structure eg UNIX command .

 

grep | sort | count textfile.txt

 

This upper command produces a "wordcount" within the output text document which is referred to as "textfile.txt"

 

* There are three commands in the sequence and they work as follows

(a) grep is a command which is used to find and read a text file and create an intermediate file with one word

(b) sort is a command that works upon an intermediate file and produces an alphabetically sorted list of words

(c) count is a command which works on a sorted list to produce the number of occurrences of each word and display the results to a user in a "word-frequency" pair format

 

For example : Suppose a file "file.txt" contains the following text :

" I stay at Wonderville in the city of Gods . I am going to a picnic near our house . Many of our friends are coming too . You are welcome to join us . We will have fun"

  

The outputs of grep , sort and wordcount command on this text are in the following manner  


* For the sake of simplicity the case taken into account is of a relatively smaller text file . Had the text been very large , then it would have taken the computer a long amount of time to process the longer text document

* In order to process such a file one would take into account the service of Parallel Processing where MapReduce algorithm speeds up the computation process by reading and processing small chunks of file by different computers in parallel mode .

 

* Taking this into consideration , if the same logic could be applied to a file , then it could be said that the file object could be broken down into 100 smaller chunks where each of the chunks could be processed at a separate computer using parallel processing of the requests . The total time taken to process such a file is then minimised to 1/100 of the time that a single computer/ server/processor would have taken to accomplish the task of the file division .

 

* Now after the processing at separate nodes/processors is done separately/ parallely , the results of the computation on smaller chunks are done separately and later on aggregated together to produce a composite result . The results of the outputs from the various chunks are combined by another program called as "Reduce Program "

 

* The Map step distributes the full job into smaller tasks that can be done over separate computers using only a part of the data set . The result of the Map Step can be considered as intermediate results . The Reduce step reads the intermediate results and combines all of the results to produce the aggregated result .


(some results in the above diagram are not in proper order within the "SORT" column)

 

* As the concrete programmatic level breakdown of the logical handling of Mapping and Reducing the requisite steps are out of the context for this article , I am not able to show the background level code of the flow of each of the ensuing steps within the entire process .

 

* However one can only imagine the process through the given example where all the requisite data have been provided in the form of a text file which has all the required data and from the required data formation of the singular data chunks , sorting of the dataset (which is a standard procedure available in all database systems ) based on one or multiple fields can be performed . Therefore the intermediate results should have a key field upon which the sorting operation could be performed .

 

* In order to manage the variety of data structures stored over a file system , data is stored within it in the form of one - key and non-key attribute values . Therefore , data is represented in the form of a key-value pair . Along with that , the intermediate results would be also stored in the form of key-value pair format intermediate results would be also stored in the form of key-value pair format .

 

* Hence , one of the most significant things to remember about the manner of storage of all the input and output data over a "MapReduce Parallel Processing System is that the input data and the output data are all represented in the form of key-value pairs "

 

* A Map step reads all data in the form of key-value pair format . The programmer working upon the storage and managment of the stored data decides upon the characteristics and attributes of the key and value fields .

 

* The Map Step produces results in the form of key-value pair formats . However , the characteristics of the keys produced by the Map step need not be in the same format . Therefore , all this data is called as key2-value2 pairs

 

* The Reduce Step reads the key2-value2 pairs and produces an output using the same keys that were used for reading the data .

 

* The overall process of this entire MapReduce process can be seen in the

following manner :





 

Friday, March 12, 2021

Creating Classes and Objects in Python



     Creating Classes and Objects  in Python

 

* One can create a class "Person" in which "Andy" and "Bandy" are the names of the objects within the class

 

* A Class can be created by using the keyword "class" infront of the name of the class that one is trying to make .

 

* A Class describes the attributes and actions performed by the objects of the class . Therefore , one can write the attributes ( Variables ) and actions ( Functions ) in the class as :

 ===================================================

# This is a Class with the name Person attributes means variables

  Class Person:

  name = 'Andy'

  age = 20

# actions means functions

  def talk(cls):

        print(cls.name)

        print(cls.age)


  

* One can observe from the given code that the Person Class has two variables and one function . The function that is written in the class is called a Method .

 

* If someone wants to use this Class , then one should create an Object for the class first in the given manner .

 p1 = Person()

 Here , p1 is an object of the Person Class . Object 'p1' represents memory to store the actual data . The memory needed to create object 'p1' is provided by the PVM . One may be able to observe the function / method in the class in the given manner:

 

def talk(cls):

 

Here , 'cls' represents a default parameter that indicates the class of the object . Therefore , cls.name refers to the class variable 'Andy' . One can call the "talk()" method to display the object's details as :


p1.talk()

===========================================================

Friday, March 5, 2021

Dictionary Methods in Python for processing of constituent Key and Value Pair Data Objects

 

Dictionary Methods in Python


* There are various methods to process the elements of a dictionary

 * These methods are used for retrieving and manipulating the contents of a dictionary

 * These various methods have been summarised in the given description table


 ===================================

Methods to process Dictionaries in Python

===================================

 1) clear()

dict.clear()

This method removes all the key-value pairs from the dictionary 'd' .

 

2) copy()

d1 = dict.copy()

This method copies all the elements from the dictionary 'd' into a new dictionary object 'd1' .

 

3) fromkeys()

dict.fromkeys(s[,v])

This creates a new dictionary with keys from a given sequence 's' and all the values set to 'v'

 

4) get()

dict.get(k[,v])

This returns all the values associated with the key 'k' . If the key is not found , then it returns 'v'

 

5) items()

dict.items()

This returns an object that contains key-value pairs of dictionary item 'dict' . The key value pairs are stored as tuples in the objects .

 

6) keys()

dict.keys()

This returns a sequence of keys from the dictionary object "dict" .

 

7) values()

dict.values()

This returns a sequence of values from the dictionary object "dict"

 

8) update()

dict.update(x)

This method adds all elements from the dictionary 'x' to the dictionary item object 'dict'

 

9) pop()

dict.pop(k[,v])

This method removes the key 'k' and its associated value from the dictionary object "dict" and returns the value associated with that key "v" is returned . If the key is not found and "v" is not mentioned then "KeyError" exception error is raised .

 

10) setdefault()

dict.setdefault(k[,v])

If the key 'k' is found , then its value is returned and if the key is not found , then the k,v pair is stored into the dictionary 'd'. In the given program , we are going to retrieve the keys from a dictionary object using the keys() method . The keys() method returns dict_keys object that contains only keys . We will be also able to retrieve the values from the dictionary object using the values() method . This method "values()" returns all the values in the form of a dict_values object . Similarly , the items() method can be used to retrieve all the key-value pairs into the "dict_items"

method can be used to retrieve all the key-value pairs into the "dict_items" object .

 

==============================================

 

Program

A Python program to retrieve keys , values and key-value pairs from a dictionary object

# dictionary methods - create a dictionary with employee details

dict =

{

  'Name' : 'D001' ,

  'Id' : '001',

  'Salary' : 1000

}

 

# print the entire dictionary

print(dict)

# display only keys

print(" values in dict = ", dict.values())

# display both key and value pairs as tuples

print(" Items in Dictionary =", dict.items())

 

Output :

{

   'Name' : 'D001' ,

   'Id' : '001',

   'Salary' : 1000

}

Keys in dict =dict_keys(['Name','Id','Salary'])

Values in dict = dict_values(['D001','001','1000'])

Items in dict = dict_items([('Name','D001'),('Id',001),('Salary',1000)])

 

In the given program, we are creating a dictionary by entering the elements from the keyboard and when the user enters the elements from the keyboard inside the curly braces , then the values inside the dictionary object are treated as key value pairs of a dictionary by using the eval() function . Once the elements are entered , one can find the sum of the values using the sum() function over the values of the dictionary .


The screenshot for the used code implemented over Jupyter Notebook is given as below :



An article on - Dictionaries in Python ( with example code and result of Dictionary Creation )

                         Dictionaries in Python


 

* A dictionary represents a group of elements arranged in the form of key-value pairs .

 

* In the dictionary , the first element is considered as "key" and the immediate next element is considered as the "value"

 

* The key and the associated value are separated by the help of a colon (:) operator

 

* All the key-value paris in a dictionary are inserted in curly braces { }

 

* One can take a dictionary by the name "dict" that contains the employee details in the following manner :

 

dict_01 =

{

'Name' : 'Gareeb' ,

'Id' : 001 ,

'Salary' : 0000

}

 

* The name of the dictionary item object in the given example is "dict_01"

 

* The first element in the dictionary is a string object called "Name" and its associated value is "Gareeb" .

The second item in the element is "Id" and its associated value is "001" . The third object in the dictionary object is "Salary" and its associated value is "0000"

 

* The above example is that of a dictionary object with its identifier name as "dict_01" . The dictionary item object is holding three values within it in the form of key and value pair .

 

* From this , we can get to see that there are 3 pairs of keys and values in the dictionary which can be shown in the below figure .


  

 

 

* To briefly explain the process of creation of a dictionary object , one can write a

program in the given manner :

 

# creating dictionary with key-value pairs

dict_01 =

 {

    'Name' : 'Gareeb' ,

    'Id':001 ,

    'Salary':0000

}

# Here a dictionary object is created with the following items in the form of

# key-value pairs - Name , Id and Salary

# If someone wants to retrieve the requisite values from the dictionary then one

# can do the following to retrieve the necessary objects

print(' Name of Employee ',dict_01['Name'])

print(' Id of Employee ',dict_01['Id'])

print(' Salary ',dict_01['Salary'])

 

The Code and Output for the same can be seen in the below given screenshot: