0
0
Pythonprogramming~5 mins

Importing specific items in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean to import specific items in Python?
It means you bring only certain functions, classes, or variables from a module into your program instead of importing the whole module.
Click to reveal answer
beginner
How do you import only the <code>sqrt</code> function from the <code>math</code> module?
You write: <br><code>from math import sqrt</code><br>This lets you use <code>sqrt()</code> directly without <code>math.</code> prefix.
Click to reveal answer
beginner
What happens if you try to use a function not imported specifically?
You get an error because Python doesn't know about it unless you import it or the whole module.
Click to reveal answer
beginner
Can you import multiple specific items from a module at once? How?
Yes, by listing them separated by commas: <br><code>from module_name import item1, item2</code>
Click to reveal answer
beginner
Why might you prefer importing specific items instead of the whole module?
It keeps your program clean and can save memory by only loading what you need.
Click to reveal answer
Which syntax correctly imports the choice function from the random module?
Aimport choice from random
Bimport random.choice
Cfrom random import choice
Dfrom choice import random
What will happen if you write import math and then call sqrt(9)?
AIt works and returns 3
BError: <code>sqrt</code> is not defined
CImports <code>sqrt</code> automatically
DReturns 9
How do you import both sin and cos from the math module?
Afrom math import sin, cos
Bimport math.sin, math.cos
Cfrom math import sin cos
Dimport sin, cos from math
What is a benefit of importing specific items instead of the whole module?
AFaster program startup and less memory use
BYou get all module features automatically
CYou can use any function without import
DNo difference at all
Which of these is NOT a correct way to import the datetime class from the datetime module?
Afrom datetime import datetime
Bfrom datetime import date
Cimport datetime
Dimport datetime.datetime
Explain how to import specific functions from a Python module and why it might be useful.
Think about how you bring only what you need from a toolbox.
You got /3 concepts.
    Describe what happens if you try to use a function from a module without importing it specifically or importing the whole module.
    Imagine trying to use a tool you never took out of the box.
    You got /3 concepts.