0% found this document useful (0 votes)
54 views9 pages

Python Modules: What Is A Module?

Uploaded by

Krishnaprasad k
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views9 pages

Python Modules: What Is A Module?

Uploaded by

Krishnaprasad k
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Introduction to Python Modules
  • Creating and Using Modules
  • Built-in Modules
  • Naming and Re-naming Modules
  • Importing Specific Parts of a Module
  • Exercises
  • Additional Resources

5/18/22, 2:29 PM Python Modules

 Menu  Log in
Dark mode

Dark code
  HTML CSS   

Python Modules
❮ Previous Next ❯

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 :

Example
Save this code in a file named [Link]

def greeting(name):

  print("Hello, " + name)

Use a Module
[Link] 1/9
5/18/22, 2:29 PM Python Modules

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

Example
Import the module named mymodule, and call the greeting function:

import mymodule

[Link]("Jonathan")

Run Example »

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):

Example
Save this code in the file [Link]

person1 = {

  "name": "John",

  "age": 36,

  "country": "Norway"

Example
[Link] 2/9
5/18/22, 2:29 PM Python Modules

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

import mymodule

a = mymodule.person1["age"]

print(a)

Run Example »

Naming a Module
You can name the module file whatever you like, but it must have the file extension .py

Re-naming a Module
You can create an alias when you import a module, by using the as keyword:

Example
Create an alias for mymodule called mx :

import mymodule as mx

a = mx.person1["age"]

print(a)

Run Example »

Built-in Modules
There are several built-in modules in Python, which you can import whenever you like.

[Link] 3/9
5/18/22, 2:29 PM Python Modules

Example
Import and use the platform module:

import platform

x = [Link]()

print(x)

Try it Yourself »

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:

Example
List all the defined names belonging to the platform module:

import platform

x = dir(platform)

print(x)

Try it Yourself »

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

Import From Module


[Link] 4/9
5/18/22, 2:29 PM Python Modules

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

Example
The module named mymodule has one function and one dictionary:

def greeting(name):

  print("Hello, " + name)

person1 = {

  "name": "John",

  "age": 36,

  "country": "Norway"

Example
Import only the person1 dictionary from the module:

from mymodule import person1

print (person1["age"])

Run Example »

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"]

Test Yourself With Exercises


[Link] 5/9
5/18/22, 2:29 PM Python Modules

Exercise:
What is the correct syntax to import a module named "mymodule"?

mymodule

Submit Answer »

Start the Exercise

❮ Previous Next ❯

NEW

We just launched

W3Schools videos

Explore now

[Link] 6/9
5/18/22, 2:29 PM Python Modules

COLOR PICKER




Get certified

by completing

a course today!

school
w3 s

2
CE

02

TI 2
R

FI .
ED

Get started

CODE GAME

Play Game
[Link] 7/9
5/18/22, 2:29 PM Python Modules

Report Error

Forum

About

Shop

Top Tutorials
HTML Tutorial

CSS Tutorial

JavaScript Tutorial

How To Tutorial

SQL Tutorial

Python Tutorial

[Link] Tutorial

Bootstrap Tutorial

PHP Tutorial

Java Tutorial

C++ Tutorial

jQuery Tutorial

Top References
HTML Reference

CSS Reference

JavaScript Reference

SQL Reference

Python Reference

[Link] Reference

Bootstrap Reference

PHP Reference

HTML Colors

Java Reference

Angular Reference

jQuery Reference

Top Examples
HTML Examples

CSS Examples

JavaScript Examples

[Link] 8/9
5/18/22, 2:29 PM Python Modules

How To Examples

SQL Examples

Python Examples

[Link] Examples

Bootstrap Examples

PHP Examples

Java Examples

XML Examples

jQuery Examples

Web Courses
HTML Course

CSS Course

JavaScript Course

Front End Course

SQL Course

Python Course

PHP Course

jQuery Course

Java Course

C++ Course
C# Course

XML Course

Get Certified »

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant
full correctness of all content.
While using W3Schools, you agree to have read and accepted our terms of use,
cookie and privacy policy.

Copyright 1999-2022 by Refsnes Data. All Rights Reserved.

W3Schools is Powered by [Link].

[Link] 9/9

5/18/22, 2:29 PM
Python Modules
https://www.w3schools.com/python/python_modules.asp
1/9
❮ Previous (https://www.w3schools.com
5/18/22, 2:29 PM
Python Modules
https://www.w3schools.com/python/python_modules.asp
2/9
Now we can use the module we just cre
5/18/22, 2:29 PM
Python Modules
https://www.w3schools.com/python/python_modules.asp
3/9
Import the module named mymodule, and
5/18/22, 2:29 PM
Python Modules
https://www.w3schools.com/python/python_modules.asp
4/9
Example
Import and use the platform
5/18/22, 2:29 PM
Python Modules
https://www.w3schools.com/python/python_modules.asp
5/9
You can choose to import only parts f
(https://www.w3schools.com/videos/index.php)5/18/22, 2:29 PM
Python Modules
https://www.w3schools.com/python/python_modules.
(https://www.w3schools.com/codegame/index.html) (https://www.w3schools.com/colors/colors_picker.asp) (https://www.w3schools.
(https://www.w3schools.com/codegame/index.html)5/18/22, 2:29 PM
Python Modules
https://www.w3schools.com/python/python_modul
5/18/22, 2:29 PM
Python Modules
https://www.w3schools.com/python/python_modules.asp
9/9
How To Examples
 (https://www.w3schoo

You might also like