Wednesday, 30 September 2015

Interesting and Basic Programming facts in Python


1. Higher order functions: 

Such functions that takes  at least one or more functions as arguments, returns a function as its result are called as Higher Order functions.
Programme of calculator using Higher order function.

calculations: 

1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Modulus

Programme:


def add(a,b):
 return a+b
def sub(a,b):
 return a-b
def mul(a,b):
 return a*b
def div(a,b):
 return a/b
def modu(a,b):
 return a%b
 
def apply(fucn,a,b): #here it takes a function as argument
 return fucn(a,b)

operatn = [add,mul,div,sub,modu]

for i in operatn:
 print apply(i,5,2)
Output:7 10 2 3 1


2.  Return keyword in function 
if in a function we are using return without any value then it gives a None value. In the below example we can see the output of a programme.

Programme:
def car():
print("car is running")
return
print car()
output:
car is running
None

No comments:

Post a Comment