• Home
  • HTML
    • HTML Introduction
    • HTML Basic
    • HTML Elements
    • HTML Attributes
    • HTML Headings
    • HTML Paragraphs
    • HTML Text Formatting
    • HTML Styles
    • HTML Comments
    • HTML Styles - CSS
    • HTML Links
    • HTML Images
    • HTML Tables
    • HTML Lists
    • HTML Block and Inline Elements
    • HTML class Attribute
    • HTML Forms
    • HTML Media
  • CSS
    • Introduction of CSS
    • CSS Syntax
    • CSS Selectors
    • How To Add CSS
    • CSS Comments
    • CSS Colors
    • CSS Backgrounds
    • CSS Borders
    • CSS Margins
    • CSS Text
    • CSS Lists
    • CSS Tables
    • CSS Box Model
    • CSS Dimension
    • CSS Padding
    • CSS Border
    • CSS Margin
    • CSS Outline
    • CSS Cursors
    • CSS Overflow
    • CSS Units
    • CSS Visual Formatting
    • CSS Display
    • CSS Visibility
    • CSS Position
    • CSS Layers
    • CSS Float
    • CSS Alignment
    • CSS Pseudo-classes
    • CSS Pseudo-elements
    • CSS Media Types
    • CSS Sprites
    • CSS Opacity
    • CSS Attribute Selectors
    • CSS Validation
    • CSS3 Border
    • CSS3 Gradients
    • CSS3 Text Overflow
  • JavaScript
    • JS Introduction
    • JS Getting Started
    • JS Syntax
    • JS Variables
    • JS Generating Output
    • JS Data Types
    • JS Operators
    • JS Events
    • JS Strings
    • JS Numbers
    • JS If Else
    • JS Switch Case
    • JS Arrays
    • JS Sorting Arrays
    • JS Loops
    • JS Functions
    • JS Objects
    • JS DOM Nodes
    • JS DOM Selectors
    • JS DOM Styling
    • JS DOM Get Set Attributes
    • JS DOM Manipulation
    • JS DOM Navigation
    • JS Window
    • JS Screen
    • JS Location
    • JS History
    • JS Navigator
    • JS Dialog Boxes
    • JS Timers
    • JS Date and Time
    • JS Math Operations
    • JS Type Conversions
    • JS Event Listeners
    • JS Event Propagation
    • JS Borrowing Methods
    • JS Hoisting Behavior
    • JS Closures
    • JS Strict Mode
    • JS JSON Parsing
    • JS Error Handling
    • JS Regular Expressions
    • JS Form Validation
    • JS Cookies
    • JS AJAX Requests
    • JS ES6 Features
  • jQuery
    • jQuery Introduction
    • jQuery Syntax
    • jQuery Selectors
    • jQuery Events
    • jQuery Show/Hide
    • jQuery Fade
    • jQuery Slide
    • jQuery Animation
    • jQuery Stop
    • jQuery Chaining
    • jQuery Callback
    • jQuery Get/Set
    • jQuery Insert
    • jQuery Remove
    • jQuery CSS Classes
    • jQuery Style Properties
    • jQuery Dimensions
    • jQuery Traversing
    • jQuery Ancestors
    • jQuery Descendants
    • jQuery Siblings
    • jQuery Filtering
    • jQuery Ajax
    • jQuery Load
    • jQuery Get/Post
    • jQuery No-Conflict
  • PHP
    • PHP Introduction
    • PHP Install
    • PHP Syntax
    • PHP Comments
    • PHP Variables
    • PHP Echo / Print
    • PHP Data Types
    • PHP Strings
    • PHP Constants
    • PHP Operators
    • PHP If...Else...Elseif
    • PHP Switch
    • PHP Loops
    • PHP Functions
    • PHP Arrays
    • PHP Superglobals
    • PHP Date and Time
    • PHP Include
    • PHP File Handling
    • PHP File Upload
    • PHP Cookies
    • PHP Sessions
    • PHP Filters
    • PHP Callback Functions
    • PHP JSON
    • PHP Exceptions
    • PHP What is OOP
    • PHP Classes/Objects
    • PHP Constructor
    • PHP Destructor
    • PHP Access Modifiers
    • PHP Inheritance
    • PHP Abstract Classes
    • PHP Interfaces
    • PHP Traits
    • PHP Static Methods
    • PHP Namespaces
  • SQL
    • Introduction to SQL
    • SQL Create Command
    • SQL ALTER Command
    • SQL Truncate Drop Rename
    • INSERT SQL command
    • UPDATE SQL command
    • DELETE SQL command
    • SQL COMMIT command
    • SQL ROLLBACK command
    • SQL GRANT and REVOKE Command
    • SQL WHERE clause
    • SQL LIKE clause
    • SQL ORDER BY Clause
    • SQL Group By Clause
    • SQL HAVING Clause
    • SQL DISTINCT keyword
    • SQL AND OR operator
    • SQL Constraints
    • SQL Functions
    • SQL JOIN
  • Python
    • Getting started with Python
    • Introduction to IDLE
    • Python 2.x vs. Python 3.x
    • Syntax Rules and First Program
    • Numbers and Math Functions
    • Python Operators
    • Python Variables
    • Python Modules and Functions
    • Python Input and Output
    • Data Types in Python
    • String in Python
    • String Functions in python
    • Lists in Python
    • Utilizing List Elements by Iterating
    • Deleting List Elements & other Functions
    • Dictionaries in Python
    • Functions for Dictionary
    • Tuples in Python
    • Relational and Logical Operators
    • Conditional Statements in Python
    • Looping in Python
    • Define Functions in Python
    • Python-Introduction to OOP
    • Object Oriented Programming in Python
    • Classes in Python
    • The concept of Constructor
    • Destructors - Destroying the Object in Python
    • Inheritance in Python
    • Access Modifers in Python
    • Types of Inheritance
    • Method Overriding in Python
    • Polymorphism
    • static Keyword
    • Operator Overloading Python
    • Introduction to Error Handling
    • Exception Handling: try and except
    • Exeption Handling: finally
    • Exception Handling: raise
    • File Handling
    • Reading and Writing File
    • Introduction to Multithreading
    • Threading Module in Python
    • Thread Object
    • Lock Object
    • RLock Object
    • Event Object
    • Timer Object
    • Condition Object
    • Barrier Object
    • __name__ Variable in Python
    • Iterable and Iterator
    • yield Keyword
    • Python Generators
    • Python Closures
    • Python Decorators
    • @property Decorator in Python
    • Assert Statement
    • Garbage Collection
    • Shallow and Deep Copy
    • Introduction to Logging
    • Configure Log LEVEL, Format etc
    • Python Logging in a file
    • Python Logging Variable Data
    • Python Logging Classes and Functions
    • Python MySQL Introduction
    • Create Database - Python MySQL
    • Create Table - Python MySQL
    • Insert Data in Table
    • Select Data from Table
    • Update data in Table
    • Delete data from Table
    • Drop Table from Database
    • WHERE clause - Python MySQL
    • Order By clause - Python MySQL
    • Limit clause - Python MySQL
    • Table Joins - Python MySQL
  • MongoDB
    • MongoDB Introduction
    • Overview of MongoDB
    • MongoDB vs SQL Databases
    • Advantages of MongoDB
    • When to go for MongoDB
    • Data Modelling in MongoDB
    • Is MongoDB really Schemaless?
    • Installing MongoDB on Windows and Linux
    • Datatypes in MongoDB
    • Create and Drop Database in MongoDB
    • MongoDB: Creating a Collection
    • CRUD Operations in MongoDB
    • Data Relationships in MongoDB
    • Indexing in MongoDB
    • Sorting in MongoDB
    • Aggregation in MongoDB
    • Data Backup and Restoration in MongoDB
    • Sharding in MongoDB
    • Java Integration with MongoDB
  • Elixir
    • Elixir Overview
    • Elixir Environment
    • Elixir Basic Syntax
    • Elixir Data Types
    • Elixir Variables
    • Elixir Operators
    • Elixir Pattern Matching
    • Elixir Decision Making
    • Elixir Strings
    • Elixir Char Lists
    • Elixir Lists and Tuples
    • Elixir Keyword Lists
    • Elixir Maps
    • Elixir Modules
    • Elixir Aliases
    • Elixir Functions
    • Elixir Recursion
    • Elixir Loops
    • Elixir Enumerables
    • Elixir Streams
    • Elixir Structs
    • Elixir Protocols
    • Elixir File I/O
    • Elixir Processes
    • Elixir Sigils
    • Elixir Comprehensions
    • Elixir Typespecs
    • Elixir Behaviours
    • Elixir Errors Handling
    • Elixir Macros
    • Elixir Libraries
  • TypeScript
    • TypeScript Overview
    • Install TypeScript
    • First TypeScript Program
    • Type Annotation
    • TypeScript Variable
    • TypeScript Data Type Number
    • TypeScript Data Type String
    • TypeScript Data Type Boolean
    • TypeScript Arrays
    • TypeScript Tuples
    • TypeScript Enum
    • TypeScript Union
    • TypeScript Any Data Type
    • TypeScript Void Data Type
    • TypeScript Never Data Type
  • Home
  • Getting started with Python
  • Introduction to IDLE
  • Python 2.x vs. Python 3.x
  • Syntax Rules and First Program
  • Numbers and Math Functions
  • Python Operators
  • Python Variables
  • Python Modules and Functions
  • Python Input and Output
  • Data Types in Python
  • String in Python
  • String Functions in python
  • Lists in Python
  • Utilizing List Elements by Iterating
  • Deleting List Elements & other Functions
  • Dictionaries in Python
  • Functions for Dictionary
  • Tuples in Python
  • Relational and Logical Operators
  • Conditional Statements in Python
  • Looping in Python
  • Define Functions in Python
  • Python-Introduction to OOP
  • Object Oriented Programming in Python
  • Classes in Python
  • The concept of Constructor
  • Destructors - Destroying the Object in Python
  • Inheritance in Python
  • Access Modifers in Python
  • Types of Inheritance
  • Method Overriding in Python
  • Polymorphism
  • static Keyword
  • Operator Overloading Python
  • Introduction to Error Handling
  • Exception Handling: try and except
  • Exeption Handling: finally
  • Exception Handling: raise
  • File Handling
  • Reading and Writing File
  • Introduction to Multithreading
  • Threading Module in Python
  • Thread Object
  • Lock Object
  • RLock Object
  • Event Object
  • Timer Object
  • Condition Object
  • Barrier Object
  • __name__ Variable in Python
  • Iterable and Iterator
  • yield Keyword
  • Python Generators
  • Python Closures
  • Python Decorators
  • @property Decorator in Python
  • Assert Statement
  • Garbage Collection
  • Shallow and Deep Copy
  • Introduction to Logging
  • Configure Log LEVEL, Format etc
  • Python Logging in a file
  • Python Logging Variable Data
  • Python Logging Classes and Functions
  • Python MySQL Introduction
  • Create Database - Python MySQL
  • Create Table - Python MySQL
  • Insert Data in Table
  • Select Data from Table
  • Update data in Table
  • Delete data from Table
  • Drop Table from Database
  • WHERE clause - Python MySQL
  • Order By clause - Python MySQL
  • Limit clause - Python MySQL
  • Table Joins - Python MySQL
