avatarJim McAulay🍁 I'm nobody. Are you a nobody too?

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1878

Abstract

in may require minor adjustments to run.</p><p id="9e3a">In Python 3.8.5 the function randrange in the random module was introduced.</p><p id="5cb0">Prior to the release of Python 3.8.5 if you wanted to generate a random number between two integers you would use randint. Randint continues to be used for that purpose.</p><div id="9f86"><pre>import <span class="hljs-built_in">random</span>() print (<span class="hljs-built_in">random</span>.randint (<span class="hljs-number">1</span>,<span class="hljs-number">10</span>)) would <span class="hljs-literal">return</span> <span class="hljs-keyword">a</span> randomly generated <span class="hljs-built_in">number</span> between <span class="hljs-number">1</span> <span class="hljs-keyword">and</span> <span class="hljs-number">10.</span></pre></div><p id="d24b">A very common usage of randint is to generate a number corresponding to the contents of a list. In python a list is indexed starting at 0. This means that if you wanted to generate a random number corresponding to a list of 10 items. You would use the following code.</p><div id="267e"><pre><span class="hljs-keyword">import</span> <span class="hljs-built_in">random</span>() <span class="hljs-built_in">print</span> (<span class="hljs-built_in">random</span>.<span class="hljs-property">randint</span> (<span class="hljs-number">0</span>,<span class="hljs-number">9</span>))</pre></div><p id="75a9">With the advent of Python 3.8.5 you could write instead</p><div id="4822"><pre><span class="hljs-keyword">import</span> <span class="hljs-built_in">random</span> () <span class="hljs-built_in">print</span> (<span class="hljs-built_in">random</span>.<span class="hljs-property">randrange</span> (<span class="hljs-number">10</span>)</pre></div><p id="5ba5">If you want to create a function that takes a list of indeterminate length you could use the following code.<

Options

/p><div id="6d0c"><pre><span class="hljs-keyword">import</span> random</pre></div><div id="af56"><pre>def rand_num (my_list): <span class="hljs-built_in">print</span> (<span class="hljs-built_in">random</span>.randrange (<span class="hljs-built_in">len</span>(my_list)))</pre></div><div id="e3a8"><pre><span class="hljs-attribute">my_list</span> = [<span class="hljs-string">"A"</span>, <span class="hljs-string">"B"</span>, <span class="hljs-string">"C"</span>,] rand_num (my_list) </pre></div><div id="d975"><pre># <span class="hljs-keyword">returns</span> randomly <span class="hljs-keyword">generated</span> number <span class="hljs-keyword">between</span> <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> <span class="hljs-number">2</span></pre></div><p id="2e31">To accomplish the same thing prior to the release of Python 3.8.5 you would have to use the following code</p><div id="68f5"><pre><span class="hljs-keyword">import</span> random</pre></div><div id="ae6c"><pre>def rand_num (my_list): <span class="hljs-built_in">print</span> (<span class="hljs-built_in">random</span>.randint (<span class="hljs-number">0</span>,<span class="hljs-built_in">len</span>(my_list)<span class="hljs-number">-1</span>))</pre></div><div id="ab0b"><pre><span class="hljs-attribute">my_list</span> = [<span class="hljs-string">"A"</span>, <span class="hljs-string">"B"</span>, <span class="hljs-string">"C"</span>,] rand_num (my_list)</pre></div><p id="4f9d"><a href="https://readmedium.com/c4d5fe0b9b6c?source=post_page-----acf8106ab83d----------------------">Jim McAulay🍁</a> says, <a href="https://readmedium.com/c4d5fe0b9b6c?source=post_page-----acf8106ab83d----------------------">Jim McAulay🍁</a> says, “ Politicians and diapers should be changed regularly for the same reason”</p><p id="ba90">44__45</p><p id="6b3b">12__10</p></article></body>

Keeping Up With The Latest Version Of Python

The introduction of randrange in Python 3.8.5

Photo by Nathan Dumlao on Unsplash

In this story I provide an example of how Python is changing by discussing the introduction of the function randrange in Python 3.8.5.

A language such as English is constantly changing new words continue to be added and older words or expressions become less common.When I was in high school I would have been confused if somebody had told me to find the answer to a question by googling it.Thousands of new words have been added to our language since then.

A computer language is no different it is constantly changing. If you are looking at a Python module and you see.

print "hello world"

You would be looking at code that was probably written quite some time ago. Python 3.0 was introduce in 2008. Programmers continued to write code in Python 2.x for many years. However code in Python 3.0 is not backward compatible with 2.x.

What that means is that if you are using Python 3.0 or later you can not use it to understand code written in Python 2.x

Since 2008 minor changes and updates to the language have been happening on a regular basis. If you are using Python 3.8. You can read code written with Python 3.0 and if you are using Python 3.0 it may work okay in Python 3.8 or in may require minor adjustments to run.

In Python 3.8.5 the function randrange in the random module was introduced.

Prior to the release of Python 3.8.5 if you wanted to generate a random number between two integers you would use randint. Randint continues to be used for that purpose.

import random() 
print (random.randint (1,10))  would return a randomly generated number between 1 and 10.

A very common usage of randint is to generate a number corresponding to the contents of a list. In python a list is indexed starting at 0. This means that if you wanted to generate a random number corresponding to a list of 10 items. You would use the following code.

import random()
print (random.randint (0,9))

With the advent of Python 3.8.5 you could write instead

import random ()
print (random.randrange (10)

If you want to create a function that takes a list of indeterminate length you could use the following code.

import random
def rand_num (my_list):
   print (random.randrange (len(my_list)))
my_list = ["A", "B", "C",]
rand_num (my_list)   
# returns randomly generated number between 0 and 2

To accomplish the same thing prior to the release of Python 3.8.5 you would have to use the following code

import random
def rand_num (my_list):
    print (random.randint  (0,len(my_list)-1))
my_list = ["A", "B", "C",]
rand_num (my_list)

Jim McAulay🍁 says, Jim McAulay🍁 says, “ Politicians and diapers should be changed regularly for the same reason”

44__45

12__10

Python
Programming For Beginners
Technology
Jim Mcaulay
Illumination
Recommended from ReadMedium