Getting Started with Python: Basics with examples, debugging, errors, exceptions, database handling , interacting with system & more by Hemant Sharma

Getting Started with Python: Basics with examples, debugging, errors, exceptions, database handling , interacting with system & more by Hemant Sharma

Author:Hemant Sharma [Sharma, Hemant]
Language: eng
Format: azw3, epub
Published: 2019-01-26T16:00:00+00:00


Multiple parameters

The parameters in function call are evaluated from left to right and associated with variables in that order.

Following example defines three parameters and calculated the volume of the object.

>>> length=2

>>> width=4

>>> height=5

>>> def getvol(length,width,height):

... print “volume is “, a*b*c

...

>>>

>>> getvol(length,width,height)

volume is 40

>>>

User input

If you working on a interactive program which need user input, it can be defines as in following example:

>>

>>> height = input("Enter Height ")

Enter Height 3

>>> width= input("Enter Width ")

Enter Width 4

>>> length= input("Enter Length ")

Enter Length 5

>>>

>>> getvol(length,width,height)

volume is 60

Using data from another function

As mentioned earlier, function data sharing is hierarchical to share data from one function to another, define the called function with parameter, it can be any name, doesn’t have to be same name as shared.

In the example below a topfunc() is define which sets the value of x, second function midfunc() is define inside which takes x as parameter and print its value.

>>> def topfunc():

... x = 10 ## set the value of x

... def midfunc(x): ## second function takes x as argument

... print “printing from mid function”, x

... midfunc(x) ## call to mid function to print value

...

>>> topfunc()

printing from mid function 10

>>>

A more convinent way do this is to use functions under the scope of main function.

Function Value Return

Function can return a value using return statement in a function. Python does not let any function without a return value, even if a function doesn’t return anything. You will see “None” as returned value.

In all the function examples above a value is returned when a function is called. This value can be used for display or further processing in the program.

>>> def test(a,b):

... c=a*b

... return 'DONE'

... print c

...

>>>

>>> test(a,b)

'DONE'

>>>

Only return value is printed as it returned before the print command, anything to print or return value must be stated before return statement.

Specialized built in Functions – filter, map and reduce

filter()

filter is a built in function which prints the values based on a condition. It takes two arguments

Function – which evaluate a condition and return true or false status.

Iterator , a range , list or tuple to go over



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.