Welcome to codeswithpankaj.com! In this tutorial, we will explore the concept of modules in Python. We'll cover what modules are, how to create and use them, and provide detailed examples to illustrate their application.
- Introduction to Modules
- Why Use Modules?
- Creating a Module
- Importing a Module
- Using
importvs.from...import - Built-in Modules
- The
dir()Function - The
__name__Attribute - Practical Examples
- Summary
Modules are files containing Python code that can be imported and used in other Python programs. They help in organizing code into manageable sections and promote reusability.
- A module can contain functions, classes, and variables.
- Modules help in dividing a large program into smaller, manageable, and organized files.
- Code Organization: Modules help in organizing code into logical sections.
- Code Reusability: Functions and classes defined in one module can be reused in other programs.
- Maintainability: Modules make it easier to maintain and update code.
- Namespace Management: Modules provide a separate namespace, preventing naming conflicts.
Create a file named mymodule.py with the following content:
# mymodule.py
def greet(name):
return f"Hello, {name}!"
def add(a, b):
return a + bYou can import a module using the import statement.
# main.py
import mymodule
print(mymodule.greet("Pankaj"))
print(mymodule.add(10, 20))import mymodule
print(mymodule.greet("Pankaj"))
print(mymodule.add(10, 20))from mymodule import greet, add
print(greet("Pankaj"))
print(add(10, 20))from mymodule import *
print(greet("Pankaj"))
print(add(10, 20))Python comes with many built-in modules that you can use in your programs.
import math
print(math.sqrt(16))
print(math.pi)import random
print(random.randint(1, 10))
print(random.choice(['apple', 'banana', 'cherry']))The dir() function is used to list the names defined in a module.
import mymodule
print(dir(mymodule))import math
print(dir(math))The __name__ attribute is a special built-in variable that represents the name of the module.
# mymodule.py
def greet(name):
return f"Hello, {name}!"
if __name__ == "__main__":
print(greet("Pankaj"))When you run mymodule.py directly, it will print "Hello, Pankaj". When you import it into another module, the code inside the if __name__ == "__main__": block will not be executed.
Create a file named utils.py with the following content:
# utils.py
def is_even(number):
return number % 2 == 0
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)# main.py
import utils
print(utils.is_even(10)) # True
print(utils.factorial(5)) # 120Create a file named string_utils.py with the following content:
# string_utils.py
def reverse_string(s):
return s[::-1]
def is_palindrome(s):
return s == s[::-1]# main.py
import string_utils
print(string_utils.reverse_string("codeswithpankaj")) # "jaknaphtiwsecod"
print(string_utils.is_palindrome("madam")) # True
print(string_utils.is_palindrome("codeswithpankaj")) # FalseIn this tutorial, we explored the concept of modules in Python, their importance, and how to create and use them. We covered importing modules, using built-in modules, the dir() function, and the __name__ attribute. We also provided practical examples to illustrate the application of modules. Modules are a powerful feature that enhance code organization, reusability, and maintainability.
For more tutorials and in-depth explanations, visit codeswithpankaj.com!
This tutorial provides a comprehensive overview of Python modules, detailing each topic and subtopic with examples and explanations. For more such tutorials, keep following codeswithpankaj.com!