Difference between revisions of "User:Praveer-mathur-9f3d@uni-bonn.de/python-on-hpc"
Jump to navigation
Jump to search
| (11 intermediate revisions by the same user not shown) | |||
| Line 6: | Line 6: | ||
<quiz display=simple> | <quiz display=simple> | ||
{ | { | ||
| − | |type=" | + | |type="[]"} |
+ Anything that is in $PYTHONPATH | + Anything that is in $PYTHONPATH | ||
|| Explanation: Correct | || Explanation: Correct | ||
| Line 15: | Line 15: | ||
+ The current working directory | + The current working directory | ||
|| Explanation: Correct | || Explanation: Correct | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | === Short Quiz: Virtual Environments === <!--T:5--> | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = Which of the following statements is true? | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="[]"} | ||
| + | + venv comes with Python | ||
| + | || Explanation: Correct | ||
| + | - Virtual environments always go to ~/.virtualenvs | ||
| + | || Explanation: Wrong | ||
| + | - Each virtual env always has its own Python instance | ||
| + | || Explanation: Wrong | ||
| + | + The activate script has to be source-d to activate the venv | ||
| + | || Explanation: Correct | ||
| + | - The deactivate script has to be source-d to deactivate the venv | ||
| + | || Explanation: Wrong | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | === Short Quiz: Conda vs. Pip === <!--T:5--> | ||
| + | To which package manager do the statements apply? pip, conda, neither or both? | ||
| + | {{hidden begin | ||
| + | |title = "Non-Python packages available by default" | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | - Pip | ||
| + | || Explanation: Wrong | ||
| + | - Conda | ||
| + | || Explanation: Wrong | ||
| + | + Both of them | ||
| + | || Explanation: Conda better, but arguably both. | ||
| + | - Neither of them | ||
| + | || Explanation: Wrong | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = "Tightly integrated with its own virtual environment feature" | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | - Pip | ||
| + | || Explanation: Wrong | ||
| + | + Conda | ||
| + | || Explanation: Correct | ||
| + | - Both of them | ||
| + | || Explanation: Wrong | ||
| + | - Neither of them | ||
| + | || Explanation: Wrong | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = "Built-in third-party package repositories" | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | - Pip | ||
| + | || Explanation: Wrong | ||
| + | + Conda | ||
| + | || Explanation: Correct | ||
| + | - Both of them | ||
| + | || Explanation: Wrong | ||
| + | - Neither of them | ||
| + | || Explanation: Wrong | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = "Installs new Python executable into env by default" | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | - Pip | ||
| + | || Explanation: Wrong | ||
| + | - Conda | ||
| + | || Explanation: Wrong | ||
| + | - Both of them | ||
| + | || Explanation: Wrong | ||
| + | + Neither of them | ||
| + | || Explanation: Correct | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = "Automatically installs dependencies for requested package" | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | - Pip | ||
| + | || Explanation: Wrong | ||
| + | - Conda | ||
| + | || Explanation: Wrong | ||
| + | + Both of them | ||
| + | || Explanation: Correct | ||
| + | - Neither of them | ||
| + | || Explanation: Wrong | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = "Requires you to specify package version" | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | - Pip | ||
| + | || Explanation: Wrong | ||
| + | - Conda | ||
| + | || Explanation: Wrong | ||
| + | - Both of them | ||
| + | || Explanation: Wrong | ||
| + | + Neither of them | ||
| + | || Explanation: Correct | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | === Short Quiz: Arrays and Performance === <!--T:5--> | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = What should you always do before you optimize your code's performance? (Hard to implement). | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || Measure your code's performance! | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | === Short Quiz: Array Memory Layout === <!--T:5--> | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = Which index varies fastest in a row-major layout, which varies fastest in a | ||
| + | column-major layout? (Hard to implement). | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || Row-major: last index, column-major: first index | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = Which ordering do NumPy arrays have by default? More than one answer | ||
| + | may be correct. | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="[]"} | ||
| + | + C order | ||
| + | || Explanation: Correct | ||
| + | - Fortran order | ||
| + | || Explanation: Wrong | ||
| + | - Column-major | ||
| + | || Explanation: Wrong | ||
| + | + Row-major | ||
| + | || Explanation: Correct | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | === Short Quiz: Array Shapes, Broadcasting === <!--T:5--> | ||
| + | |||
| + | Consider the following code. What does each print statement output? Could be an error. Treat all print statements independently. (Hard to implement) | ||
| + | |||
| + | <code>x = np.zeros(5) <br> y = np.zeros((3, 5)) <br> z = np.zeros((5, 3, 1))</code> | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = <code>print((x + y).shape)</code> | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || <code>(3, 5)</code> | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = <code>print((x + z).shape)</code> | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || <code>(5, 3, 5)</code> | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = <code>print((y + z).shape)</code> | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || <code>(5, 3, 5)</code> | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = <code>print(y[:2, :2].shape)</code> | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || <code>(2, 2)</code> | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = <code>print(y[:2].shape)</code> | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || <code>(2, 5)</code> | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = <code>print((y[:2] + x[:2]).shape)</code> | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || <code>ValueError: [...] shapes (2, 5) (2,)</code> | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = <code>print((y[..., np.newaxis] + z).shape)</code> | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || <code>ValueError: [...] shapes (3, 5, 1) (5, 3, 1)</code> | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = <code>print((y.ravel() + x[0]).shape)</code> | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || <code>(15,)</code> | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | === Short Quiz: Advanced Indexing === <!--T:5--> | ||
| + | |||
| + | Consider the following code. What does each print statement output? Could be an error. Treat all print statements independently. (Hard to implement) | ||
| + | |||
| + | <code>a = np.array([[1, 2, 3] [4, 5, 6]]) <br> idx = np.array([0, 0]) <br> mask = np.array([False, True, True])</code> | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = <code>print(a[idx])</code> | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || <code>[[1, 2, 3], [1, 2, 3]]</code> | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = <code>print(a[:, idx])</code> | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || <code>[[1, 1], [4, 4]]</code> | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = <code>print(a[idx, :])</code> | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || <code>[[1, 2, 3], [1, 2, 3]]</code> | ||
| + | </quiz> | ||
| + | {{hidden end}} | ||
| + | |||
| + | {{hidden begin | ||
| + | |title = <code>a[:, mask] = -1 <br> print(a)</code> | ||
| + | }} | ||
| + | <quiz display=simple> | ||
| + | { | ||
| + | |type="()"} | ||
| + | + Click and submit to see the answer | ||
| + | || <code>[[1, -1, -1], [4, -1, -1]]</code> | ||
</quiz> | </quiz> | ||
{{hidden end}} | {{hidden end}} | ||
Latest revision as of 15:07, 18 March 2026
Short Quiz: Python Packages
Which of these locations will Python search for packages by default?
Short Quiz: Virtual Environments
Which of the following statements is true?
Short Quiz: Conda vs. Pip
To which package manager do the statements apply? pip, conda, neither or both?
"Non-Python packages available by default"
"Tightly integrated with its own virtual environment feature"
"Built-in third-party package repositories"
"Installs new Python executable into env by default"
"Automatically installs dependencies for requested package"
"Requires you to specify package version"
Short Quiz: Arrays and Performance
What should you always do before you optimize your code's performance? (Hard to implement).
Short Quiz: Array Memory Layout
Which index varies fastest in a row-major layout, which varies fastest in a
column-major layout? (Hard to implement).
Which ordering do NumPy arrays have by default? More than one answer
may be correct.
Short Quiz: Array Shapes, Broadcasting
Consider the following code. What does each print statement output? Could be an error. Treat all print statements independently. (Hard to implement)
x = np.zeros(5)
y = np.zeros((3, 5))
z = np.zeros((5, 3, 1))
print((x + y).shape)
print((x + z).shape)
print((y + z).shape)
print(y[:2, :2].shape)
print(y[:2].shape)
print((y[:2] + x[:2]).shape)
print((y[..., np.newaxis] + z).shape)
print((y.ravel() + x[0]).shape)
Short Quiz: Advanced Indexing
Consider the following code. What does each print statement output? Could be an error. Treat all print statements independently. (Hard to implement)
a = np.array([[1, 2, 3] [4, 5, 6]])
idx = np.array([0, 0])
mask = np.array([False, True, True])
print(a[idx])
print(a[:, idx])
print(a[idx, :])
a[:, mask] = -1
print(a)