Home >> python >> Python Modules and Functions

Python Modules and Functions

Welcome to another tutorial, here you learn how to use modules and other related functions in Python. In the previous tutorial, you learned about modules and other mathematical functions. Aside from the module, you learned previously, there are other mathematical modules available in the python library that can be used directly inside any python program. Few of the modules are very important to programmers who want to make real-life applications.

 

Functions lie inside the Module

Just as already known, you can consider a module as a piece of python code saved within the python package and can be reused from the module files or other python code files.

Before we proceed, try and remember what we did after importing the math module; as we tested some of the functions defined inside the math module, such as the math.floor(8.4), math.sin(3.14159) and so on. Examples of calling a function include math.floor(8.4), pow(5,2) etc.

Check out the example below to see how to use the import statement to import external modules such as the math module.

# Using Math library in Python
import math

x = 10

# log() function is in math module
y = math.log(x)

print (y)

# abs function
z = -7
print (abs(z))

# pow function
print (pow(x,2))

 

From the above example, the code we have written above is just a way to call and use these functions. The logic or code that performs this complex mathematical operation resides inside the module file and is called function definition. 

Function definition sets the rules of how a function will interpret the input provided. Also, it is the function definition itself that decides the type and number of input(s) and output for the functions. A parameter is an official term for these inputs.

