{"id":70,"date":"2025-10-21T04:30:06","date_gmt":"2025-10-21T04:30:06","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=70"},"modified":"2025-12-17T07:47:36","modified_gmt":"2025-12-17T07:47:36","slug":"lesson-13-modules-and-packages-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=70","title":{"rendered":"Lesson 13: Modules and Packages in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p>To learn how to <strong>use and organize code with modules and packages<\/strong>, making programs modular, reusable, and easier to maintain.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 <strong>1. What Is a Module?<\/strong><\/h2>\n\n\n\n<p>A <strong>module<\/strong> is a <strong>Python file (.py)<\/strong> containing functions, variables, and classes that can be <strong>imported<\/strong> into other programs.<\/p>\n\n\n\n<p>\u2705 <strong>Example: Using the <code>math<\/code> module<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import math\n\nprint(math.sqrt(16))   # Square root\nprint(math.factorial(5))  # Factorial\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>4.0\n120\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Creating Your Own Module<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a file <code>mymodule.py<\/code>:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet(name):\n    print(f\"Hello, {name}!\")\n\npi = 3.14159\n<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>Use it in another program:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import mymodule\n\nmymodule.greet(\"Sameer\")\nprint(\"Value of pi:\", mymodule.pi)\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, Sameer!\nValue of pi: 3.14159\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>3. Importing Specific Functions<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>from math import sqrt, factorial\n\nprint(sqrt(25))\nprint(factorial(4))\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>5.0\n24\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf0 <strong>4. Using Aliases<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import math as m\n\nprint(m.sqrt(36))\nprint(m.factorial(6))\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>6.0\n720\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 <strong>5. What Is a Package?<\/strong><\/h2>\n\n\n\n<p>A <strong>package<\/strong> is a <strong>collection of Python modules organized in directories<\/strong> with an <code>__init__.py<\/code> file.<br>It helps structure large projects into multiple modules.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example Structure:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>my_package\/\n    __init__.py\n    module1.py\n    module2.py\n<\/code><\/pre>\n\n\n\n<p><strong>Using a module from a package:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from my_package import module1\nmodule1.function_name()\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd04 <strong>6. Standard Library Modules<\/strong><\/h2>\n\n\n\n<p>Python comes with many built-in modules:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Module<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>os<\/code><\/td><td>Operating system interaction<\/td><\/tr><tr><td><code>sys<\/code><\/td><td>System-specific parameters<\/td><\/tr><tr><td><code>math<\/code><\/td><td>Mathematical functions<\/td><\/tr><tr><td><code>random<\/code><\/td><td>Random number generation<\/td><\/tr><tr><td><code>datetime<\/code><\/td><td>Date and time handling<\/td><\/tr><tr><td><code>json<\/code><\/td><td>JSON data handling<\/td><\/tr><tr><td><code>csv<\/code><\/td><td>CSV file operations<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u2705 <strong>Example: Random number<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import random\nprint(random.randint(1, 100))  # Random integer between 1 and 100\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udee0\ufe0f <strong>7. Installing External Modules<\/strong><\/h2>\n\n\n\n<p>Use <strong>pip<\/strong> to install external modules:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install requests\n<\/code><\/pre>\n\n\n\n<p>\u2705 <strong>Example: Using <code>requests<\/code> module<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\n\nresponse = requests.get(\"https:\/\/api.github.com\")\nprint(response.status_code)\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf0d <strong>8. Real-Life Example \u2013 Organizing Code<\/strong><\/h2>\n\n\n\n<p>Create a package <code>calculator<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>calculator\/\n    __init__.py\n    operations.py\n<\/code><\/pre>\n\n\n\n<p><code>operations.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add(a, b):\n    return a + b\n\ndef subtract(a, b):\n    return a - b\n<\/code><\/pre>\n\n\n\n<p>Use in another file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from calculator import operations\n\nprint(operations.add(10, 5))\nprint(operations.subtract(10, 5))\n<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>15\n5<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To learn how to use and organize code with modules and packages, making programs modular, reusable, and easier to maintain. \ud83e\udde9 1. What Is a Module? A module is a Python file (.py) containing functions, variables, and classes that can be imported into other programs. \u2705 Example: Using the math module Output: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=70"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/70\/revisions\/71"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}