Skip to main content

Python Modules

Introduction:
        Module is a file that is contain Pyhton definitions and statements. It defines functions, classes and variables and includes runnable code also. Functions are group of code and Modules are group of functions.

Steps:

  • Create a python file with one or more functions and save it with .py extension.
  • Include the import  statement.
    •  The import has the following syntax:
      • import [module1,module2,........,module N]
        • When the intrepeter comes across an import statement, it imports the module if the module is already present in the search path.
        • A search path is a list of directories that the interpreter searches before importing a module.
        • Module is loaded only once, even if multiple imports occur.
  • Using the module name the function is accessed using dot(.) operation.
The from....import Statement:

  • For a module involving hundreads of functions, from.....import Statement is recommended to save loading time
Syntax:
                         from python_file import function_name

  • Multiple functions can be imported by separating them using the commas.
Syntax:
                         from python_file import function_name 1, function_name 2
  • Use the astrisk (*) symbol to import eveything
Syntax:
                         from python_file import *


The Built-in Modules:
        There are plenty of built in modules, just as built in functions. The most useful are:
  1. Random
  2. Math
  3. Calender and date-time
1. Random:
       This module generates random numbers. If a random integer is needed, use the randint function. randint accepts two parameters: a lowest and a highest number. If a random floating point number is needed, use the random function. Choose a random element from a set such as list, called choices.

Try Yourself:
import random as rd
print rd.radint(0, 10)
print rd.random()
print rd.random()*10
data  =  [156, 89, "Vikram", 22.33, True]
print rd.choice(data)

2. Math:
         The math module provides access to mathematical conatsnts and functions.

3. Calender & Date-time:
         Python's time and calender modules help in tracking dates and times.
Try Yourself:
import calendar as cr
cal = cr.month(2017, 5)
print "May month calendar of the year 2017"
print cal

import time as tm
ticks = tm.time()
print "Number of ticks since 12:00am, January 1, 1970:", ticks
localtime = tm.localtime(tm.time())
print "Local current time:", localtime

#Formatted time
localtime = tm.asctime(tm.localtime(tm.time()))
print "Local current time: ", localtime

Comments

Popular posts from this blog

Python NumPy Introduction

Python is emerging as one of the favorite tools in the filed of data science. With powerful data science libraries like NumPy, SciPy, pandas, matplotlib, scikit-learn and tools like IPython (Jupyter) notebook combined with ease of programming, Python is proven as the powerful and preferred language of organizations. In this course I will tech you the basics of NumPy and further take a deep dig on playing with NumPy. NumPy  : NumPy is a python library, which supports efficient handling of various numerical operations on arrays holding numeric data.  They are known as N-dimensional-arrays or ndarrays.  Ndarrays are capable of holding data elements in multiple dimensions  and each data element of it is of fixed size and also all the elements of ndarray are of same data type. N-dimensional array  ( ndarray)  : N-dimensional array is an object, capable of holding data elements of same type and of fixed size in multiple dim...

5 Kind of Bugs Every Programmer Encounter During Coding

                As a programmer, you have to expect bugs. In simple terms, a bug can be defined as an error in a program. During the coding of a program, we often make some mistakes. These mistakes showcase themselves as bugs in your code. Writing a code is the easy part. The hard step is the debugging (Searching for the errors or bugs in a program). And it can get especially frustrating in situations where you create more bugs instead of fixing the current one. If you haven’t encountered the following bugs, you should expect them any time soon: Tiny Bugs:                 These types of bugs may be miniature, but dealing with them is no easy task. You will receive compiler errors, and then spend hours, or even days trying to figure out where you went wrong. Such bugs include forgetting that little semicolon or bracket. In a programming language like Python, you can face trouble when indentation i...

Python File

File handling is the most important part of any Web Applications. This has several functions for creating, reading, updating, deleting files etc., File Handling:           The key for working with files in Python is the open() function. Syntax: open(filename, mode) There are four different methods for opening a file: "r" - Read - Default value. Opens a file for reading, error if the file does not exist. "a" - Append - Opens a file for appending, creates the file if it does not exist. "w" - Write - Opens a file for writing, creates the file if it does not exist. "x" - Create - Creates the specified file, returns an error if the file exists. In addition you can specify if the file should be handled as binary or text mode: "t" - Text - Default value. Text mode. "b" - Binary - Binary mode(e.g., images) Note: Make sure the file exists, or else you will get an error. Syntax: f = ...