{"id":5963,"date":"2022-04-16T19:04:52","date_gmt":"2022-04-16T17:04:52","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5963"},"modified":"2026-01-23T20:55:32","modified_gmt":"2026-01-23T19:55:32","slug":"modules-in-python","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/04\/16\/modules-in-python\/","title":{"rendered":"Modules in Python"},"content":{"rendered":"<p>So what is a module? The <a href=\"https:\/\/docs.python.org\/3\/tutorial\/modules.html\" target=\"_blank\" rel=\"noopener\">Python Tutorial<\/a> defines it as <b>a file containing Python definitions and statements<\/b>, which can be later imported and used when necessary.<\/p>\n<p><!--more--><\/p>\n<p>1. If you want to import a module as a whole, you can do it using the import module_name statement. You are allowed to import more than one module at once using a comma-separated list. For example:<\/p>\n<pre class=\"lang:default decode:true \">import mod1\r\nimport mod2, mod3, mod4<\/pre>\n<p>although the latter form is not recommended due to stylistic reasons, and it&#8217;s better and prettier to express the same intention in more a verbose and explicit form, such as:<\/p>\n<pre class=\"lang:default decode:true \">import mod2\r\nimport mod3\r\nimport mod4<\/pre>\n<p>&nbsp;<\/p>\n<p>2. If a module is imported in the above manner and you want to access any of its entities, you need to prefix the entity&#8217;s name using dot notation. For example:<\/p>\n<pre class=\"lang:default decode:true \">import my_module\r\n\r\nresult = my_module.my_function(my_module.my_data)<\/pre>\n<p>The snippet makes use of two entities coming from the <strong><code>my_module<\/code><\/strong> module: a function named <strong><code>my_function()<\/code><\/strong> and a variable named <strong><code>my_data<\/code><\/strong>. Both names must be prefixed by<strong><code> my_module<\/code><\/strong>. None of the imported entity names conflicts with the identical names existing in your code&#8217;s namespace.<\/p>\n<p>&nbsp;<\/p>\n<p>3. You are allowed not only to import a module as a whole, but to import only individual entities from it. In this case, the imported entities must not be prefixed when used. For example:<\/p>\n<pre class=\"lang:default decode:true\">from module import my_function, my_data\r\n\r\nresult = my_function(my_data)<\/pre>\n<p>The above way &#8211; despite its attractiveness &#8211; is not recommended because of the danger of causing conflicts with names derived from importing the code&#8217;s namespace.<\/p>\n<p>&nbsp;<\/p>\n<p>4. The most general form of the above statement allows you to import all entities offered by a module:<\/p>\n<pre class=\"lang:default decode:true \">from my_module import *\r\n\r\nresult = my_function(my_data)<\/pre>\n<p>Note: this import&#8217;s variant is not recommended due to the same reasons as previously (the threat of a naming conflict is even more dangerous here).<\/p>\n<p>&nbsp;<\/p>\n<p>5. You can change the name of the imported entity &#8220;on the fly&#8221; by using the<strong><code> as<\/code><\/strong> phrase of the <strong><code>import<\/code><\/strong>. For example:<\/p>\n<pre class=\"lang:default decode:true \">from module import my_function as fun, my_data as dat\r\n\r\nresult = fun(dat)<\/pre>\n<p>&nbsp;<\/p>\n<p><em>Exercise 1<\/em><\/p>\n<p>You want to invoke the function <strong><code>make_money()<\/code> <\/strong>contained in the module named <code><strong>mint<\/strong><\/code>. Your code begins with the following line:<\/p>\n<pre class=\"lang:default decode:true \">import mint<\/pre>\n<p>What is the proper form of the function&#8217;s invocation?<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">mint.make_money()<\/pre>\n<p>&nbsp;<\/p>\n<p><em>Exercise 2<\/em><\/p>\n<p>You want to invoke the function <strong><code>make_money()<\/code><\/strong> contained in the module named <code><strong>mint<\/strong><\/code>. Your code begins with the following line:<\/p>\n<pre class=\"lang:default decode:true \">from mint import make_money<\/pre>\n<p>What is the proper form of the function&#8217;s invocation?<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">make_money()<\/pre>\n<p>&nbsp;<\/p>\n<p><em>Exercise 3<\/em><\/p>\n<p>You&#8217;ve written a function named <strong><code>make_money<\/code> <\/strong>on your own. You need to import a function of the same name from the <strong><code>mint<\/code> <\/strong>module and don&#8217;t want to rename any of your previously defined names. Which variant of the import statement may help you with the issue?<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \"># sample solution\r\nfrom mint import make_money as make_more_money<\/pre>\n<p>&nbsp;<\/p>\n<p><em>Exercise 4<\/em><\/p>\n<p>What form of the <strong><code>make_money<\/code> <\/strong>function invocation is valid if your code starts with the following line:<\/p>\n<pre class=\"lang:default decode:true\">from mint import *<\/pre>\n<p>?<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">make_money()<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Working with standard modules<\/strong><\/p>\n<p>1. A function named <strong><code>dir()<\/code><\/strong> can show you a list of the entities contained inside an imported module. For example:<\/p>\n<pre class=\"lang:default decode:true \">import os\r\ndir(os)<\/pre>\n<p>prints out the list of all the os module&#8217;s facilities you can use in your code.<\/p>\n<p>2. The math module couples more than 50 symbols (functions and constants) that perform mathematical operations (like<code> sine()<\/code>, <code>pow()<\/code>, <code>factorial()<\/code>) or providing important values (like <code>\u03c0<\/code> and the Euler symbol <code>e<\/code>).<\/p>\n<p>3. The random module groups more than 60 entities designed to help you use pseudo-random numbers. Don&#8217;t forget the prefix &#8220;<code>random<\/code>&#8220;, as there is no such thing as a real random number when it comes to generating them using the computer&#8217;s algorithms.<\/p>\n<p>4. The platform module contains about 70 functions which let you dive into the underlaying layers of the OS and hardware. Using them allows you to get to know more about the environment in which your code is executed.<\/p>\n<p>5. Python Module Index (https:\/\/docs.python.org\/3\/py-modindex.html is a community-driven directory of modules available in the Python universe. If you want to find a module fitting your needs, start your search there.<\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 1<\/em><\/p>\n<p>What is the expected value of the result variable after the following code is executed?<\/p>\n<pre class=\"lang:default decode:true \">import math\r\nresult = math.e == math.exp(1)<\/pre>\n<p>&nbsp;<\/p>\n<p><code>True<\/code><\/p>\n<p><em>Exercise 2<\/em><\/p>\n<p>(Complete the sentence) Setting the generator&#8217;s seed with the same value each time your program is run guarantees that&#8230;<\/p>\n<p>&#8230; the pseudo-random values emitted from the random module will be exactly the same.<\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 3<\/em><\/p>\n<p>Which of the platform module&#8217;s functions will you use to determine the name of the CPU running inside your computer?<\/p>\n<p>The<code> processor()<\/code> function<\/p>\n<p>&nbsp;<\/p>\n<p><em>Exercise 4<\/em><\/p>\n<p>What is the expected output of the following snippet?<\/p>\n<pre class=\"lang:default decode:true \">import platform\r\nprint(len(platform.python_version_tuple()))<\/pre>\n<p>&nbsp;<\/p>\n<p><code>3<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Exercise 5<\/p>\n<p>What is the expected output of the following code?<\/p>\n<pre class=\"lang:default decode:true\">import math\r\n     \r\ndef my_func(x,y):\r\n    z = math.trunc(x) + math.ceil(x) - math.floor(y)\r\n    return z\r\n     \r\nprint(my_func(-1.6,-3.2))<\/pre>\n<p><code>trunc(x)<\/code> :\u00a0 returns the value of x <strong>truncated <\/strong>to an integer.<\/p>\n<p><code>ceil(x)<\/code> :\u00a0 returns the <strong>smallest <\/strong>integer <strong>greater than or equal to x<\/strong>.<\/p>\n<p><code>floor(x)<\/code> :\u00a0 returns the <strong>largest <\/strong>integer <strong>less than or equal to x.<\/strong><\/p>\n<p>So:<\/p>\n<p><code>math.trunc(-1.6)<\/code> returns <code>-1<\/code><\/p>\n<p><code>math.ceil(-1.6)<\/code> returns <code>-1<\/code><\/p>\n<p><code>math.floor(-3.2)<\/code> returns <code>-4<\/code><\/p>\n<p>and <code>my_func(-1.6,-3.2)<\/code> returns <code>2<\/code> (-1 -1 -(-4) = 2)<\/p>\n<p>&nbsp;<\/p>\n<p>Exercise 6.<\/p>\n<p>You want to use a function named <code>hyppo()<\/code> that resides in a module named <code>animals <\/code>, and it has been imported using the following line:<\/p>\n<p><code>import animals as hyppos<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><strong><code>hyppos.hyppo()<\/code><\/strong><\/p>\n<p>Here the <code>animals <\/code>module is imported using aliasing.\u00a0Aliasing causes the module to be identified under a different name than the original &#8211; in this case the alias is <code>hyppos<\/code>.<\/p>\n<p>Because the whole module is imported, calling any entity from that module must be made using both the module name and the entity name as <em>module.entity <\/em>where module is the name of the module OR its alias.<\/p>\n<p>So calling the <code>hyppo()<\/code> function is done as <code>hyppos.hyppo()<\/code>.<\/p>\n<p><code>hyppo()<\/code> would have been a correct answer if the module had been imported as : <code>from animals import hyppo<\/code>.<\/p>\n<p><code>animals.hyppo()<\/code> wouldd have been a correct answer if the module had been imported without an alias : <code>import animals<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Exercise 7.<\/p>\n<p>Consider the code below :<\/p>\n<pre class=\"lang:default decode:true \">from random import random\r\nfrom math import floor\r\n     \r\nfor i in range(5):\r\n    print(floor(random()*10),end=',')<\/pre>\n<p>What would be a possible result from the above code ? (Pick 2)<\/p>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<p>A. <code>1,6,5,2,0,<\/code><br \/>\nB.<code> 2,7,3,10,2,<\/code><br \/>\nC. <code>1,4,4,0,8,<\/code><br \/>\nD. <code>0,10,20,40,10,<\/code><\/p>\n<\/div>\n<p>A and C<\/p>\n<p>Explanation:<\/p>\n<p>The <code>random()<\/code> function (from the random module) returns a <strong>random floating point number in the range [0.0, 1.0)<\/strong> (in other words: (0.0 &lt;= x &lt; 1.0).<\/p>\n<p>The <code>floor()<\/code> method (from the math module) returns <strong>the largest integer not greater than x<\/strong>.<\/p>\n<p>For example :<\/p>\n<p><code>floor(3.4)<\/code> returns <code>3<\/code><\/p>\n<p><code>floor(-3.6)<\/code> returns <code>-4<\/code><\/p>\n<p>So, <code>floor(random()*10)<\/code>\u00a0 would returns an integer between 0 and 9 (10 is not included since 1.0 is not included in the range of the random() function).<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 8<\/p>\n<p>What is the expected output of the following snippet ?<\/p>\n<pre class=\"lang:default decode:true \">    from math import factorial\r\n     \r\n    try:\r\n        print(factorial(-4))\r\n    except:\r\n        print(\"Error #1\")\r\n    except ValueError:\r\n        print(\"Error #2\")<\/pre>\n<p>A. <code>24<\/code><\/p>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>B. <code>Error #1<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>C. None of the other answers are correct.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>D.<code>Error #2<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><em>Explanation:<\/em><\/p>\n<p>The <code>factorial()<\/code> function is part of the <code>math <\/code>module and allows to compute the factorial of a number. This number must be a positive integer; if not a <em>ValueError <\/em>exception is raised.<\/p>\n<p>In the above code, the unnamed <code>except<\/code> branch is placed <em>before <\/em>a named <code>except<\/code> branch : this is not a correct syntax and the code will generate a <strong>syntax error<\/strong>. When using multiple <code>except<\/code> branches, <strong>if using an unnamed <\/strong><code><strong>except<\/strong><\/code><strong> branch, it must be placed last.<\/strong><\/p>\n<p>Try it yourself:<\/p>\n<pre class=\"lang:default decode:true \">    # this will raise a Syntax Error:\r\n    from math import factorial\r\n     \r\n    try:\r\n        print(factorial(-4))\r\n    except:\r\n        print(\"Error #1\")\r\n    except ValueError:\r\n        print(\"Error #2\")\r\n    # SyntaxError: default 'except:' must be last\r\n     \r\n    # Corrected code should be as below:\r\n    from math import factorial\r\n     \r\n    try:\r\n        print(factorial(-4))\r\n    except ValueError:\r\n        print(\"Error #2\")    # Error #2\r\n    except:\r\n        print(\"Error #1\")<\/pre>\n<p>C.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 9<\/p>\n<div id=\"question-prompt\" class=\" ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>You are being asked to create a Python program that will ask a user for the leg lengths of a right-angle triangle and returns the length of the hypotenuse.<\/p>\n<p>Which code snippet below will achieve this requirement ?<\/p>\n<p>A.<\/p>\n<pre class=\"lang:default decode:true \">    import math\r\n     \r\n    x=float(input(\"Enter first length: \"))\r\n    y=float(input(\"Enter second length: \"))\r\n    print(\"Length of hypotenuse is: \", math.hypotenuse(x,y))<\/pre>\n<p>B.<\/p>\n<pre class=\"lang:default decode:true \">    import hypot\r\n     \r\n    x=float(input(\"Enter first length: \"))\r\n    y=float(input(\"Enter second length: \"))\r\n    print(\"Length of hypotenuse is: \", hypot(x,y))<\/pre>\n<p>C.<\/p>\n<pre class=\"lang:default decode:true \">    import math\r\n     \r\n    x=input(\"Enter first length: \")\r\n    y=input(\"Enter second length: \")\r\n    print(\"Length of hypotenuse is: \", math.hypot(x,y))<\/pre>\n<p>D.<\/p>\n<pre class=\"lang:default decode:true \">    from math import hypot\r\n     \r\n    x=float(input(\"Enter first length: \"))\r\n    y=float(input(\"Enter second length: \"))\r\n    print(\"Length of hypotenuse is: \", hypot(x,y))<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<p>Function <code>hypot()<\/code>, from the <strong>math <\/strong>module, returns the length of the hypotenuse of a right-angle triangle with the leg lengths equal to x and y.<\/p>\n<p>This function can be imported either as : <code>from math import hypot<\/code>\u00a0 (in this case the function can be used as <code>hypot()<\/code> in the program) or : <code>import math<\/code>\u00a0 (in this case the function can be used as <code>math.hypot()<\/code> in the program).<\/p>\n<p>Additionally, when using the<code> input()<\/code> function, keep in mind that the function always returns a <em>string<\/em> &#8211; so the output of <code>input()<\/code> must be converted (in this case to a <em>float<\/em>) to be used with the <code>hypot()<\/code> function.<\/p>\n<p>Based on this, only the following answer is correct :<\/p>\n<pre class=\"lang:default decode:true \">    from math import hypot\r\n     \r\n    x=float(input(\"Enter first length: \"))\r\n    y=float(input(\"Enter second length: \"))\r\n    print(\"Length of hypotenuse is: \", hypot(x,y))<\/pre>\n<p>D.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 10<\/p>\n<div id=\"question-prompt\" class=\" ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>You are being asked to create a Python program that will randomly return five unique integers between 0 and 100 (both 0 and 100 are included as possible choices).<\/p>\n<p>Which code will achieve this requirement ?<\/p>\n<p>A.<\/p>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">import random\r\nprint(\"Selected numbers are: \", random.randrange(0,101,5))<\/pre>\n<p>B.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">import random\r\nprint(\"Selected numbers are: \", random.choices(range(101),k=5))<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">C.<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">import random\r\nprint(\"Selected numbers are: \", random.sample(range(101),k=5))<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">D.<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">import random\r\nprint(\"Selected numbers are: \", [random.randint(0,101) for x in range(5)])<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\"><code><\/code><\/p>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><strong>Explanation:<\/strong><\/p>\n<p>Let&#8217;s review each suggested answers:<\/p>\n<p>&#8211;&gt; <code>print(\"Selected numbers are: \", random.randrange(0,101,5))<\/code><\/p>\n<p>Function <code>randrange(<em>start<\/em>, <em>stop<\/em>[, <em>step<\/em>])<\/code>\u00a0 (or <code>randrange(<em>stop<\/em>)<\/code>) returns <strong>one <\/strong>randomly selected element from <code>range(start, stop, step)<\/code>.\u00a0 So, <code>random.randrange(0,101,5))<\/code> will return one integer from <code>range(0,101,5)<\/code> . The requirement calls for five integers, not one &#8211; so this will not work.<\/p>\n<p>&#8211;&gt; <code>print(\"Selected numbers are: \", [random.randint(0,101) for x in range(5)])<\/code><\/p>\n<p>Function <code>randint(<em>a<\/em>, b)<\/code>\u00a0returns a random integer <em>N<\/em> such that <code>a &lt;= N &lt;= b<\/code>.\u00a0 So, <code>[random.randint(0,101) for x in range(5)]<\/code>\u00a0 (a list comprehension) will return a list of five integers between 0 and 101 (included) &#8211; also those elements will not necessarily be unique. So this is not complying with the requirements and it is not the correct answer.<\/p>\n<p>&#8211;&gt; <code>print(\"Selected numbers are: \", random.choices(range(101),k=5))<\/code><\/p>\n<p>Function <code>choices(<em>population<\/em>, <em>k=1<\/em>)<\/code>\u00a0returns a <em>k<\/em> sized list of elements chosen from the population &#8211; those elements may be repeated. So, <code>random.choices(range(101),k=5)<\/code>\u00a0 will return a list of five non-unique integers between 0 and 100 (included). Because the requirement calls for uniqueness, this answer is incorrect. Note : the <code>choices()<\/code> function has some additional optional weight parameters not discussed here.<\/p>\n<p>&#8211;&gt; <code>print(\"Selected numbers are: \", random.sample(range(101),k=5))<\/code><\/p>\n<p><strong>Function <code>sample(<em>population<\/em>, <em>k<\/em>)<\/code>\u00a0returns a k length list of unique elements chosen from the population sequence. So, <code>random.sample(range(101),k=5)<\/code>\u00a0 will return a list of five unique integers between 0 and 100 (included). This is exactly what the requirement calls for and this is the correct answer. Note : the <code>sample()<\/code> function has one additional optional parameter not discussed here.<\/strong><\/p>\n<p>More details on the random module available at <a href=\"https:\/\/docs.python.org\/3\/library\/random.html\">https:\/\/docs.python.org\/3\/library\/random.html<\/a><\/p>\n<p>Try it yourself:<\/p>\n<pre class=\"lang:default decode:true \">    # Because we are dealing with random numbers here, your output may\r\n    # be different from the one below !\r\n    import random\r\n     \r\n    print(\"Selected numbers are: \", random.randrange(0,101,5))\r\n    # Selected numbers are:  55\r\n     \r\n    print(\"Selected numbers are: \", [random.randint(0,101) for x in range(5)])\r\n    # Selected numbers are:  [96, 46, 41, 87, 3]\r\n     \r\n    print(\"Selected numbers are: \", random.choices(range(101),k=5))\r\n    # Selected numbers are:  [49, 0, 99, 61, 41]\r\n     \r\n    print(\"Selected numbers are: \", random.sample(range(101),k=5))\r\n    # Selected numbers are:  [62, 92, 22, 47, 13]<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>So what is a module? The Python Tutorial defines it as a file containing Python definitions and statements, which can be later imported and used when necessary.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5963"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=5963"}],"version-history":[{"count":18,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5963\/revisions"}],"predecessor-version":[{"id":6284,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5963\/revisions\/6284"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5963"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}