Python Learning Series · @onlysamarrr · Day 28 / 30
Day 28 teaches modules. A module is code from another file: import math and import random give you their functions. Never name your own file random.py or math.py, or Python will hide the real module behind yours.
Phase: Intermediate · Reels: 136/450 – 140/450
Day 28 teaches modules. A module is code from another file: import math and import random give you their functions. Never name your own file random.py or math.py, or Python will hide the real module behind yours.
import math, random
print(math.pi > 3, math.sqrt(16))
random.seed(0)
dice = random.randint(1, 6)
print(dice)
otp = "".join(random.choices("0123456789", k=6))
print(len(otp))
print(otp)
if __name__ == "__main__":
print("Only on direct run")
True 4.0 4 6 742547 Only on direct run
Dice games, OTP generators and shuffling all use the random module.
Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.
Q1. What does random.randint(1, 6) return?
Q2. Why not name your file random.py?
Q3. When does if __name__ == "__main__" run?
| 136/450 | Concept | import kaise kaam karta |
| 137/450 | Live demo | random aur math demo |
| 138/450 | Common mistake | File ka naam random.py mat rakho |
| 139/450 | Quiz | Quiz: dice roller |
| 140/450 | Interview / real life | Real life: OTP generator |