{"id":93,"date":"2026-03-02T07:17:10","date_gmt":"2026-03-02T02:17:10","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=93"},"modified":"2026-03-14T09:25:38","modified_gmt":"2026-03-14T04:25:38","slug":"importing-modules","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/importing-modules\/","title":{"rendered":"Importing Modules"},"content":{"rendered":"\n<p>A module is a file that contains Python code (functions, variables, classes) which can be reused in other programs.<\/p>\n\n\n\n<p>Importing modules helps you:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Reuse existing code<\/li>\n\n\n\n<li>Organize programs<\/li>\n\n\n\n<li>Use built-in libraries<\/li>\n\n\n\n<li>Save development time<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>WHAT IS A MODULE?<\/strong><\/h2>\n\n\n\n<p>A module can be:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A built-in Python module (like <code>math<\/code>)<\/li>\n\n\n\n<li>A user-defined Python file<\/li>\n\n\n\n<li>A third-party library<\/li>\n<\/ul>\n\n\n\n<p>Example: <code>math.py<\/code> is a built-in module that provides mathematical functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. BASIC IMPORT<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">import mathprint(math.sqrt(16))<\/pre>\n\n\n\n<p>Here, we import the <code>math<\/code> module and use <code>sqrt()<\/code>.<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">4.0<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. IMPORT SPECIFIC FUNCTION<\/strong><\/h2>\n\n\n\n<p>Instead of importing the whole module, you can import specific functions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from math import sqrtprint(sqrt(25))<\/pre>\n\n\n\n<p>Now you don\u2019t need to write <code>math.sqrt()<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. IMPORT MULTIPLE ITEMS<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">from math import sqrt, piprint(sqrt(9))<br>print(pi)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. USING ALIAS (AS)<\/strong><\/h2>\n\n\n\n<p>You can rename a module using <code>as<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import math as mprint(m.sqrt(36))<\/pre>\n\n\n\n<p>This is useful for long module names.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. IMPORT ALL FUNCTIONS<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">from math import *print(sqrt(49))<\/pre>\n\n\n\n<p>\u26a0 Not recommended in large programs because it can cause name conflicts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. IMPORTING USER-DEFINED MODULE<\/strong><\/h2>\n\n\n\n<p>Suppose you have a file named <code>my_module.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def greet(name):<br>    return \"Hello \" + name<\/pre>\n\n\n\n<p>In another file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import my_moduleprint(my_module.greet(\"Hira\"))<\/pre>\n\n\n\n<p>Both files must be in the same folder.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. COMMON BUILT-IN MODULES<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>math<\/code> \u2192 Mathematical functions<\/li>\n\n\n\n<li><code>random<\/code> \u2192 Random numbers<\/li>\n\n\n\n<li><code>datetime<\/code> \u2192 Date and time<\/li>\n\n\n\n<li><code>os<\/code> \u2192 Operating system functions<\/li>\n\n\n\n<li><code>sys<\/code> \u2192 System-specific parameters<\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import randomprint(random.randint(1, 10))<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>WHY IMPORT MODULES?<\/strong><\/h2>\n\n\n\n<p>\u2022 Avoid rewriting code<br>\u2022 Keep code organized<br>\u2022 Use powerful built-in tools<br>\u2022 Build scalable applications<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>KEY TAKEAWAYS<\/strong><\/h2>\n\n\n\n<p>\u2022 Use <code>import module_name<\/code> for full module<br>\u2022 Use <code>from module import item<\/code> for specific function<br>\u2022 Use <code>as<\/code> for alias<br>\u2022 Avoid <code>import *<\/code> in large projects<\/p>\n\n\n\n<p>Importing modules is essential for writing efficient and professional Python programs.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON INTERMEDIATE (PYI) > Modules and Packages > Importing Modules<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773461630741\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p><\/p>\n","protected":false},"menu_order":41,"template":"","class_list":["post-93","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Importing Modules - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn how to import and use Python modules, including built-in and user-defined ones, for reusable and organized code efficiently.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/importing-modules\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Importing Modules - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn how to import and use Python modules, including built-in and user-defined ones, for reusable and organized code efficiently.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/importing-modules\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-14T04:25:38+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/importing-modules\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/importing-modules\\\/\",\"name\":\"Importing Modules - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T02:17:10+00:00\",\"dateModified\":\"2026-03-14T04:25:38+00:00\",\"description\":\"Learn how to import and use Python modules, including built-in and user-defined ones, for reusable and organized code efficiently.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/importing-modules\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/importing-modules\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/importing-modules\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON INTERMEDIATE (PYI) > Modules and Packages > Importing Modules\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Importing Modules - One Language. Endless Possibilities","description":"Learn how to import and use Python modules, including built-in and user-defined ones, for reusable and organized code efficiently.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/importing-modules\/","og_locale":"en_US","og_type":"article","og_title":"Importing Modules - One Language. Endless Possibilities","og_description":"Learn how to import and use Python modules, including built-in and user-defined ones, for reusable and organized code efficiently.","og_url":"https:\/\/gigz.pk\/python\/lesson\/importing-modules\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-14T04:25:38+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/importing-modules\/","url":"https:\/\/gigz.pk\/python\/lesson\/importing-modules\/","name":"Importing Modules - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T02:17:10+00:00","dateModified":"2026-03-14T04:25:38+00:00","description":"Learn how to import and use Python modules, including built-in and user-defined ones, for reusable and organized code efficiently.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/importing-modules\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/importing-modules\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/importing-modules\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON INTERMEDIATE (PYI) > Modules and Packages > Importing Modules"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}