Skip to content

Instantly share code, notes, and snippets.

@nomyfan
Last active August 16, 2023 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nomyfan/557487ac990966abb8b3dac3cca3d8ac to your computer and use it in GitHub Desktop.
Save nomyfan/557487ac990966abb8b3dac3cca3d8ac to your computer and use it in GitHub Desktop.
learn-you-a-python
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)
# 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