choice() in Python

In this tutorial, we will discuss the usage of the choice() method in Python.

To use this in our program, we need to first import the random module.

The function of choice() is to pick or generate a random element that can be anything a number or a string from a given collection.

Syntax

The syntax for using choice() is

Let us have a look at some programs that will make it's application clear.

Program 1:

Consider the following program-

Output:

Java

Explanation:

It's time to have a look at the explanation of the program given above-

  1. In the first step, we have imported the random module.
  2. After this, we have initialized a list that contains different string values.
  3. Finally, we have used the choice() to pick a certain string from the provided list and display its value.

Program 2:

In the second program, we will see how it can be used to take a random character from the string.

Output:

y

Explanation:

Let's understand what happened here-

  1. In the first step, we have imported the random module.
  2. After this, we have initialized a string called "Python at JavaTpoint.
  3. Finally, we have used the choice() to pick a certain character from the provided string and display it's value.

Program 3:

Now, let's have a look at one more program and discuss another approach of writing this method.

Output:

The random element from(1,2,3,4,5) is  5
The random element from(a,b,c,d,e,f) is f 

Explanation:

Let's see what we have done in this program-

  1. In the first step, we have imported the random module.
  2. After this, we have written two print statements that will display the value.
  3. And then we can observe that we have used the random.choice() in the print function itself to pick a certain character or element from the provided list and display its value.

Program 4:

In the next program, we shall see how we can choose a definite number of values to be displayed in the output.

Output:

[2,2,3,2]

Explanation:

It's time to have a look at the explanation of the program given above-

  1. In the first step, we have imported the random module.
  2. After this, we have initialized a list that contains different numeric values.
  3. Finally, we have used the random.choice() in which we have specified the list name and k value as parameters.
  4. On executing the program, we can observe that the number of elements in the returned list depends on the value of k.

Program 5:

Now, we will see how a for loop can be used to meet the same objective.

The following program illustrates the same-

Output:

1
3
1
3
1
4

Program 6:

In the next program, we will see how a random value from a given range of numbers can be taken.

Output:

A random number from the given range is 31.

Explanation:

Let's see what we have done in this program-

  1. In the first step, we have imported the random module.
  2. After this, we have specified the range of numbers in choice().
  3. On executing the given program, it displays the expected output.

Now, we will learn how to get the Boolean values using choice().

Displaying Boolean Result

The following program shows how we can obtain the Boolean values.

Output:

The boolean value is False

Explanation:

It's time to have a look at the explanation of the program given above-

  1. In the first step, we have imported the random module.
  2. After this, we have specified True and False inside choice().
  3. On executing the given program, the expected output is displayed.

Program 7:

Finally, the last program illustrates how random.getrandbits() can be used for the same purpose.

Output:

False

The procedure of the above program is similar to the previous program, the only difference is here we have used random.getrandbits().

Conclusion

In this tutorial, we learned the different ways of using choice() in our Python program.