Python Program to Generate a Random Number

Here’s an example Python code to generate a random number:

# Import the random module
import random

# Generate a random number between 1 and 10
random_number = random.randint(1, 10)

# Print the random number
print("The random number is:", random_number)

In this code, we first import the random module, which provides several functions for generating random numbers.

We then use the randint() function to generate a random integer between 1 and 10 (inclusive) and assign it to the variable random_number.

Finally, we use the print() function to output the random number.

When you run this code, it should output a random number between 1 and 10. For example:

The random number is: 7

Note that each time you run this code, it will generate a different random number between 1 and 10. If you want to generate a random number within a different range, you can simply modify the arguments to the randint() function.

Related Examples

1. Python Program to Print Hello world!

2. Calculate the multiplication and sum of two numbers

3. Python Program to Find the Square Root

4. Python Program to Calculate the Area of a Triangle

5. Python Program to Solve Quadratic Equation

6. Python Program to Swap Two Variables

7. Return the count of a given substring from a string

8. Print the following pattern

9. Python Program to check if the given number is a palindrome number

10. Python Program to print multiplication table form 1 to 10

11. Print downward Half-Pyramid Pattern with Star (asterisk)

12. Python Program to Convert Kilometers to Miles

13. Python Program to Check if a Number is Positive, Negative or 0

14. Python Program to Find the Sum of Natural Numbers

15. Print the sum of the current number and the previous number in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial