⚡0 XP🔥0

Ch 10 · Python Modules | पायथन मॉड्यूल

Lesson 10 of 11 ⏱ 3 min read ❓ 5-question quiz ▶ Runs Python

Importing modules - import and from-import; math, random and statistics modules with commonly used functions.

Importing#

import math                 # use as math.sqrt(16)
from random import randint  # use as randint(1, 6)
import statistics as st     # alias

math module#

Function Example
math.sqrt(x) sqrt(25) → 5.0
math.pow(x, y) pow(2, 3) → 8.0
math.ceil(x), math.floor(x) ceil(4.2) → 5, floor(4.8) → 4
math.fabs(x) absolute value as float
math.factorial(n) factorial(5) → 120
math.gcd(a, b) gcd(12, 18) → 6
math.pi, math.e constants
math.sin(x), cos, tan x in radians

random module#

random.random() → float in [0, 1); random.randint(a, b) → integer a ≤ n ≤ b (both included); random.randrange(start, stop, step) (stop excluded); random.choice(seq); random.shuffle(list).

statistics module#

mean(), median(), mode().

▶ Try it YourselfPython
Ctrl + Enter to run
हिंदी में समझें

मॉड्यूल तैयार फ़ंक्शनों का संग्रह है जिसे import से उपयोग करते हैं। math में sqrt, ceil, floor, factorial; random में randint (दोनों सीमाएँ शामिल) और randrange (अंतिम सीमा शामिल नहीं); statistics में mean, median और mode मिलते हैं।

Exam tips#

  • Possible outputs of random-based code: list which values randint/randrange can produce.

Quick Quiz

Q1.

math.ceil(4.2) returns

Q2.

random.randint(1, 6) can return

Q3.

statistics.median([3, 1, 2]) is

Q4.

math.floor(-4.1) is

Q5.

random.randrange(1, 10, 3) can return