0
0
Pythonprogramming~5 mins

Import aliasing in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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
AImports np module
BImports numpy and calls it np in the code
CCreates a new module named np
DRenames numpy to numpy
How do you alias a function random.randint as rand?
Afrom random import randint as rand
Bfrom random import randint
Cfrom random import randint as randint
Dimport random as randint
Why might you want to alias imports?
ATo make code longer
BTo delete the original module
CTo avoid name conflicts and shorten code
DTo change module functionality
Which is the correct syntax to alias the module pandas as pd?
Aimport pandas as pd
Bfrom pandas import pd
Cimport pd as pandas
Dalias pandas as pd
What happens if you don't alias a long module name?
ACode will not run
BThe module will be renamed
CPython will alias it automatically
DYou must type the full module name every time
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.