Parameter: This is the structure that defines the type of the input accepted as the function is been defined.

Argument: This is the actual value that is inserted as an input while calling or using the function.

Take for example, if we create a module file (INSERT NAME.py) (.py means that is a python code file). So inside the module file, a function has been defined which can solve the quadratic equation ax2 + bx +c =0. Let name the function qSolve(a,b,c)

Now, what you need to do next is to use this function, by providing the values of a, b, and c, and the function will print whether the two solutions are real or not and what they are. Below is how you can import the module and use the function.

Step 1: Import the module

>>> import PythonTutorial

Step 2: call the function with appropriate arguments.

>>> qSolver(2, -6, 3)

Note that this function does not need to be tried on the IDLE, as it will not work, because you have not created any module name (INSERT NAME.py here above example PythonTutorial.py) on your computer. However, this example was just to explain to you how modules and functions work and how you can create custom modules with functions defined by you. The steps highlighted above must be followed to call every function from the modules.

 

How to use a function defined inside a module

Below are the things you should know before making use of a function defined inside a module:

  • The module contained inside a function must be known
  • Confirm if the module file is on your computer; if it is there then you can use it, but if it is not, then you have to download it. Take note that you can check the module by importing it in the IDLE, and an error means it’s not there.
  • Identify the name of the function to be used. You can check out python documentation for the names.
  • Also, check python documentation to know the number of arguments it requires, what they mean, and their order.
  • Finally, call the function; <module-name> is module's name, <function-name> is function's name; and <agrument-1>, <argument-2>... represent arguments of functions.
