Home › Python with Chai › Lesson 13
Modules: the lucky chai
Python with Chai, lesson 13 of 20. A short Hinglish video, simple English notes and code you can run, free.
Open the interactive lesson with Run button
The idea
A module is a ready-made toolbox of code. You don't write it yourself, you import it.
random comes with Python. random.choice(list) picks one item from a list, a different one each run.
random.randint(1, 10) gives a whole number from 1 to 10, and both ends are included.
The code
import random
chai = ["Masala", "Adrak", "Elaichi"]
print("Aaj ki lucky chai:", random.choice(chai))
num = random.randint(1, 10)
print("Lucky number:", num)
Output:
Aaj ki lucky chai: Elaichi Lucky number: 7 (run it again: you get new answers)
Try it yourself
- Add two more flavours to the list and run it 5 times. Did every flavour come up?
- Make a game: if the lucky number is 7, print "Chai free!"
Remember
Don't build every tool yourself: import it.
← Files: read the bill back · f-strings: a neat bill →
All lessons: Python with Chai