Bird
Raised Fist0

Which of the following is the correct way to import the random module and use choice to pick a random element from a list items?

easy📝 Syntax Q12 of Q15
Python - Standard Library Usage
Which of the following is the correct way to import the random module and use choice to pick a random element from a list items?
Aimport random; random.choice(items)
Bfrom random import randint; choice(items)
Cimport random.choice; choice(items)
Dimport random; random.randint(items)
Step-by-Step Solution
Solution:
  1. Step 1: Check import syntax

    To use choice, you must import the random module fully or import choice specifically. import random; random.choice(items) imports the module correctly.
  2. Step 2: Verify function usage

    import random; random.choice(items) calls random.choice(items), which is correct. from random import randint; choice(items) imports randint but tries to call choice without import. import random.choice; choice(items) has invalid import syntax. import random; random.randint(items) calls randint with a list, which is incorrect.
  3. Final Answer:

    import random; random.choice(items) -> Option A
  4. Quick Check:

    Correct import and call = import random; random.choice(items) [OK]
Quick Trick: Import random module fully to use choice function [OK]
Common Mistakes:
MISTAKES
  • Importing wrong functions
  • Calling functions without module prefix
  • Using randint instead of choice

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes