Python Modules: Importing and Creating Your Own

#DAY 21/ 50DaysChallenge #Python Modules 🔥 Module: A module is a file that contains Python code (functions, variables, classes) which we can reuse in another program. 👉 Python has built-in modules. Example: math random datetime ✅ Import a Module: Code import math print(math.sqrt(16)) Output 4.0 👉 sqrt() means square root. ✅ Using math module: Code import math print(math.pow(2,3)) print(math.factorial(5)) Output 8.0 120 ✅ Import specific function: Instead of importing full module. Code from math import sqrt print(sqrt(25)) Output 5.0 ✅random Module: Used to generate random numbers. Code import random print(random.randint(1,10)) Output Example: 7 (Random value each time) ✅ Create Your Own Module: #Step 1: Create file mymodule.py def greet(name):    print("Hello", name) #Step 2: Use it in another file import mymodule mymodule.greet("Durga") Output Hello Durga 🎯 Task : 👉 Generate a random number between 1 and 100 ✅ Code import random num = random.randint(1,100) print("Random number:", num) Output Random number: 57 #50dayschallenge #coding #pythonbasics #ECE #BVCEC

To view or add a comment, sign in

Explore content categories