Thursday, March 1, 2012

Functions and Modules


What are functions?

Functions are built in programs of python, which helps us to do complex things without having to type every single instructions.

Let me try to illustrate with an example(Everything in this post is done in IDLE)

Generally, to calculate a power in python, we type

>>> 5 ** 4
625
>>>

Lets now try this with a function, which makes it more easier..

To use a Function, first we need to type the function name and then parameter that the function would need.

Here is an example of how we calculate power:

Syntax for power: pow(x,y)

>>> pow(5,4)
625
>>>

Here, 5 and 4 were the parameters that the function pow needed to calculate the power.

One more example to calculate absolute value

I am not going to teach maths here, just in a nutshell, absolute values, pretty much takes any number and neutralize it to make a positive number.

Syntax of abs: abs(x), where x can be positive or negative


>>> abs(-18)
18
>>>
>>> abs(5)
5
>>>

Here abs was the function and -18 and 5 were the parameters.


What are modules?

Modules are a simple way to structure a program, modules contains list of functions. To use a function from module, we need to import the module.

If we try to use a function of a module without importing the module it returns an error.

Lets check it out:

>>> floor (18.7)
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    floor(18.7)
NameError: name 'floor' is not defined

[floor is a function, that takes a fractional number and returns the integer, the number that's on the left hand side of the "."]

It returned an error because, though floor is a built-in function, but python doesn't know where to look for it. So to use it we have to import the module, where the function floor is located.

function floor is located in math module, so lets see how to import a module.


Syntax: import modulename

>>> import math
>>> floor (18.7)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    floor (18.7)
NameError: name 'floor' is not defined
>>>

Oops, we have imported the module, so why cant we use the floor function now, don't worry, i did it on purpose.

Here is the explanation:

We have imported the math module, but to use the floor function of math module, we need to instruct the python that floor is located in math module.

We might use several module in a program, so python will not know which of the imported module to look for.

So how we instruct python after importing?

Syntax: module-name.function-name(parameter)
Note: The module has to be imported before we use it

Example:

>>> import math
>>> math.floor(18.7)
18.0
>>>

One more Example, SquareRoot is also a function of math module, so we can check this out too

>>> math.sqrt(81)
9.0
>>>


Modules can be assigned to a variable, if you are not sure what variable is please check my last post.

Example:

>>> import math
>>> v = math.sqrt

Now instead of typing math.sqrt every time in a particular program, where math.sqrt is assigned to variable v

We can simply use now the variable with parameters and done to get the result.

Check this out:

>>> v(9)
3.0
>>>
So now in a certain program, after assigning a function to a variable, we can simply use the variable instead of typing the whole function.

So this was a quick lesson about how to use a function and import modules and how to use them.

Hope this was informative for you, practice this, and for any query, use the comment box below.

And don't forget to check my next post.

Thank You!

7 comments:

  1. Good Work.......Awesome..........Like It

    ReplyDelete
    Replies
    1. Thanks..Practise them otherwise it wil be a problem when in sart programming...

      Delete
  2. Really Its A Good Job..
    I started Learning Python Now Days from here..its a awesome blog for it.

    I have a Query:
    pow(x,y) is also a function which relates with "Math" Module...but why it don't need to be define while floor(x) needs???
    please reply...

    Thank You..!

    ReplyDelete
    Replies
    1. Hello, Vikas

      Fire up the Python IDLE

      >>> import math
      >>> floor(5.3)

      Traceback (most recent call last):
      File "", line 1, in
      floor(5.3)
      NameError: name 'floor' is not defined
      >>> math.floor(5.6)
      5.0
      >>> math.pow(2,3)
      8.0
      >>> pow(2,3) #see here the color of word 'pow' is violet
      8
      >>>

      What can be concluded? :)

      pow is a one of the basic function that gets loaded when python starts, but when you import the math module, you extend the scope of using mathematical calculation.

      Another example would be 'divmod' which returns quotient and remainder, its a mathematical calculation, but it gets loaded as default.
      Ex:
      >>> divmod(4,2)
      (2, 0)
      >>>
      Ex: when 4 is divided by 2, Quotient is 2, remainder is 0.

      Delete
    2. okay
      I understand now..
      Thank You.

      and
      What are the another modules in Python except "math".

      Delete
    3. Take A Look Here :)

      http://docs.python.org/library/functions.html

      Delete
  3. i just started learning from here, and its goin really nice :D

    ReplyDelete