Python random.choice() Method: Print Random Elements

Python | random.choice() method with Example: In this tutorial, we are going to learn about the choice() function which is an in-built function of a random module, and how can we generate and print random numbers. By IncludeHelp Last updated : January 12, 2024

Python random.choice() Method: Description & Use

The random() is an in-built function of 'random' module in Python, it is used to return a random element from a container like object like lists, string, tuple etc.

Import statement

To use the choice() method, you need to import the random module. Consider the below import statement:

import random

Syntax

Below is the syntax of random.choice() method:

random.choice(container_type_object)

Parameters

Here, container_type_object may a string, list of numbers, list of strings, tuple etc.

Sample Input & Output

The below example demonstrates the working of the random.choice() method with the help of sample input and output.

Input string: "Hello"

Output (May different)
e - first time
H - second time

Input:
random.choice([1, 2, 3, 4, 5, 6])
Output:
A random number from 1 to 6

Examples of Python random.choice() Method

Practice these examples to understand the concept of generating random elements using the random.choice() method.

Example 1: Generate random characters from a string

Given a string, generate and print random characters from it.

# import statement
import random

# defining string
str_var = "Hello!"

# Generating & pritning 3 random
# characters from string 'str_var'
print("Random chars from string: ", str_var)
print(random.choice(str_var))
print(random.choice(str_var))
print(random.choice(str_var))

Output

The output of the above program is:

RUN 1:
Random chars from string:  Hello!
!
l
o

RUN 2:
Random chars from string:  Hello!
o
l
!

Example 2: Generate random numbers from a list of numbers

Given a list of numbers, generate and print random numbers from it.

# import statement
import random

# defining list of numbers
num_list = [10, 20, 30, 40, 50]

# Generating & pritning 3 random
# numbers from number list 'num_list'
print("Random numbers from num list: ", num_list)
print(random.choice(num_list))
print(random.choice(num_list))
print(random.choice(num_list))

Output

The output of the above program is:

RUN 1:
Random numbers from num list:  [10, 20, 30, 40, 50]
20
40
10

RUN 2:
Random numbers from num list:  [10, 20, 30, 40, 50]
30
20
20

Example 3: Generate random strings from a list of strings

Given a list of strings, generate and print random strings from it.

# import statement
import random

# defining list of strings
str_list = ["Hello", "Hi", "Bye"]

# Generating & pritning 3 random
# strings from strings list 'str_list'
print("Random strings from strings list: ", str_list)
print(random.choice(str_list))
print(random.choice(str_list))
print(random.choice(str_list))

Output

The output of the above program is:

RUN 1:
Random strings from strings list:  ['Hello', 'Hi', 'Bye']
Bye
Hi
Hi

RUN 2:
Random strings from strings list:  ['Hello', 'Hi', 'Bye']
Bye
Bye
Hello

To understand the above examples, you should have the basic knowledge of the following Python topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.