Show7_Pro

๐ŸŸข Lesson 6 โ€“ Intermediate Level

๐Ÿ”ฐ What Is a Module?

The current image has no alternative text. The file name is: Show.jpg

A module is simply a Python file (.py) containing code (functions, variables, classes) you can reuse in other programs.

Python has:

  • Built-in modules (already included with Python, like math, random, os)
  • External libraries (installed with pip, like requests, pandas)
  • Custom modules (your own .py files)

๐Ÿ“ฆ Importing a Module

pythonCopierModifierimport math

print(math.sqrt(25))  # Output: 5.0

๐ŸŽฏ Importing Specific Functions

pythonCopierModifierfrom math import sqrt, pi

print(sqrt(49))   # Output: 7.0
print(pi)         # Output: 3.141592653589793

โœ๏ธ Creating Your Own Module

  1. Create a file called mymodule.py:
pythonCopierModifierdef greet(name):
    return f"Hello, {name}!"

pi_value = 3.1416
  1. In another Python file:
pythonCopierModifierimport mymodule

print(mymodule.greet("Ali"))
print(mymodule.pi_value)

๐Ÿ“š Installing External Libraries with pip

Example: Install and Use requests

  1. In your terminal or command prompt:
bashCopierModifierpip install requests
  1. In Python:
pythonCopierModifierimport requests

response = requests.get("https://api.github.com")
print(response.status_code)

๐Ÿ” Popular Python Libraries

LibraryPurpose
mathMathematical functions
randomRandom number generation
osInteract with the operating system
datetimeDates and times
requestsHTTP requests (APIs, web data)
pandasData analysis
numpyNumerical computing

๐Ÿง  Exercise 1: Using a Built-in Module

  1. Import the random module.
  2. Generate 5 random integers between 1 and 50.
pythonCopierModifierimport random

for i in range(5):
    print(random.randint(1, 50))

๐Ÿง  Exercise 2: Create and Import Your Module

  • Create a module calculator.py with:
    • add(a, b)
    • subtract(a, b)
  • Import and use it in another script.

๐Ÿ“ Summary

  • Modules let you reuse and organize your code.
  • Use import to bring in Pythonโ€™s built-in modules.
  • Use pip install to add external libraries.
  • You can write your own modules for personal or team projects.
โฌ‡๏ธ Download Show7_Pro ุชุญู…ูŠู„ Server 1 โฌ‡๏ธ Download Show7_Pro ุชุญู…ูŠู„ Server 2 โฌ‡๏ธ Download Show7_Pro ุชุญู…ูŠู„ Server 3

Related Posts

FivG_Pro

๐Ÿ”ฐ What Is a Function? A function is a reusable block of code that performs a specific task.Instead of repeating code, we can write it once and call it whenever…

Read more

GoldiptvPro

๐ŸŸข Lesson 5 โ€“ Intermediate Level ๐Ÿ”ฐ What Are Exceptions? An exception is an error that occurs during the execution of a program.If not handled, it will stop the program…

Read more

Domingo

๐ŸŸข Lesson 4 ๐Ÿ”ฐ What Is OOP? Object-Oriented Programming (OOP) is a programming style where you organize code into objects that contain: Python supports OOP using: ๐Ÿงฑ Example: Why Use…

Read more

SHARK_PRO

๐Ÿ”ฐ What Is File Handling? ๐ŸŸข Lesson 3 File handling lets your Python programs interact with files on your computer โ€” read data from them or save data to them….

Read more

ZinaTv

๐ŸŸข Lesson 2 โ€“ Intermediate Level ๐Ÿ”น Python Dictionaries: Storing and Accessing Key-Value Data ๐Ÿ”ฐ What Is a Dictionary? A dictionary in Python is a data structure that stores data…

Read more

Pro_Click

๐Ÿ”ฐ What Are Lists ? A list is an ordered collection of items stored in a single variable. ๐Ÿ“Œ Lists are defined using square brackets []. ๐Ÿงช Example : pythonCopierModifierfruits…

Read more