Tuesday, 21 May 2019

Functions its arguments

Date 21th May,2020

Python

#user defined function

def fun(a):

    #create one empty list to store only even numbers

    k=[] #k is for even number

    #to check even

    for n in a:

        if n%2==0:

            k.append(n)

    return k

#create empty list

a=[]

#enter no of elements as input

b=int(input("enter no of values "))

#use for loop to store list values into var a

for i in range(1,b+1):

    a.append(i)

print(fun(a))#function call

 

"""

enter no of values 10

[2, 4, 6, 8, 10]

"""

 

def ex1(s):

    for i in s:

        if i=='h':

            pass

        print(i)

a="python"

ex1(a)

"""

p

y

t

h

o

n

"""

Using break statement:

def ex1(s):

    for i in s:

        if i=='h':

            break

        print(i)

a="python"

ex1(a)

"""

p

y

t

"""

def ex1(s):

    for i in s:

        if i=='h':

            continue

        print(i)

a="python"

ex1(a)

"""

p

y

t

o

n

"""

Function definition:

Which contains

1.  Function heading

2.  Function body

1.  Function heading:

1.  Keyword: def

2.  Name of the function

It may contain list of parameters (it is optional)

Syntax:

def Fun_name(list of paremters):

 

2.function body:

It contains collection of statements and return statement. Here ‘return’ statement is optional.

 

3.  Function call:

A function can not execute its own. It is executed when the function is called.

def  fun(list of parameters):

 

Syntax: fun(list parameters) #calling the function

Name of the function and function call should be same.

 

Parameters or Arguments:

 

To execute the function, we call a function with variables/values.

Ex:

def ex1(s):

   …..

ex1(a)

 

Here ‘a’, ‘s’ are nothing but a function arguments or function parameters.

Actual parameters, formal parameters

Example: ‘a’ is actual parameter, ‘s’ is formal parameter

Actual parameter may be value or variable.  Where as formal parameter should be variable only.

Types of arguments:

1.  Required arguments

2.  Keyword arguments

3.  Default arguments

4.  Variable length arguments

Required arguments:

 #Required arguments

def cal(a,b,c):

    return(a+b+c)

 #function call

print(cal(2,3,4))

 #Required argument/parameter

def ex(a,b):

    c=a+b

    print(c)

 

ex(2,3)

 

 

these are the arguments which are required to be passed at the time of function call with exact match of their positions in the function call and function definition

#Required arguments

def cal(a,b,c):

    return(a+b+c)

#function call

print(cal(2,3)) #due to required arguments are not passed to function

 

Required arguments passed to a function in correct positional order. In the above example number of arguments in the function call should match exactly with the function definition.

 

Non-keyword and keyword arguments:

We can pass a variable number of parameters or arguments to a function using special symbols.

We have two type:

*args, **kwargs

1. Non-keyword arguments (*args)/arbitrary arguments

2. Keyword arguments (** kwargs)

 

 

 

###Required arguments

##def cal(a,b,c):

##    return(a+b+c)

##

###function call

##print(cal(2,3,4,5)) #Error due to required arguments are not passed

##

def cal(*n):

    total=0

    for i in n:

        total=total+i

    return(total)

print(cal(2))

print(cal(2,3))

print(cal())

print(cal(2,4,7))

"""

2

5

0

13

"""

1. Non-keyword arguments (*args)/arbitrary arguments

*args are used to pass non-keyword arguments.

 

Ex: cal(2,3)…passed to function cal(*n)

 Keyword Arguments:

#Keyword argument

def ex(a,b):

    print(a)

    print(b)

 

ex(a=2,b=3)

 

Ex2:

#Keyword argument

def ex(b,a):

    print("a=",a)

    print("b=",b)

 

ex(a=2,b=3)

 

Name of the arguments is treated as the keywords and matched in the function call and fun. Defn

If the same match is found, the values of the arguments are copied in the function definition otherwise it raises error.

If we provide the different name of the variables passed to the function at the time function call, it leads to error.

Ex3:

#Keyword argument

def ex(e,f):

    print("f=",f) #Error

    print("e=",e)#Error

 

ex(a=2,b=3)

 

#Ex5: Mixed with keyword and required arguments

def example(a,b,c):

    print(a,b,c)

 

#function call

example("

ravi",b=4,c="sita")

 

2.  Keyword arguments (**kwargs)

 

#example program on keyword arguments

def ex1(**n):

    for key,value in n.items():

        print("{} is{}".format(key,value))

ex1(name="ravi",age=20,mobile=98787878)

"""

name is ravi

age is20

mobile is98787878

"""

 

#example program on keyword arguments

def ex1(**n):

    for key,value in n.items():

        print("{} is{}".format(key,value))

#ex1(name="ravi",age=20,mobile=98787878)

#ex1('ravi') #invalid due to non keyword argument

ex1(a=5)

"""

name isravi

age is20

mobile is98787878

"""

Python passes variable length non keword argument to the function using * args but we can not use this to pass keyword argument.

Use double * * before the parameter/arugment.  These arguments passed to a function as dictionary.

 

 

#using both keyword arguments(**) and non keyword arguments (*)

def ex(*args,**kwargs):

    for i in args:

        print(i)

    for item in kwargs.items():

        print(item)

ex(4,a="ravi",b="sita")

 

"""

4

('a', 'ravi')

('b', 'sita')

"""

   


No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...