Python - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Python Modules

Python Modules

shape Description

Modules are utilized to classify code in Python into littler part. A module is essentially a record, where variables, functions and classes are characterized. Gathering comparative code into a solitary document makes it simple to get to. The following are the advantages of the modules. The following are the different ways to import the module.

By using import statement

shape Description

The functionality of import statement is to import the module. The following is an example. [c] def add(a,b): c=a+b print c return [/c] Save the above example with the extension .py like add.py . Now use the import statement to import the saved file as follows. [c]import add addition.add(100,200) addition.add(300,400) [/c] Now compile the code result will be as follows. [c]>>> 300 700 >>> [/c] The following is an example to access multiple modules at a time. message.py [c]def msg_method(): print "Welcome" return [/c] display.py [c]def display_method(): print "Welcome to splessons" return [/c] original.py [c]import message,display msg.msg_method() display.display_method() [/c] Now compile the code result will be as follows. [c]>>> Welcome Welcome to splessons >>> [/c]

From import statement

shape Description

The functionality of from import is to get attribute from the module. The following is the syntax. [c]from <module_name> import <attribute1,attribute2,attribute3,...attributen> </attribute1,attribute2,attribute3,...attributen></module_name> [/c] The following is an better example to understand the concept. area.py [c]def circle(r): print 3.14*r*r return def square(l): print l*l return def rectangle(l,b): print l*b return def triangle(b,h): print 0.5*b*h return [/c] area1.py [c]from area import square,rectangle square(10) rectangle(2,5) [/c] Now compile the code result will be as follows. [c]>>> 100 10 >>> [/c]

import whole module

shape Description

By using from? import * one can easily import whole module. The following is the syntax. [c]from <module_name> import * </module_name> [/c] The following is an example. area.py [c]def circle(r): print 3.14*r*r return def square(l): print l*l return def rectangle(l,b): print l*b return def triangle(b,h): print 0.5*b*h return [/c] area1.py [c]from area import * square(10) rectangle(2,5) circle(5) triangle(10,20) [/c] Now compile the code result will be as follows. [c]>>> 100 10 78.5 100.0 >>> [/c]
ceil(n)

Summary

shape Key Points

  • The tangent of the radian will be given by the function tan(n).
  • The past integer number will be given by the function code>floor(n)
  • The next integer number will be given by the function code>ceil(n)