Saturday, July 18, 2020

Types of Function in python

In python, function can be classified into two types based on who is defining the function i.e. pre-defined function and user-defined function
per-defined function can be further classified into Built-in function and function defined in modules

function%20types.JPG

Pre-defined function

These are the function that are present already defined in the system, they are either inbuit or they are defined in some external module

Built-in function:

We have been using these function, for example print(), int(), float(), input etc. to use them we have to simply call them. example:
In [2]:
nm=input("enter your name")
print("hello ", nm)
enter your namebender
hello  bender
in the above program we have used input() and print() inbuilt function, these are already defined in python so we dont have to define or import them

Function defined in modules

These function are defined in module i.e. seperate program files and not in the program itself. import keyword has be used to get the function in calling program
syntax:
import module_name
module_name.function_name()
OR
import module_name as alias
alias.function_name()

In [4]:
import math
val=math.sqrt(25)
print(val)
5.0
In [5]:
import math as m
val=m.sqrt(64)
print(val)
8.0
here the function sqrt() is neither defined by us or nor present by default as simply calling sqrt() will through an error but this function is defined in the math module and can be imported in this script by importing the math module and calling the function by using dot operator(as dot symbolizes that sqrt belogs to math module.we will look into creating modules later

User-defined function

Now these are the function which are custome created by the user, is define in the same program and is called in the same program itself
In [7]:
def sayHello(name):
    print("Hello ", name)

sayHello("tom")
Hello  tom
Here sayHello is the user defined function. You can read all about user defined function here.
References:
1. computer science for class 12 by sumita arora
Share:

Friday, July 3, 2020

Flow of execution in a function call - python

Flow of execution in a function call:

    To understand the program execution we have to know  how a program is organized in the 
computer's primary memory(RAM). Below is a diagram given of a over simplified memory layout diagram


Heap
Dynamic memory allocation
Stack
Function call and local variable
Static/Global 
Stores Global Variable
Source Code
Stores the program instruction
Operating System
Stores the OS codes

This diagram will be used in our further discussion

    As we know that any function in order to work, has to be called with proper arguments. 
So we are going to look into how a function works, starting from the function call till result generation.  
We will consider a very simple addition program that adds two numbers.

def ad(a,b):
    return a+b

print(ad(10,30))

The above code takes 5 steps to be executed. Underneath we have listed the steps


Visualization
The now flow of execution is from top to bottom, line 1 gets executed but as it contains the def keyword 
and the interpreter gets aware that it is a function declaration, so it jumps over it. This shows that a function 
never gets executed unless called
The execution jumps to line 4, this is the point where the function ad is called inside the print function
 and an global frame is created that stores the function ad and its execution will start 
The execution control jumps to line 1 and the stack frame(memory block) for ad is created along with the
 values that are passed as arguments in the function call
Now the return statement is encountered in line 2
The sum operation is executed and the result is generated
Now the stack frame is destroyed and the value is returned to the calling function. Hence we get the output of 40

Points to remember:
1. Execution of a program starts from the first line of the code, executes one line at a time from top to bottom.
2.The function definition never changes the sequence of execution  of a program but the statements inside the function 
are only executed when the function is called.
3. A function can be called from inside of another function.
4. A function can be declared inside of another function declaration. 
5. This idea is similar to recursion.Don’t read the program from top to bottom but read the execution flow


References: 
2. Pythontutor
3. Computer Science with python Sumita arora
Share:

Sunday, June 28, 2020

Function in python - Why, What and How

Function:
Why do you I need to care about function?

Large program usually have may functional units, for example  whatsapp has many function such as chat, file sharing, video chat, audio recording and sharing, status etc. Its not a good idea to write every thing in one large file and execute everything from top to bottom(normally program are executed linearly from top to bottom). Now if we have a mechanism to split the large code into segments(pieces) and use the code segments on need basis, the application becomes more manageable and efficient. This is where function comes into existence

What is function in python programming language?

Def: A function is a named unit of a group of program statement. This named unit can be called/ invoked using is its name from other parts of the program. Function is also known as subprograms or subroutines

Advantages:
1. Modularity: function allows to divide the program to modules ans use them on need basis.
2. Complexity: large program written with function are less complex to handle
3. Code reuse: function can written once and imported into different program and used without rewriting them.
4. Debugging: A program divided into function is easier to remove errors(i.e. debugging)
5. Code maintenance and Implementing new features becomes easy

Disadvantages:
1. Adds complexity for small code(so they are not to be used on small program)
2. Makes the program slow as the called function will be loaded into the primary memory and only then its can be executed

How to use function in python.

The usage of function needs two steps:
1. Defining the function:
Syntax:
def function_name(parameter1, parameter2.....):
“““ doc_string “““
Statement1
Statement2
.
.
.
StatementN
return object
Note:
* def is keyword and must be included
* function_name follows the rules of identifier declaration
* parameters are optional and are used to supply object/values from outside  into the function
* doc_string is optional and is used to create documentation for a function
  Which can be accessed using help() function
* a function must have one or many statements with proper indents, for an empty function pass has to be written in the function  
* return is optional and used to provide/ return object to the calling program

2. Calling a function:

A function can not execute on its own and it has to be called in order to run it
Note:
* the function must be called by exact name and its case sensitive
* if the function has parameters then its must be supplied

Example
def add(a,b): # function declaration
    """
    -> this function can calculate the sum of two number
    -> takes two argument, only in numeric format i.e. integer and float
    -> return the value in float
    """
    val=float(a)+float(b)
    return float(val)

su=add(10,30) # function call with arguments
print(su)

More on function ->

Reference:
1. Core python programming - R. Nageswara rao
2. Computer science with python - Sumita Arora
Share:

Sunday, May 3, 2020

Data Frame in python pandas

Data Frame in python pandas
Pandas provide data structure like Series, DataFrame, Panels. Of all these data structure, DataFrame is the most widely used. DataFrame object of pandas allows to store 2 Dimensional data I.e in rows and columns just like in spreadsheet application like MS Excel.

Share:

Wednesday, April 22, 2020

Series Data Structure in python pandas:

Series Data Structure 
Note:
1. One Dimensional Array of index data
2. It consists of two parts
a) An array of actual data
b) An associated array of index for the data array
3. Pandas has to be imported so as to create series object

Share:

Monday, April 20, 2020

Input and Output in python program

Input and Output in Python
INPUT in a python program
Note:
1. Used to get data into the program
2. ‘input()’ function is used
3. Default return type of input() in string so has to be converted into desired format

Share:

Wednesday, April 15, 2020

Data Structure in python - LIST

Data Structure in python:
Def: Data Structure is a logical arrangement of data that imposes some protocol(rules) on how the data will be arranged, modified and accessed. Data structure are usually temporary storage only used for efficient data manipulation.
Types of data structure in python:
1. Inbuilt Data Structure: List, Tuple, Set, Dictionary, String.
2. Used Defined Data Structure : Stack, Queue, Tree etc

Share:

Sunday, April 5, 2020

Introduction to Python Pandas

Introduction to Python Pandas
The Problem( Why?)
Its said that ‘Data is the new Oil’ and its very accurate as the current world creates an enormous amount of data and all the important decision are based on these data. Now we are all familiar with spread sheet application like Microsoft Office Excel or Google Sheets. they can both store as well as perform some analytic on these data which is fine but when we talk about huge amount of data(Big Data) these solution are not sufficient and this is where Python Pandas comes in.

Share: