Open In App

Python Numbers | choice() function

Last Updated : 10 Jan, 2018
Improve
Improve
Like Article
Like
Save
Share
Report


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

Previous Article
Next Article

Similar Reads

numpy.random.choice() in Python
With the help of choice() method, we can get the random samples of one dimensional array and return the random samples of numpy array. Syntax : numpy.random.choice(a, size=None, replace=True, p=None) Parameters: 1) a - 1-D array of numpy having random samples. 2) size - Output shape of random samples of numpy array. 3) replace - Whether the sample
1 min read
How to get weighted random choice in Python?
Weighted random choices mean selecting random elements from a list or an array by the probability of that element. We can assign a probability to each element and according to that element(s) will be selected. By this, we can select one or more than one element from the list, And it can be achieved in two ways. By random.choices()By numpy.random.ch
3 min read
Python EasyGUI – Multi Choice Box
Multi Choice Box : It is used to display a window having a multiple options i.e items in EasyGUI, it can be used where there is a need to select multiple item among a group of items, it consist of title, message to be displayed, group of items and buttons i.e "Cancel", "Select All", "Clear All", "Ok" buttons to confirm the selection of the item. It
2 min read
Python EasyGUI – Choice Box
Choice Box : It is used to display a window having a multiple options i.e items in EasyGUI, it can be used where there is a need to select any one item among a group of items, it consist of title, message to be displayed, group of items and a pair of "Ok", "Cancel" button to confirm the selection of the item. Below is how the choice box looks like
2 min read
Python MCQ (Multiple Choice Questions) with Answers
Python is a free open-source, high-level and general-purpose with a simple and clean syntax which makes it easy for developers to learn Python. Python programming language (latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Software Industry. In this Python MCQ article, you wi
3 min read
Choice Selection Fields in serializers - Django REST Framework
In Django REST Framework the very concept of Serializing is to convert DB data to a datatype that can be used by javascript. Every serializer comes with some fields (entries) which are going to be processed. For example if you have a class with name Employee and its fields as Employee_id, Employee_name, is_admin, etc. Then, you would need AutoField
5 min read
An Introduction to Freelancing: The Career Choice for Modern Millennials
Are you a night owl? Perhaps you fancy working late at night and sleeping well into the morning (Like I do!!!) Unfortunately, my friend, that is not really possible in a conventional 9 to 5 job. But fear not, Freelancing is here to solve your problem!!! Freelancing is increasingly becoming a lucrative prospect for many working professionals, especi
6 min read
Convert Strings to Numbers and Numbers to Strings in Python
In Python, strings or numbers can be converted to a number of strings using various inbuilt functions like str(), int(), float(), etc. Let's see how to use each of them. Example 1: Converting a Python String to an int: C/C++ Code # code # gfg contains string 10 gfg = "10" # using the int(), string is auto converted to int print(int(gfg)+2
2 min read
Python map function | Count total set bits in all numbers from 1 to n
Given a positive integer n, count the total number of set bits in binary representation of all numbers from 1 to n. Examples: Input: n = 3 Output: 4 Binary representations are 1, 2 and 3 1, 10 and 11 respectively. Total set bits are 1 + 1 + 2 = 4. Input: n = 6 Output: 9 Input: n = 7 Output: 12 Input: n = 8 Output: 13 We have existing solution for t
2 min read
Python3 Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples:  Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6, 3, 8, 5, 2} Explanation: Even element = {2, 4, 6
2 min read
Article Tags :
Practice Tags :