Best post on user defined modules in python-3

User defined modules

Before we explain user defined modules let’s have glims on ‘what are modules?’.

Python has extensive libraries for supporting various functionalities like graphical user interface, database connectivity, mathematical calculations etc.

A file which consists of a collection of related functions is called as a module.

Python has many built-in modules like math, time, random etc.

Python also allows user to define its own modules according to the requirement such modules are called as user defined modules.

To use or call these modules in our script we use specific keywords like import and from.

In this post we will be studying math, time and random module in detail along with user defined modules.

What are user defined modules?

Python allows us to create and import our own module with desired functionalities.

To create a module of our choice we must first decide the functionalities required and choice the name of the module accordingly.

To create a module first step after deciding the module name is to create a file in python and save it with an extension of “.py”.

Keyword should not be use as a module name.

Whenever you create a module in it can be called in any other file using “import” keyword.

It is important run “” i.e. file or module created by us before calling it in another script.

If we call our user defined module in any file without executing the module or file it will generate and import error.

Advantages of user defined modules

  • With the help of user defined modules we can enable the use of new functionalities defined by user.
  • It helps in better code management and reusability.
  • Only required functionalities are included while creating user defined modules which saves line of code and improves performance.

The dir function

  • The dir() function is python built in function which is used to display the list of functions defined in a module.
  • It returns the list datatype consisting strings of function names separated by comma.
  • All attributes beginning with an underscore are default python attributes associated with a module.
  • dir() can return both built-in functions as well as user defined functions present in module.

Syntax of dir()


Example of user defined modules - 1

import math
def sphereArea(r):
    return 4*math.pi*r**2
def sphereVolume(r):
    return 4*math.pi*r**3/3
def sphereMetrics(r):
    return SphereArea(r).sphereVolume(r)
def circleArea(r):
    return math.pi*r**2
def squareArea(x):
    return x**2

import geometry
def pointyShapeVolume(x,h,square):
if square:
return h*base/3.0
print("Output of user defined function in python")



Example of user defined modules - 2

task = []
completed = []
def add(i):

def delete(i):
print("n"+i + " is deleted from the list")

def view():
    n = 1
    print("nTasks in list")
    for t in task:
        print(str(n)+" : "+t)
        n = n + 1
    n = 1
    if(completed != []):
        print("Tasks Completed")
        for t in completed:
            print(str(n)+" : "+t)
            n = n + 1
        print("No task completed yetn")

def done(i):
    print("n"+ i + " is donen")

import task as todolist


Tasks in list
1 : Milk
2 : Egg
3 : Bread
No task completed yet

Bread is done

Tasks in list
1 : Milk
2 : Egg
Tasks Completed
1 : Bread

Egg is deleted from the list

Tasks in list
1 : Milk
Tasks Completed
1 : Bread

Other python related post

Best post on programming language and its types – 1
Python Programming (Basics) – History & Features
Python Programming (Basics)- Debugging & Types of errors
Python Programming (Basics) – Variables, Values, Types & Keywords.
Best post on built-in functions in python – 1
Python Programming (Basics) – Type Conversion
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions
Best post on IDLE & script mode in Python – 1
Python Programming (Basics)- Operators in Python
Best post on Order of Operations – Python
Simple and compound statements of python in easy way
Best post on conditional statements in python – Part 1
Best post on conditional statements in python – Part 2
Best post on looping in python (for loop)-Part 1
Best post on looping in python (while loop)-Part 2
Best post on nested loop in python(looping) -Part 3
Best post on infinite loop in python(looping) -Part 4
Best post on control statements(break,continue,pass)-1
Best post on Function definition & calling in python -1
Easy flow of function execution,parameters,arguments-2
Easy fruitful & void function,return values,stack diagrams-3
Best post on types of function arguments in python- 4
Best post on recursive function, scope of variable-5
Best post on import and from statement in python – 1
Best post on modules and built-in modules math,random & time-2
Best post on user defined modules in python-3
Best post on string datatype in python – 1
Best post immutable string,string operations python-2
Best post on string methods in python – 3
Best post on list datatype in python – 1
Best post on mutable list, list operations python – 2
Best post on List methods in python – 3
Best post on dictionary datatype in python – 1
Best post on dictionary methods and operations-2
Best post on tuple datatype in python – 1
Best post on tuple operations and immutable tuple- 2
Best post on tuple methods and built-in functions-3
17 -Python program to demonstrate Button in tkinter and its event in easy way
New posts coming soon…….

Other advance python related post

File Systems and File Handling in Python
Types of file modes and attributes of file object in Python
How to read,write and append in a file in python?
Remaining file methods in Python
File Positions in Python
Directory in Python and its methods
Iterator and Iterables in Python
Exceptions in Python
Exception Handling in Python (Part I)
Exception Handling in Python (Part II)
Regular Expressions
Metacharacters or Regular Expression Patterns
Functions in ‘re’ module(Part I)- Match vs Search
Functions in ‘re’ module(Part II)-findall(), split(), sub()
Flags for regular expressions(Modifiers)
GUI programming in Python and Python GUI Library
What is Tkinter ?
Layout Manager (Geometry Manager) in Python
Events and Bindings in Python along with Widget configuration and styling
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
Canvas widget of tkinter module – Python
Widgets in tkinter module – Python
Label, Text, Entry & Message Widget in Python
Button, Checkbutton & Radiobutton Widget in Python
Best post on Menu and Menubutton Widget of tkinter
Best post- Listbox and Scrollbar(Slider) Widget-Python
Best post on Frame Widget of tkinter in Python
Best post: message box widget and its methods
Best post- LabelFrame, Toplevel, PanedWindow widgets
Best post on Spinbox and Scale Widget in Python
Best post : Database connectivity in Python (Part 1)
Best post : Database Connectivity in Python (Part 2)
Best post on Create and Insert query : Python + MySQL
Best post on Select Query to Search & Read: Python + MySQL-4
Best post on Update query and Delete query : Python + MySQL-4
New posts coming soon…….
We are aiming to explain all concepts of Python in easiest terms as possible.

Leave a Comment