{"id":79302,"date":"2020-07-06T09:00:10","date_gmt":"2020-07-06T03:30:10","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=79302"},"modified":"2020-07-06T09:00:10","modified_gmt":"2020-07-06T03:30:10","slug":"modules-in-python","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/","title":{"rendered":"Modules in Python &#8211; Types and Examples"},"content":{"rendered":"<p>Like many other programming languages, Python supports <strong>modularity<\/strong>. That is, you can break large code into smaller and more manageable pieces. And through modularity, Python supports <strong>code reuse<\/strong>. You can import modules in Python into your programs and reuse the code therein as many times as you want.<\/p>\n<h3>What are Python Modules?<\/h3>\n<p>Modules provide us with a way to share reusable functions. A module is simply a \u201cPython file\u201d which contains code we can reuse in multiple Python programs. A module may contain functions, classes, lists, etc.<\/p>\n<p>Modules in Python can be of two types:<\/p>\n<ul>\n<li>Built-in Modules.<\/li>\n<li>User-defined Modules.<\/li>\n<\/ul>\n<h3>1. Built-in Modules in Python<\/h3>\n<p>One of the many superpowers of Python is that it comes with a \u201crich standard library\u201d. This rich standard library contains lots of built-in modules. Hence, it provides a lot of reusable code.<\/p>\n<p>To name a few, Python contains modules like \u201cos\u201d, \u201csys\u201d, \u201cdatetime\u201d, \u201crandom\u201d.<br \/>\nYou can import and use any of the built-in modules whenever you like in your program. (We\u2019ll look at it shortly.)<\/p>\n<h3>2. User-Defined Modules in Python<\/h3>\n<p>Another superpower of Python is that it lets you take things in your own hands. You can create your own functions and classes, put them inside modules and voila! You can now include hundreds of lines of code into any program just by writing a simple import statement.<\/p>\n<p>To create a module, just put the code inside a .py file. Let\u2019s create one.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"># my Python module\n\ndef greeting(x):\n    print(\"Hello,\", x)\n<\/pre>\n<p>Write this code in a file and save the file with the name mypymodule.py. Now we have created our own module.<\/p>\n<p>Half of our job is over, now let\u2019s learn how to import these modules.<\/p>\n<h3>Importing Modules in Python<\/h3>\n<p>We use the import keyword to import both built-in and user-defined modules in Python.<\/p>\n<p><strong>Let\u2019s import our user-defined module from the previous section into our Python shell:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import mypymodule\n\nTo call the greeting function of mypymodule, we simply need to use the dot notation:\n\n&gt;&gt;&gt; mypymodule.greeting(\"Techvidvan\")\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Hello, Techvidvan<\/div>\n<p>Similarly, we can import mypymodule into any Python file and call the greeting function as we did above.<\/p>\n<p><strong>Let\u2019s now import a built-in module into our Python shell:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import random\n\nTo call the randint function of random, we simply need to use the dot notation:\n\n&gt;&gt;&gt; random.randint(20, 100)\n\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">63<\/div>\n<p>The randint function of the random module returns a random number between a given range, here (20 to 100).<\/p>\n<p>We can import modules in various different ways to make our code more Pythonic.<\/p>\n<h3>Using import&#8230;as statement (Renaming a module)<\/h3>\n<p>This lets you give a shorter name to a module while using it in your program.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import random as r\n&gt;&gt;&gt; r.randint(20, 100)\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">54<\/div>\n<h3>Using from&#8230;import statement<\/h3>\n<p>You can import a specific function, class, or attribute from a module rather than importing the entire module. Follow the syntax below,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from &lt;modulename&gt; import &lt;function&gt;\n\n&gt;&gt;&gt; from random import randint\n&gt;&gt;&gt; randint(20, 100)\n69\n<\/pre>\n<p>You can also import multiple attributes and functions from a module:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; from math import pi, sqrt\n&gt;&gt;&gt; print(3 * pi)\n9.42477796076938\n&gt;&gt;&gt; print(sqrt(100))\n10.0\n&gt;&gt;&gt; \n<\/pre>\n<p>Note that while importing from a module in this way, we don\u2019t need to use the dot operator while calling the function or using the attribute.<\/p>\n<h3>Importing everything from Python module<\/h3>\n<p>If we need to import everything from a module and we don\u2019t want to use the dot operator, do this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; from math import *\n&gt;&gt;&gt; print(3 * pi)\n9.42477796076938\n&gt;&gt;&gt; print(sqrt(100))\n10.0\n&gt;&gt;&gt; \n<\/pre>\n<h3>Python dir() function<\/h3>\n<p>The dir() function will return the names of all the properties and methods present in a module.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import random\n&gt;&gt;&gt; dir(random)\n['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_log', '_os', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']\n&gt;&gt;&gt; \n<\/pre>\n<h3>Reloading Modules in Python<\/h3>\n<p>If you have already imported a module but need to reload it, use the reload() method. This is intended to be used in cases when you edit a source file of a module and need to test it without leaving Python.<\/p>\n<p>In Python 3.0 and above, you need to import imp standard library module to make use of this function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import random\n&gt;&gt;&gt; import imp\n&gt;&gt;&gt; imp.reload(random)\n<\/pre>\n<h3>Wrapping Up!<\/h3>\n<p>This brings us to the end of our article on modules. Modules are a great deal in programming languages as they give us the ability to reuse code in a much more manageable way. They say, \u201cPython comes with batteries included\u201d. By batteries, they mean modules. Modules are everything you need to get going. All you have to do is code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like many other programming languages, Python supports modularity. That is, you can break large code into smaller and more manageable pieces. And through modularity, Python supports code reuse. You can import modules in Python&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":79402,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[2996],"class_list":["post-79302","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-modules-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Modules in Python - Types and Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn what are Modules in Python, Types of modules- built-in, user-defined, Importing and reloading Modules, Using from...import statement,Using dir() function\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Modules in Python - Types and Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn what are Modules in Python, Types of modules- built-in, user-defined, Importing and reloading Modules, Using from...import statement,Using dir() function\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-06T03:30:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/Modules-in-Python-tv.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Modules in Python - Types and Examples - TechVidvan","description":"Learn what are Modules in Python, Types of modules- built-in, user-defined, Importing and reloading Modules, Using from...import statement,Using dir() function","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:\/\/techvidvan.com\/tutorials\/modules-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Modules in Python - Types and Examples - TechVidvan","og_description":"Learn what are Modules in Python, Types of modules- built-in, user-defined, Importing and reloading Modules, Using from...import statement,Using dir() function","og_url":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-07-06T03:30:10+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/Modules-in-Python-tv.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Modules in Python &#8211; Types and Examples","datePublished":"2020-07-06T03:30:10+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/"},"wordCount":638,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/Modules-in-Python-tv.jpg","keywords":["Modules in Python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/modules-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/","url":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/","name":"Modules in Python - Types and Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/Modules-in-Python-tv.jpg","datePublished":"2020-07-06T03:30:10+00:00","description":"Learn what are Modules in Python, Types of modules- built-in, user-defined, Importing and reloading Modules, Using from...import statement,Using dir() function","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/modules-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/Modules-in-Python-tv.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/Modules-in-Python-tv.jpg","width":1200,"height":628,"caption":"Modules in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/modules-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Modules in Python &#8211; Types and Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=79302"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79302\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/79402"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=79302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=79302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=79302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}