MODULES



Python - Modules


What is a Module?


Consider a module to be the same as a code library.

A file containing a set of functions you want to include in your application.


Create a Module :


To create a module just save the code you want in a file with the file extension .py:


Save this code in a file named FTpython.py

def greeting(name):
  print("Hello, " + name)

Use a Module :


Now we can use the module we just created, by using the import statement:


Import the module named FTpython, and call the greeting function:

import FTpython

FTpython.greeting("John")


=======o/p=======

Hello john

Note: When using a function from a module, use the syntax: module_name.function_name.


Variables in Module :


The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc):


Save this code in the file FTpython.py

person1 = {
  "name": "freedom",
  "age": 2,
  "year": "2018"
}

Import the module named FTpython, and access the person1 dictionary:

import FTpython

a = FTpython.person1["name"]
print(a)


========o/p=========

freedom

Naming a Module :

We can name the module file whatever you like, but it must have the file extension ".py"


Re-naming a Module :


We can create an alias when you import a module, by using the as keyword:


Create an alias for FTpython called mx:

import FTpython as mx

a = mx.person1["age"]
print(a)

=======o/p=======

2


Built-in Modules :



There are several built-in modules in Python, which we can import whenever we like.


Import and use the platform module:

import platform

x = platform.system()
print(x)

========o/p========

windows

Using the dir() Function :


There is a built-in function to list all the function names (or variable names) in a module. The dir() function:


List all the defined names belonging to the platform module:

import platform

x = dir(platform)
print(x)

=======o/p======

['DEV_NULL', '_UNIXCONFDIR', '_WIN32_CLIENT_RELEASES', '_WIN32_SERVER_RELEASES', '__builtins__', '__cached__', '__copyright__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_default_architecture', '_dist_try_harder', '_follow_symlinks', '_ironpython26_sys_version_parser', '_ironpython_sys_version_parser', '_java_getprop', '_libc_search', '_linux_distribution', '_lsb_release_version', '_mac_ver_xml', '_node', '_norm_version', '_parse_release_file', '_platform', '_platform_cache', '_pypy_sys_version_parser', '_release_filename', '_release_version', '_supported_dists', '_sys_version', '_sys_version_cache', '_sys_version_parser', '_syscmd_file', '_syscmd_uname', '_syscmd_ver', '_uname_cache', '_ver_output', 'architecture', 'collections', 'dist', 'java_ver', 'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node', 'os', 'platform', 'popen', 'processor', 'python_branch', 'python_build', 'python_compiler', 'python_implementation', 'python_revision', 'python_version', 'python_version_tuple', 're', 'release', 'subprocess', 'sys', 'system', 'system_alias', 'uname', 'uname_result', 'version', 'warnings', 'win32_ver']

Note: The dir() function can be used on all modules, also the ones you create yourself.


Import From Module :


You can choose to import only parts from a module, by using the from keyword.


The module named FTpython has one function and one dictionary:


def greeting(name):
    print("hello" + name)

person1={
    "name":"freedom",
    "age":2,
    "year":2018
    }


Import only the person1 dictionary from the module:

from FTpython import person1
print (person1["name"])


=======o/p=======

freedom

Note: When importing using the from keyword, do not use the module name when referring to elements in the module. Example: person1["age"], not mymodule.person1["age"]



RegEx SETS

Python - RegEx SETS

posted on 2019-11-12 23:06:24 - Python Tutorials


RegEx_Functions

Python - RegEx_Functions

posted on 2019-11-09 06:07:29 - Python Tutorials


RegEx_Sets

Python - RegEx_Sets

posted on 2019-11-09 05:30:54 - Python Tutorials


Prompt Examples

ChatGPT Prompt Examples

posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


Use Cases

Chat GPT Key Use Cases

posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


Prompt Frameworks

Prompt Frameworks

posted on 2023-06-21 19:33:06 - ChatGPT Tutorials