>>> <module-name>.<function-name>(<argument-1>, <argument-2>, ...)

 

Note: The number of arguments depends on the function used. Also, some functions, might not accept any argument, and so the number of arguments depends on the function that is been called, as such you need to check the documentation to see the function definition before you use them. 

Check out this example:

>>> variable-name = <module-name>.<function-name>(<argument-1>, <argument-2>, ...)

From the example, you can see that it only makes sense when the function returns any value (output). In addition, functions can return only one output value or none at all.

From what we have learned to suffer, let's make use of function lower() which is defined in the string module.

Before we continue, let’s understand what a String is.

 

What is a String?

A string is a term used to refer to a word such as ‘(INSERT YOUR WORD)', or a sentence like ‘(this website is a good one)’. Strings must be enclosed in single or double quotes ('…'/ "…"). Remember, that in one of our previous tutorials we explained how strings can be stored in a variable. Therefore, a variable that stores a string inside them is known as a String variable.

Furthermore, the String module is not the same as the String explained above, rather, the string module is a module with a name string with functions defined to execute various operations on string variables.

However, the string module is made up of functions that deal with string operations. Check out the example below, as will use lower(), which converts every character of a string to lowercase.

In summary:

  • You should know now that string is the library/module which consists of the function lower().
  • You must import the string module to check if it's available. However, it is a basic module, so it is very much likely to be available.
  • Also, you should check the documentation and find the function lower() on the module's page, as it will give you the required information to use it.
  • The lower() function has just one argument which is a string. 
  • Finally, call the function,
>>> string_variable = "Let's StartLearning Python"
>>> string.lower(string_variable)

but, if you want to go direct so as not to waste memory on saving a variable, you can do it as shown below:

>>> string.lower("Let's StartLearning Python")

Now, calling a function just as we did above, will print only the result when we hit enter. This is shown below:

>>> import string
>>>string.lower("Let's StartLearning Python")
tet's startlearning python

 

From the example above, it clears as the returned value is having all uppercase letters (i.e. Capital letters) converted to lowercase, that is L to l, S to s, L to l,  and P to p. Know that, if you used a variable, using a function does not modify the value of the original variable. Just as seen in the above example. You can save the modified value in some other variable, or even in the same variable itself, just like this:

>>> string_variable = string.lower(string_variable)

In the above example, the statement is perfectly valid, because python will compile everything from right to left, that is to say, it will use string.lower() to get the modified string and then later assign the output value to the string variable by using the equal to '=' operator.

 

 

  • Prev
  • Next


-Advertisement-


DeveloperTutorial
[email protected] © 2022-2023 Developers Tutorial All rights reserved.

Follow Us

Facebook Twitter LinkedIn Printerest Reddit

Announcement

Its a big achivement for us, We make a partnership with TutorialWithExample.com for the better content of our users.

Still Need Help ?

Let us now about your issue and a Professional will reach you out.