Last active
August 16, 2023 08:52
-
-
Save nomyfan/557487ac990966abb8b3dac3cca3d8ac to your computer and use it in GitHub Desktop.
learn-you-a-python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
print("List comprehensions") | |
print([x**2 for x in range(10) if x % 2 == 0]) | |
print("Nested list comprehensions") | |
matrix = [ | |
[1, 2, 3, 4], | |
[5, 6, 7, 8], | |
[9, 10, 11, 12], | |
] | |
# Matrix transposition using nested list comprehensions | |
print([[row[i] for row in matrix] for i in range(4)] | |
) | |
print("Dictionary comprehensions") | |
print({x: x**2 for x in range(5)}) | |
# numpy | |
print(np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])) | |
# Generator & iterator | |
def generate(count = 5): | |
for i in range(count): | |
yield i | |
print("Normal generator traversal") | |
it = generate(2) | |
while True: | |
try: | |
value = next(it) | |
print(value) | |
except StopIteration as e: | |
break | |
print("With iterator") | |
for x in generate(2): | |
print(x) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
numpy==1.25.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Setup the virtual environment | |
python3 -m venv . | |
source bin/activate | |
# For Windows(assume you are using PowerShell) | |
# . ./bin/Activate.ps1 | |
# Config PyPi mirror for mainland China | |
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple | |
# Install packages | |
pip install -r requirements.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment