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

Python basic tutorial

Python flow control

Python Functions

Python Data Types

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Python program generates random numbers

Python example大全

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 random numbers in Python, please use the randint() function. This function is inrandom moduleDefinition.

Source code

# 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 range of 0 and9The random number, so we may get different outputs. The syntax of this function is:

random.randint(a,b)

This will return a N containing a range of numbers [a, b], that is, a <= N <= b, and the range includes the endpoints.

Python example大全