← Back to Learn
🎲

Intermediate â€ĸ Python

Modules & Random Fun

Lesson 1 of 6
Step 1

đŸ“Ļ What is a Module?

A module is a toolbox full of code that other programmers already wrote for us. We use the word import to open the toolbox and use its tools. After importing, we write the module name, a dot, and the tool name, like math.sqrt().

🐍 Python
import math

print(math.pi)
print(math.sqrt(25))
â–ļ Output
3.141592653589793
5.0
⭐ Earn 10 XP

📚 Everything in Modules & Random Fun

Discover Python modules! Use import to borrow ready-made tools, roll dice with random, pick surprises, shuffle lists, and do clever math with the math module. Here is every step in this topic in one place, so you can review what you learned or jump straight to a step.

  1. đŸ“Ļ What is a Module?

    A module is a toolbox full of code that other programmers already wrote for us. We use the word import to open the toolbox and use its tools. After importing, we write the module name, a dot, and the tool name, like math.sqrt().

    import math
    
    print(math.pi)
    print(math.sqrt(25))
  2. 🎲 Random Numbers with randint

    The random module helps Python make surprises! random.randint(a, b) picks a whole number between a and b, and both a and b can be chosen. Every time you run the program, you might get a different number.

    import random
    
    number = random.randint(1, 10)
    print("My secret number is", number)
  3. đŸŽ¯ Picking with random.choice

    random.choice() picks one item from a list, just like pulling a name out of a hat. It is great for choosing a random color, snack, or game move. The list stays the same; Python just picks one item.

    import random
    
    colors = ["red", "blue", "green", "yellow"]
    favorite = random.choice(colors)
    print("Today's color is", favorite)
  4. 🔀 Mixing Things with random.shuffle

    random.shuffle() mixes up the order of a list, like shuffling a deck of cards. It changes the list itself, so we print the list after shuffling it. This is useful for games and quizzes.

    import random
    
    cards = ["A", "B", "C", "D", "E"]
    random.shuffle(cards)
    print(cards)
  5. 🧮 Math Module Magic

    The math module has handy tools like math.sqrt() for square roots and math.pi for the number pi. The round() function is built into Python, so it does not need importing. round(number, 2) keeps just 2 digits after the decimal point.

    import math
    
    print(math.sqrt(81))
    print(round(math.pi, 2))
    
    radius = 5
    area = math.pi * radius * radius
    print("Circle area:", round(area, 1))
  6. 🔮 Dice Roller and Fortune Teller

    Now let's mix our tools together! We can roll two dice with randint and add them up. Then we can use choice to make a fortune teller that gives a random message.

    import random
    
    die1 = random.randint(1, 6)
    die2 = random.randint(1, 6)
    print("You rolled", die1, "and", die2)
    print("Total:", die1 + die2)
    
    fortunes = ["You will find a treasure!", "A friend will make you laugh!", "Today is your lucky day!"]
    print("Fortune:", random.choice(fortunes))