Table of Contents

    Class - XII: SEMESTER – III: Unit – 1: Python Programming: Section 13: Introduction to Python modules

    Python Programming: Introduction to Python Modules

    ১. Python Module কী?

    Python programming language-এ module হলো এমন একটি file বা collection যেখানে pre-written code, functions, variables এবং classes থাকে। সহজভাবে বললে, module হলো ready-made code library, যা ব্যবহার করে programmer নিজের program আরও সহজ, ছোট এবং efficient করতে পারে।

    Python-এ অনেক built-in modules আছে, যেমন math, random, statistics, datetime, os ইত্যাদি। এই modules ব্যবহার করলে আমাদের সব function নিজে থেকে লিখতে হয় না। যেমন square root বের করতে চাইলে নিজে algorithm না লিখে math.sqrt() ব্যবহার করা যায়।

    Module ব্যবহার করার প্রধান উদ্দেশ্য হলো code reuse করা। অর্থাৎ, আগে থেকেই তৈরি করা code আমরা নিজের program-এ ব্যবহার করতে পারি। এতে development time কমে, error কম হয় এবং program বেশি organized হয়।

    Module ব্যবহার করার সুবিধা:

    • Ready-made functions ব্যবহার করা যায়।
    • Code ছোট এবং পরিষ্কার হয়।
    • Complex calculation সহজে করা যায়।
    • Program modular এবং reusable হয়।
    • Time save হয় এবং development faster হয়।
    • Mathematical, random এবং statistical operations সহজ হয়।

    ২. Module Import করা কী?

    কোনো module-এর functions বা variables ব্যবহার করতে হলে প্রথমে সেই module import করতে হয়। Import করার মানে হলো Python interpreter-কে বলা যে আমরা একটি নির্দিষ্ট module-এর features ব্যবহার করতে চাই।

    Python-এ module import করার জন্য সাধারণত import keyword ব্যবহার করা হয়। Module import করার পর আমরা module name-এর মাধ্যমে তার functions access করতে পারি।

    Basic Syntax:

    import module_name

    Example:

    import math
    
    result = math.sqrt(25)
    
    print(result)

    Output:

    5.0

    এখানে math module import করা হয়েছে। এরপর math.sqrt(25) ব্যবহার করে 25-এর square root বের করা হয়েছে।

    ৩. Importing Module using import <module>

    import <module> হলো module import করার সবচেয়ে common পদ্ধতি। এই পদ্ধতিতে পুরো module import হয়। এরপর module-এর কোনো function বা variable ব্যবহার করতে হলে module name লিখে dot operator . ব্যবহার করতে হয়।

    Syntax:

    import module_name
    
    module_name.function_name()

    Example:

    import math
    
    print(math.pi)
    print(math.sqrt(64))
    print(math.ceil(4.3))

    Output:

    3.141592653589793
    8.0
    5

    এখানে math.pi দ্বারা pi-এর value, math.sqrt() দ্বারা square root এবং math.ceil() দ্বারা ceiling value বের করা হয়েছে।

    Important: যদি import math ব্যবহার করা হয়, তাহলে function call করার সময় অবশ্যই math. prefix ব্যবহার করতে হবে।

    ৪. Importing using from Statement

    Python-এ from statement ব্যবহার করে module-এর নির্দিষ্ট function, variable বা constant import করা যায়। এই ক্ষেত্রে পুরো module import করার দরকার নেই। নির্দিষ্ট function import করলে function call করার সময় module name লিখতে হয় না।

    Syntax:

    from module_name import function_name

    Example:

    from math import sqrt
    
    result = sqrt(49)
    
    print(result)

    Output:

    7.0

    এখানে sqrt function সরাসরি import করা হয়েছে। তাই math.sqrt() না লিখে শুধু sqrt() লেখা হয়েছে।

    Multiple Items Import:

    from math import pi, sqrt, ceil
    
    print(pi)
    print(sqrt(81))
    print(ceil(5.2))

    Import All using *

    from math import *
    
    print(pi)
    print(sqrt(36))
    print(floor(7.9))
    Note: from module import * ব্যবহার করলে module-এর সব public names import হয়। তবে বড় program-এ এটি avoid করা ভালো, কারণ কোন function কোথা থেকে এসেছে তা বোঝা কঠিন হতে পারে।

    ৫. import এবং from Statement-এর পার্থক্য

    বিষয় import module from module import function
    Import Type পুরো module import করে নির্দিষ্ট function বা variable import করে
    Function Call Module name সহ call করতে হয় Direct function name দিয়ে call করা যায়
    Example math.sqrt(25) sqrt(25)
    Readability Function কোন module থেকে এসেছে তা পরিষ্কার বোঝা যায় Code ছোট হয়, কিন্তু source কম clear হতে পারে
    Best Use যখন module-এর অনেক function ব্যবহার করতে হয় যখন নির্দিষ্ট কয়েকটি function দরকার

    ৬. math Module

    Python-এর math module mathematical operations করার জন্য ব্যবহৃত হয়। এতে অনেক useful constants এবং functions আছে, যেমন pi, e, sqrt(), ceil(), floor(), pow(), fabs(), sin(), cos(), tan() ইত্যাদি।

    Scientific calculation, geometry, trigonometry, numeric processing এবং mathematical problem solving-এর জন্য math module খুব গুরুত্বপূর্ণ।

    ৭. math.pi এবং math.e

    math.pi হলো pi-এর value, যা সাধারণত circle-related calculation-এ ব্যবহার করা হয়। Pi-এর approximate value হলো 3.14159। অন্যদিকে math.e হলো Euler's number, যার value প্রায় 2.71828।

    Example:

    import math
    
    print("Pi:", math.pi)
    print("Euler number:", math.e)

    Output:

    Pi: 3.141592653589793
    Euler number: 2.718281828459045

    Area of Circle:

    import math
    
    radius = float(input("Circle-এর radius লিখুন: "))
    
    area = math.pi * radius * radius
    
    print("Circle-এর area:", area)

    এখানে circle-এর area formula πr² ব্যবহার করা হয়েছে।

    ৮. math.sqrt()

    sqrt() function কোনো number-এর square root বের করে। এটি floating-point result return করে।

    Example:

    import math
    
    print(math.sqrt(25))
    print(math.sqrt(2))

    Output:

    5.0
    1.4142135623730951

    Practical Example:

    import math
    
    number = float(input("একটি number লিখুন: "))
    
    result = math.sqrt(number)
    
    print("Square root:", result)
    Note: Negative number-এর square root সাধারণ math.sqrt() দিয়ে করলে error হতে পারে, কারণ math module real number-এর উপর কাজ করে।

    ৯. math.ceil() এবং math.floor()

    ceil() function কোনো decimal number-কে উপরের nearest integer-এ round করে। আর floor() function decimal number-কে নিচের nearest integer-এ round করে।

    Function কাজ Example
    ceil() উপরের integer-এ round করে ceil(4.2) → 5
    floor() নিচের integer-এ round করে floor(4.9) → 4

    Example:

    import math
    
    print(math.ceil(4.2))
    print(math.floor(4.9))
    print(math.ceil(7.0))
    print(math.floor(7.0))

    Output:

    5
    4
    7
    7

    ceil() এবং floor() সাধারণত pagination, billing, measurement এবং rounding logic-এ ব্যবহার করা হয়।

    ১০. math.pow()

    pow() function কোনো number-এর power বা exponent বের করতে ব্যবহার করা হয়। যেমন 2-এর power 3 মানে 2 × 2 × 2 = 8।

    Syntax:

    math.pow(base, exponent)

    Example:

    import math
    
    print(math.pow(2, 3))
    print(math.pow(5, 2))

    Output:

    8.0
    25.0

    math.pow() result সাধারণত float আকারে return করে। Python-এ power বের করার জন্য ** operator-ও ব্যবহার করা যায়।

    Using ** operator:

    print(2 ** 3)
    print(5 ** 2)

    ১১. math.fabs()

    fabs() function কোনো number-এর absolute value return করে। Absolute value মানে number-এর positive value। যেমন -10-এর absolute value হলো 10

    Example:

    import math
    
    print(math.fabs(-10))
    print(math.fabs(25))

    Output:

    10.0
    25.0
    Note: Python-এর built-in abs() function-ও absolute value দেয়। তবে math.fabs() সবসময় float result return করে।

    ১২. Trigonometric Functions: sin(), cos(), tan()

    math module-এ trigonometric calculation করার জন্য sin(), cos(), এবং tan() functions আছে। এগুলো angle-এর sine, cosine এবং tangent value return করে।

    Python-এর trigonometric functions angle radians-এ গ্রহণ করে। যদি degree value থাকে, তাহলে আগে degree-কে radians-এ convert করতে হয়। এই কাজের জন্য math.radians() ব্যবহার করা যায়।

    Example using radians:

    import math
    
    angle = math.pi / 2
    
    print(math.sin(angle))
    print(math.cos(angle))
    print(math.tan(angle))

    Degree থেকে Radian:

    import math
    
    degree = 90
    
    radian = math.radians(degree)
    
    print("sin:", math.sin(radian))
    print("cos:", math.cos(radian))
    print("tan:", math.tan(radian))

    Trigonometric functions physics, engineering, graphics, geometry এবং scientific calculation-এ ব্যবহার করা হয়।

    ১৩. math Module Summary Table

    Function / Constant কাজ Example
    pi Pi constant return করে math.pi
    e Euler number return করে math.e
    sqrt() Square root বের করে math.sqrt(25)
    ceil() উপরের nearest integer দেয় math.ceil(4.2)
    floor() নিচের nearest integer দেয় math.floor(4.9)
    pow() Power বের করে math.pow(2, 3)
    fabs() Absolute value float আকারে দেয় math.fabs(-10)
    sin() Sine value বের করে math.sin(math.pi/2)
    cos() Cosine value বের করে math.cos(0)
    tan() Tangent value বের করে math.tan(math.pi/4)

    ১৪. random Module

    Python-এর random module random numbers generate করার জন্য ব্যবহার করা হয়। Random number সাধারণত game development, lottery system, OTP simulation, dice rolling, random selection, testing এবং simulation-এর কাজে ব্যবহৃত হয়।

    এই syllabus অনুযায়ী গুরুত্বপূর্ণ random module functions হলো: random(), randint(), এবং randrange()

    ১৫. random.random()

    random() function 0.0 থেকে 1.0-এর মধ্যে একটি random floating-point number return করে। এখানে 0.0 include হতে পারে, কিন্তু 1.0 সাধারণত include হয় না।

    Example:

    import random
    
    value = random.random()
    
    print(value)

    প্রত্যেকবার program run করলে output আলাদা হতে পারে।

    Random Percentage Example:

    import random
    
    percentage = random.random() * 100
    
    print("Random percentage:", percentage)

    ১৬. random.randint()

    randint() function নির্দিষ্ট range-এর মধ্যে একটি random integer return করে। এতে start এবং end দুই value-ই include হয়।

    Syntax:

    random.randint(start, end)

    Example:

    import random
    
    number = random.randint(1, 10)
    
    print(number)

    এই code 1 থেকে 10-এর মধ্যে একটি random integer print করবে।

    Dice Rolling Example:

    import random
    
    dice = random.randint(1, 6)
    
    print("Dice value:", dice)

    এখানে dice roll simulation করা হয়েছে, কারণ dice-এর possible values 1 থেকে 6।

    ১৭. random.randrange()

    randrange() function নির্দিষ্ট range থেকে random number return করে। এটি range() function-এর মতো কাজ করে। এখানে stop value include হয় না।

    Syntax:

    random.randrange(start, stop, step)

    Example:

    import random
    
    number = random.randrange(1, 10)
    
    print(number)

    এখানে 1 থেকে 9-এর মধ্যে random number আসবে, কারণ 10 include হবে না।

    Even Random Number:

    import random
    
    number = random.randrange(2, 21, 2)
    
    print(number)

    এখানে 2 থেকে 20 পর্যন্ত even numbers-এর মধ্যে random number generate হবে।

    ১৮. random Module Summary Table

    Function কাজ Example
    random() 0.0 থেকে 1.0-এর মধ্যে random float দেয় random.random()
    randint() Start এবং end সহ random integer দেয় random.randint(1, 10)
    randrange() range-এর মতো random value দেয়, stop exclude করে random.randrange(1, 10)

    ১৯. statistics Module

    Python-এর statistics module numerical data-এর statistical calculation করার জন্য ব্যবহার করা হয়। Mean, median, mode-এর মতো basic statistics বের করতে এই module খুব useful।

    Data analysis, marks analysis, survey result, business report, scientific calculation এবং result processing-এ statistics module ব্যবহার করা যায়।

    Import Example:

    import statistics

    ২০. statistics.mean()

    mean() function numeric data-এর average বা গড় return করে। Mean বের করার formula হলো total sum divided by number of values।

    Formula:

    Mean = Sum of values / Number of values

    Example:

    import statistics
    
    marks = [80, 85, 90, 75, 70]
    
    result = statistics.mean(marks)
    
    print("Mean:", result)

    Output:

    Mean: 80

    এখানে marks-এর total হলো 400 এবং total values 5। তাই mean হলো 400 / 5 = 80।

    ২১. statistics.median()

    median() function sorted data-এর middle value return করে। যদি data-এর value সংখ্যা odd হয়, তাহলে middle value median হয়। আর value সংখ্যা even হলে middle দুইটি value-এর average median হয়।

    Odd Number of Values:

    import statistics
    
    numbers = [10, 20, 30, 40, 50]
    
    print(statistics.median(numbers))

    Output:

    30

    Even Number of Values:

    import statistics
    
    numbers = [10, 20, 30, 40]
    
    print(statistics.median(numbers))

    Output:

    25.0

    এখানে middle values হলো 20 এবং 30। তাই median = (20 + 30) / 2 = 25।

    ২২. statistics.mode()

    mode() function data-এর মধ্যে সবচেয়ে বেশি বার আসা value return করে। কোনো value যদি অন্য value-এর তুলনায় বেশি frequency নিয়ে থাকে, সেটিই mode।

    Example:

    import statistics
    
    numbers = [10, 20, 20, 30, 40, 20, 50]
    
    print(statistics.mode(numbers))

    Output:

    20

    এখানে 20 তিনবার এসেছে, তাই mode হলো 20।

    String Data-এর Mode:

    import statistics
    
    colors = ["red", "blue", "red", "green", "red", "blue"]
    
    print(statistics.mode(colors))

    Output:

    red

    ২৩. statistics Module Summary Table

    Function কাজ Example
    mean() Average বা গড় বের করে statistics.mean([10, 20, 30])
    median() Middle value বের করে statistics.median([10, 20, 30])
    mode() Most frequent value বের করে statistics.mode([1, 2, 2, 3])

    ২৪. Complete Practice Program: math, random এবং statistics একসাথে

    নিচের program-এ math, random, এবং statistics module একসাথে ব্যবহার করা হয়েছে। এতে mathematical calculation, random number generation এবং statistical calculation দেখানো হয়েছে।

    import math
    import random
    import statistics
    
    # math module examples
    radius = 5
    area = math.pi * radius * radius
    
    print("Circle area:", area)
    print("Square root of 81:", math.sqrt(81))
    print("Ceil of 4.3:", math.ceil(4.3))
    print("Floor of 4.9:", math.floor(4.9))
    print("2 power 5:", math.pow(2, 5))
    print("Absolute value:", math.fabs(-15))
    
    degree = 90
    radian = math.radians(degree)
    
    print("sin 90:", math.sin(radian))
    print("cos 90:", math.cos(radian))
    print("tan 45:", math.tan(math.radians(45)))
    
    # random module examples
    print("Random float:", random.random())
    print("Random integer 1 to 10:", random.randint(1, 10))
    print("Random even number:", random.randrange(2, 21, 2))
    
    # statistics module examples
    numbers = [10, 20, 20, 30, 40, 50]
    
    print("Mean:", statistics.mean(numbers))
    print("Median:", statistics.median(numbers))
    print("Mode:", statistics.mode(numbers))

    Program Explanation:

    1. math module import করে circle area, square root, ceil, floor, power, absolute value এবং trigonometric values বের করা হয়েছে।
    2. random module import করে random float, random integer এবং random even number generate করা হয়েছে।
    3. statistics module import করে mean, median এবং mode calculate করা হয়েছে।

    ২৫. Complete Practice Program using from Statement

    এবার একই ধরনের কাজ from statement ব্যবহার করে করা হলো। এখানে নির্দিষ্ট functions directly import করা হয়েছে।

    from math import pi, sqrt, ceil, floor, pow, fabs, sin, cos, tan, radians
    from random import random, randint, randrange
    from statistics import mean, median, mode
    
    radius = 7
    
    area = pi * radius * radius
    
    print("Circle area:", area)
    print("Square root:", sqrt(64))
    print("Ceil:", ceil(5.1))
    print("Floor:", floor(5.9))
    print("Power:", pow(3, 2))
    print("Absolute:", fabs(-25))
    
    angle = radians(45)
    
    print("sin 45:", sin(angle))
    print("cos 45:", cos(angle))
    print("tan 45:", tan(angle))
    
    print("Random float:", random())
    print("Random integer:", randint(1, 100))
    print("Random range:", randrange(10, 50, 5))
    
    data = [12, 15, 15, 18, 20, 22]
    
    print("Mean:", mean(data))
    print("Median:", median(data))
    print("Mode:", mode(data))

    এখানে function call করার সময় module name ব্যবহার করা হয়নি, কারণ functions সরাসরি import করা হয়েছে।

    ২৬. Suggested Program ১: Circle Calculation using math Module

    এই program user থেকে radius input নিয়ে circle-এর area এবং circumference calculate করে।

    import math
    
    radius = float(input("Circle-এর radius লিখুন: "))
    
    area = math.pi * radius * radius
    circumference = 2 * math.pi * radius
    
    print("Area:", area)
    print("Circumference:", circumference)

    Explanation:

    1. User থেকে radius input নেওয়া হয়েছে।
    2. Area বের করার formula: πr²
    3. Circumference বের করার formula: 2πr
    4. math.pi ব্যবহার করে accurate pi value নেওয়া হয়েছে।

    ২৭. Suggested Program ২: Random Number Guessing

    এই program random module ব্যবহার করে 1 থেকে 10-এর মধ্যে একটি random number generate করে। User guess করলে program বলে guess correct কিনা।

    import random
    
    secret_number = random.randint(1, 10)
    
    guess = int(input("1 থেকে 10-এর মধ্যে একটি number guess করুন: "))
    
    if guess == secret_number:
        print("Correct guess!")
    else:
        print("Wrong guess!")
        print("Secret number was:", secret_number)

    এখানে random.randint(1, 10) ব্যবহার করে random integer generate করা হয়েছে।

    ২৮. Suggested Program ৩: Marks Analysis using statistics Module

    এই program একটি marks list থেকে mean, median এবং mode বের করে।

    import statistics
    
    marks = [80, 85, 90, 85, 70, 75, 85]
    
    print("Marks:", marks)
    print("Mean:", statistics.mean(marks))
    print("Median:", statistics.median(marks))
    print("Mode:", statistics.mode(marks))

    Explanation:

    1. Marks একটি list-এ store করা হয়েছে।
    2. statistics.mean() দিয়ে average marks বের করা হয়েছে।
    3. statistics.median() দিয়ে middle value বের করা হয়েছে।
    4. statistics.mode() দিয়ে সবচেয়ে বেশি repeated marks বের করা হয়েছে।

    ২৯. Common Mistakes in Python Modules

    • Module import না করে function ব্যবহার করা: আগে module import না করলে NameError হতে পারে।
    • import math করার পর sqrt() directly call করা: import math করলে math.sqrt() লিখতে হবে।
    • from math import sqrt করার পর math.sqrt() লেখা: direct import করলে শুধু sqrt() লেখা যায়।
    • Trigonometric functions-এ degree directly দেওয়া: Python-এর sin(), cos(), tan() radians expect করে।
    • randint() এবং randrange() confuse করা: randint() end value include করে, কিন্তু randrange() stop value exclude করে।
    • statistics.mean() non-numeric data-তে ব্যবহার করা: Mean numeric values-এর জন্য ব্যবহার করা উচিত।

    Wrong Example:

    import math
    
    print(sqrt(25))

    এখানে error হবে, কারণ sqrt() সরাসরি import করা হয়নি।

    Correct Example:

    import math
    
    print(math.sqrt(25))

    Another Correct Example:

    from math import sqrt
    
    print(sqrt(25))

    ৩০. Module Functions Summary

    Module Functions / Constants Purpose
    math pi, e, sqrt(), ceil(), floor(), pow(), fabs(), sin(), cos(), tan() Mathematical এবং trigonometric calculations
    random random(), randint(), randrange() Random number generation
    statistics mean(), median(), mode() Basic statistical calculations

    ৩১. উপসংহার

    Python modules programming-কে সহজ, powerful এবং organized করে তোলে। Module ব্যবহার করলে programmer ready-made functions ব্যবহার করতে পারে, ফলে code ছোট হয় এবং complex কাজ সহজে করা যায়। Python-এর built-in modules যেমন math, random, এবং statistics beginner থেকে advanced — সব level-এর programmer-এর জন্য খুব গুরুত্বপূর্ণ।

    import <module> পদ্ধতিতে পুরো module import করা হয় এবং function call করার সময় module name ব্যবহার করতে হয়। অন্যদিকে from module import function পদ্ধতিতে নির্দিষ্ট function import করা হয় এবং function সরাসরি call করা যায়। দুটো পদ্ধতিরই নিজস্ব সুবিধা আছে; program-এর প্রয়োজন অনুযায়ী সঠিক পদ্ধতি নির্বাচন করা উচিত।

    math module দিয়ে square root, power, absolute value, rounding, pi, e এবং trigonometric calculation করা যায়। random module দিয়ে random numbers generate করা যায়, যা games, simulations এবং testing-এর জন্য useful। statistics module দিয়ে mean, median এবং mode-এর মতো statistical values সহজে calculate করা যায়।

    একজন beginner Python learner-এর জন্য modules শেখা খুব জরুরি, কারণ real-world programming-এ আমরা অনেক সময় built-in modules ব্যবহার করে দ্রুত এবং accurate solution তৈরি করি। Modules ভালোভাবে বুঝলে mathematical programs, random games, marks analysis, data processing এবং scientific calculation অনেক সহজ হয়ে যায়।