Skip to content
Worked Examples · Example 7.5

Q.Example 7.5:
from random import random
random() #Function called without the module name

Odisha ChseTextbookSubjective· 2mImportance★★★★★est
15% · 6/40 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Example 7.5 shows the from module import name form: after from random import random, the function is called as plain random() — no module prefix — and returns a random float in [0.0, 1.0).

The idea — module attribute access. There are two ways to use something from a module:

  • import random → call with the qualified name random.random();
  • from random import random → the function object itself is bound into the current namespace, so you call it directly as random().
>>> from random import random
>>> random()      # function called without the module name
0.539221040537251

Expected output. A float such as 0.539221040537251 — the actual digits change on every call because the value is (pseudo-)random; what is guaranteed is the range 0.0 <= value < 1.0 (1.0 itself can never appear).

Why it works

  • from random import random reaches into the random module and binds its attribute random (a function) to the name random in this session. …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.