Recall & Review
beginner
What is import aliasing in Python?Import aliasing is when you give a different name to a module or function when you import it. This helps to use shorter or clearer names in your code.Click to reveal answer
beginner
How do you alias a module named
math as m?You write <code>import math as m</code>. Now you can use <code>m.sqrt()</code> instead of <code>math.sqrt()</code>.Click to reveal answer
beginner
Why use import aliasing?It makes code shorter and easier to read. It also helps avoid name conflicts if two modules have the same function names.
Click to reveal answer
intermediate
Show how to alias a function
datetime.datetime as dt.You can write <code>from datetime import datetime as dt</code>. Then use <code>dt.now()</code> to get the current date and time.Click to reveal answer
intermediate
Can you alias multiple imports in one line? Give an example.
Yes. For example: <code>from math import sqrt as sq, factorial as fact</code>. Now use <code>sq(9)</code> and <code>fact(5)</code>.Click to reveal answer
What does this code do? <br>
import numpy as np✗ Incorrect
The code imports the numpy module but allows you to use the short name np instead of numpy.
How do you alias a function
random.randint as rand?✗ Incorrect
You use
from random import randint as rand to alias the function to a new name.Why might you want to alias imports?
✗ Incorrect
Aliasing helps avoid name conflicts and makes code easier to write and read.
Which is the correct syntax to alias the module
pandas as pd?✗ Incorrect
The correct syntax is
import pandas as pd.What happens if you don't alias a long module name?
✗ Incorrect
Without aliasing, you need to type the full module name each time you use it.
Explain what import aliasing is and why it is useful.
Think about how you might give a nickname to a long name to make it easier to use.
You got /4 concepts.
Write an example of aliasing a module and a function in Python.
Use 'import math as m' and 'from datetime import datetime as dt' as examples.
You got /3 concepts.