Sunday, September 7, 2025
HomeLanguagesPython Numbers | choice() function

Python Numbers | choice() function

choice() is an inbuilt function in Python programming language that returns a random item from a list, tuple, or string.

Syntax:

random.choice(sequence)
Parameters: 
sequence is a mandatory parameter that
can be a list, tuple, or string.
Returns:  
The choice() returns a random item. 

Note:We have to import random to use choice() method.

Below is the Python3 implementation of the above approach:




# Python3 program to demonstrate the use of
# choice() method 
  
# import random 
import random
  
# prints a random value from the list
list1 = [1, 2, 3, 4, 5, 6] 
print(random.choice(list1))
  
# prints a random item from the string 
string = "striver" 
print(random.choice(string))  


The output every-time will be different as the system returns a random item.
Output:

5
s

Practical application:
Print any random number 5 times from a given list.




# Python3 program to demonstrate the practical application
# choice() 
  
# import random module 
import random 
  
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
  
for x in range(5):
    print(random.choice(list1))


The output changes every time as choice() function is used.
Output:

1
4
1
5
7
RELATED ARTICLES

Most Popular

Dominic
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6641 POSTS0 COMMENTS
Nicole Veronica
11807 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11870 POSTS0 COMMENTS
Shaida Kate Naidoo
6755 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS