import random
print(random.randint(1, 10))
Output
3
Displaying 10 random integers between 0 and 10
import random
for x in range(10):
print(random.randint(0, 10), ', ', end='')
Output
4 , 0 , 3 , 10 , 2 , 6 , 7 , 3 , 4 , 7 ,
from random import randrange
print(randrange(10)) # From 0 to 9
Output
3
print(random.sample(range(1, 100), 10))
import random
print(random.random()) # between 0 and less than 1
Output
0.6919994035378282
random.uniform(1.2, 2.3) # start and stop, both inclusive
Output
1.7852514538737663
We used random.random() and random.uniform for generating random float numbers. The main difference between them are here.
def gen_marks(student_no):
my_list = []
for i in range(student_no):
my_list.append(random.randint(0, 101))
return my_list
class_4 = gen_marks(28) # Marks of class 4 for 28 students
import string
def id_generator(size=6, chars=string.ascii_lowercase):
return ''.join(random.choice(chars) for _ in range(size))
str1 = id_generator()
import string
import random
def id_generator(size=6): # default size is 6
chars = string.ascii_lowercase + string.digits
return ''.join(random.choice(chars) for _ in range(size))
str1 = id_generator(4)
l1 = ['cheking', 'cleaning', 'inspection', 'repair', 'painting']
import random
activity = random.choice(l1)
print(activity)
import random
l1 = ['cheking', 'cleaning', 'inspection', 'repair', 'painting']
activity = [random.choice(l1) for i in range(10)]
print(activity)
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.