generate a random number in python

One way to generate a random number in Python is to use the randint() function provided by the built-in random module. This function generates a random integer between two given endpoints (inclusive). Here's an example of how to use randint() to generate a random number between 1 and 10:

main.py
import random

random_number = random.randint(1, 10)

print(f"The randomly generated number is {random_number}")
113 chars
6 lines

Output:

main.py
The randomly generated number is 7
35 chars
2 lines

In this example, we import the random module, then use the randint() function to generate a random number between 1 and 10, inclusive. We assign the generated number to a variable called random_number, and then print out the value of random_number using an f-string.

gistlibby LogSnag