Choice() function
In this tutorial, we willlearn how to use choice() functin in Python with the required tools and examples.
Choice() function is an in-built function in Python programming language. This method returns randomly selected element or item from the specified sequence of list, tuple or string.
To use choice() function, we have to import the random module.
Let us look at the simple example so that we can understand easily.
In the code, first we have to import random module and then create a list of string for example or we can take any set of items. On running the code everytime, the choice() functin will choose randomly or select randomly a item from the given list. Sometimes the selected item may be same or sometimes different.
Code:
import random str = ["My","name","is","John"] print(random.choice(str))
Output:
name John name
Explaination:
When we run a code for the first time, the choice() function will produce the output as “name”. For the second time of running the code it produces “John”, for the third time it selects “name” again as same output of first one, which means the choice() function selects elements randomly from the sequence.
Syntax
random.choice(sequence)
For syntax of choice() function firstly, we need to import random module, and then we can access the function.
Parameters in choice() function
Parameters used in Python choice() function is only one parameter called sequence. Sequence is a mandatory parameter that can be a list, tuple or string. It takes a non-empty sequence, which is a required parameter in syntax.
Return value of choice() function
The function choice() will return a random element or item fom the given sequence. If the given sequence is empty, then it will give an output as IndexError.
How to use choice() function in Python
Let us assume, selecting any two colors from the bucket.
colors = ['Blue', 'Red', 'Green' ,'Orange']
using choice() funtion for colors
import random color = ['Blue', 'Red', 'Green', 'Orange'] for _ in range(2): any = random.choice(color) print(any)
Output
Red Orange
Explaination
As we are taking two times the choice() function it will be executed twice and in result output we get two different colors.
Example for selecting more number of colors from list
import random color = ['Blue', 'Red', 'Green', 'Orange'] print(random.choice(color, k=2))
Output
['Blue', 'Green']
Exceptions using choice() function
This function also has exceptions, those are TypeError and IndexError.
TypeError exception:
When we give any number to the Python choice() function it will throw TypeError.
Code:
import random num = 191 print(random.choice(num))
Output
Traceback (most recent call last):
File "c:\Users\Dell\oop.py", line 3, in <module>
print(random.choice(num))
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\random.py", line 288, in choice i = self._randbelow(len(seq))
TypeError: object of type 'int' has no len()
Explaination
As we are giving number to the choice() function in the above code, it will result and throw an exception called TypeError.
IndexError exception:
When we pass any empty sequence to choice() function, then it will throw IndexError. An empty sequence means it could be any empty list or any empty tuple or any empty string.
Code
import random num = [] print(random.choice(num)
Output
Traceback (most recent call last):
File "c:\Users\Dell\oop.py", line 3, in <module>
print(random.choice(num))
File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\random.py", line 290, in choice raise IndexError('Cannot choose from an empty sequence') from None
IndexError: Cannot choose from an empty sequence