English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic Python tutorial

Control flow in Python

Fonctions en Python

Types de données en Python

File operations in Python

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python program generates random numbers

Complete Python examples

In this example, you will learn how to generate random numbers in Python.

To understand this example, you should understand the followingPython programmingTopic:

To generate a random number in Python, please use the randint() function. This function is inIn the random moduleDefinition.

Source code

# The program generates a random number between 0 and9between random numbers
# Import the random module
import random
print(random.randint(0,9))

Output result

5

Please note that since this program generates a random number between 0 and9The random number, so we may get different outputs. The syntax of this function is:

random.randint(a,b)

This will return a number N within the range [a, b], that is, a <= N <= b, including the endpoints of the range.

Complete Python examples