{"id":166,"date":"2023-12-18T16:17:06","date_gmt":"2023-12-18T22:17:06","guid":{"rendered":"https:\/\/www.baizhao666.com\/?p=166"},"modified":"2024-07-17T18:47:16","modified_gmt":"2024-07-18T00:47:16","slug":"modules","status":"publish","type":"post","link":"https:\/\/www.baizhao666.com\/?p=166","title":{"rendered":"Modules"},"content":{"rendered":"\n<p>Python has build-in many functions, but we can still access even more functions when we &#8220;import&#8221; a &#8220;module&#8221;. A module works like a library. Since python is open source, more and more developers are working on improving the existing modules and developing new modules. What&#8217;s more, we can even make our own modules too. It&#8217;s very useful for reusing our codes and simplifying the development procedure.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Most of the modules will not be preinstalled in Python. When we are trying to access it, we need to &#8220;import&#8221; them first. The format for importing a module is: <\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import &lt;name of module&gt; (as &lt;nickname&gt;)<\/code><\/pre><\/div>\n\n\n\n<p>To use the function in the modules that we import, we can simply use &lt;name of module&gt;.&lt;name of function&gt;.<\/p>\n\n\n\n<p>The most common used modules in Python would be &#8220;math&#8221; and &#8220;random&#8221; modules. The &#8220;math&#8221; module provides a lot of math function like sin, cos, log, and exp functions that are not defined in Python. And &#8220;random&#8221; module provides random functions that can interact with not only numbers, but also lists.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># use of math module\nimport math\n# get the value of pi in the math module\n&gt;&gt;&gt; print(math.pi)\n3.141592653589793\n# use sin() function\n&gt;&gt;&gt; print(math.sin(20))\n0.9129452507276277\n# use log() function\n&gt;&gt;&gt; print(math.log(3.45))\n1.2383742310432684\n# use sqrt() function\n&gt;&gt;&gt; print(math.sqrt(36))\n6.0\n\n# there are more functions in the math module\n# floor(): return the floor of x as an Integral\n# ceil(): return the ceiling of x as an Integral\n# gcd(): Greatest Common Divisor\n# fabs(): return the absolute value of the float x\n\n\n# use of random module\nimport random\n# random float from [0,1)\nrandom.random()\n# random integer from [start, end]\nrandom.randint(start, end) # include both start and end points\n# random integer using &quot;range&quot; format\nrandom.randrange(start, end) # not include end point\n# randomly choose from a list\nrandom.choice(list)\n# randomly shuffle a list\nrandom.shuffle(list)\n# there are more functions in the random module about some distribution,\n# such as normal distribution, gaussian distribution<\/code><\/pre><\/div>\n\n\n\n<p>Sometimes in our program, we can write our own helper functions in some .py file. These .py files can be our own &#8220;module&#8221;s. In order to use them, we can write import &lt;name of file&gt;. If we only want to use one or some of the functions of the module, we can only import the functions that we need. We can also use &#8220;*&#8221; to import all functions, although this is rarely used.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code># for example, we only want to use the sin() function in math module\n&gt;&gt;&gt; from math import sin\n# in this case, we can use the function directly\n&gt;&gt;&gt; print(sin(20))\n0.9129452507276277\n\n# we can also set a &quot;nickname&quot; for the module \n# so that we don&#39;t need to type the full name of the module\n&gt;&gt;&gt; import random as rd\n&gt;&gt;&gt; print(rd.random())\n0.8025982517989496\n\n# the &quot;nickname&quot; also works for functions\n&gt;&gt;&gt; from random import randrange as rr\n&gt;&gt;&gt; print(rr(1, 10))\n1<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Python has build-in many functions, but we can still access even more functions when we &#8220;import&#8221; a &#8220;module&#8221;. A module works like a library. Since python is open source, more and more developers are working on improving the existing modules and developing new modules. What&#8217;s more, we can even make our own modules too. It&#8217;s &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.baizhao666.com\/?p=166\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Modules&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-166","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=166"}],"version-history":[{"count":2,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":281,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=\/wp\/v2\/posts\/166\/revisions\/281"}],"wp:attachment":[{"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.baizhao666.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}