Roshan Book

My Tech Notebook

Python tutorial 11- Functions


use pass statement if you want nothing inside function

e.g

def testfunc():

    pass

You can pass multiple parameters inside python

Once you must pass all parameters when defined

You can also define a default value, for in case all arguments are getting passed

You can also assign value none for parameters, if you don’t want to give default value for parameter.

#To get arbitrary/optional number of arguments

def main():
    testfunc()

def testfunc(arg1,arg2, *args):
    print(‘This is a test function’)

if __name__ == “__main__”: main()

 

 

args gives other values as tuple

It is immutable. No change, no addition

# passing a named parameters

def main():

testfunction(one=1,two=2,three=42)

def testfunc(**kwargs):

print(‘This is a test function’,kwargs[‘one’])

Kwargs are dictionary and arguments are passing from the caller as dictionary

Sequence of occurrence in a function – Named argument, Arbitary tuple argument and then Keyword arguments

i.e. arg1, arg2, *args, **kwargs

# return values from function

return- loop breaks

yield – that loop breaks and continue from next loop

# Sequence with generator function

Function that generates iterators in function

Range(start,stop,step)

stop, when only one value

start default value 0

step default value 1

 

def inclusive_range(*args):

numargs=len(args)

if numargs<1:raise typeError(‘requires at least one argument’)

elif numargs==1

start=0

stop=args[0]

step=1

 

elif numargs==2start=0

stop=args[0]

(start,stop)=args

step=1

elif numargs==3

(start,stop,step)=argsstop=args[0]

else: raise Typeerror(“atmax 3 arguments in range function”)

i=start

while i<=stop:

yield i

i+=step

3 responses to “Python tutorial 11- Functions

  1. Discount Replica Handbags December 5, 2011 at 5:09 am

    I always was interested in this topic and stock still am, regards for posting.

  2. replica louis vuitton December 5, 2011 at 5:28 pm

    I do not grant all, but I do congratulate the writer of the blog for the theme.

  3. moncler outlet December 16, 2011 at 6:24 am

    Worth it to read entry, I wait for your next! Thx for share

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: