β¨ Public API Surface | Python Best Practices β¨
π Guidelines and recommendations for using public and non-public names in your Python code.
π·οΈ #Python
π Guidelines and recommendations for using public and non-public names in your Python code.
π·οΈ #Python
β¨ Project Layout | Python Best Practices β¨
π Guidelines and best practices for structuring and organizing your Python projects effectively.
π·οΈ #Python
π Guidelines and best practices for structuring and organizing your Python projects effectively.
π·οΈ #Python
A generator in Python is a function that returns an iterator object instead of a single result.
It differs from regular functions in that it uses the
Example:
https://t.me/DataScience4β€οΈ
It differs from regular functions in that it uses the
yield keyword instead of return. Each call to next(generator) produces the next value from the sequence.Example:
def multiple_generator(x, n):
for i in range(1, n + 1):
yield x * i
multiples_of_5 = multiple_generator(5, 3)
print(next(multiples_of_5)) # 5
print(next(multiples_of_5)) # 10
print(next(multiples_of_5)) # 15
https://t.me/DataScience4
Please open Telegram to view this post
VIEW IN TELEGRAM
β€5π1
β¨ Optimization | Python Best Practices β¨
π Guidelines and best practices for optimizing your Python code.
π·οΈ #Python
π Guidelines and best practices for optimizing your Python code.
π·οΈ #Python
β¨ Constants | Python Best Practices β¨
π Guidelines and best practices for using constants in your Python code.
π·οΈ #Python
π Guidelines and best practices for using constants in your Python code.
π·οΈ #Python
β¨ How to Integrate ChatGPT's API With Python Projects β¨
π Learn how to use the ChatGPT Python API with the OpenAI library to build AI-powered features in your Python applications.
π·οΈ #intermediate #ai #api
π Learn how to use the ChatGPT Python API with the OpenAI library to build AI-powered features in your Python applications.
π·οΈ #intermediate #ai #api
β¨ Dependency Management | Python Best Practices β¨
π Guidelines and best practices for dependency management in Python.
π·οΈ #Python
π Guidelines and best practices for dependency management in Python.
π·οΈ #Python
β¨ Python's deque: Implement Efficient Queues and Stacks β¨
π Use a Python deque to efficiently append and pop elements from both ends of a sequence, build queues and stacks, and set maxlen for history buffers.
π·οΈ #intermediate #datastructures #python #stdlib
π Use a Python deque to efficiently append and pop elements from both ends of a sequence, build queues and stacks, and set maxlen for history buffers.
π·οΈ #intermediate #datastructures #python #stdlib
β€1
β¨ Reference: Pythonβs Built-in Exceptions β¨
π Predefined error classes that the Python interpreter uses to handle various error conditions.
π·οΈ #47_terms
π Predefined error classes that the Python interpreter uses to handle various error conditions.
π·οΈ #47_terms
β¨ Reference: Python Best Practices β¨
π Widely accepted and established guidelines, conventions, tips, and best practices for Python programmers.
π·οΈ #23_terms
π Widely accepted and established guidelines, conventions, tips, and best practices for Python programmers.
π·οΈ #23_terms
Python tip:
You can make dataclass fields immutable by setting frozen=True.
In this case, fields cannot be mutated after the instance is created.
Example belowπ
π @codeprogrammer
You can make dataclass fields immutable by setting frozen=True.
In this case, fields cannot be mutated after the instance is created.
Example below
from dataclasses import dataclass
@dataclass(frozen=True)
class Color:
name: str
hex_value: str
color = Color("red", "#FF0000")
# color.name = "blue" # will raise FrozenInstanceError
Please open Telegram to view this post
VIEW IN TELEGRAM
β€4π1
β¨ assertion | Python Glossary β¨
π A debugging aid that tests a condition as an internal self-check.
π·οΈ #Python
π A debugging aid that tests a condition as an internal self-check.
π·οΈ #Python
β€1
β¨ dataframe | Python Glossary β¨
π A data structure for working with tabular data in Python.
π·οΈ #Python
π A data structure for working with tabular data in Python.
π·οΈ #Python
β€2
Accelerating the Sieve of Eratosthenes
1. Quickly recall the algorithm
Classic implementation:
Time β O(N log log N). We're not interested in the asymptotics, but in how much we can speed up the implementation itself.
2. Optimization #1 β don't bother with even numbers
The idea is simple:
* all even numbers except 2 are composite
* if we only work with odd numbers, we reduce the array size and the number of iterations by about half
Implementation:
3. Optimization #2 β use bytearray instead of list[bool]
Thought:
* bool in Python is an object
* bytearray is a tightly packed buffer
* less overhead and better fits into the CPU cache
Example:
4. Optimization #3 β a hybrid of the two approaches
5. Time comparison
Test with
Conclusions:
* skipping even numbers (#1) gives ~2.6Γ speedup
* bytearray itself doesn't speed up β it's more about memory
* the hybrid (#3) gives ~22.6Γ speedup
Key trick in #3:
There's no Python loop here β everything is done by a C-level operation on the slice. On such tasks, this makes a huge difference.
General idea: in Python, we often speed up not the asymptotics, but the memory model and the number of passes over the data. Loops + memory β the main factors.
π @DataScience4
1. Quickly recall the algorithm
Classic implementation:
def eratosthenes(n):
is_prime = [True] * (n + 1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(n ** 0.5) + 1):
if is_prime[i]:
for j in range(i * i, n + 1, i):
is_prime[j] = False
return is_prime
Time β O(N log log N). We're not interested in the asymptotics, but in how much we can speed up the implementation itself.
2. Optimization #1 β don't bother with even numbers
The idea is simple:
* all even numbers except 2 are composite
* if we only work with odd numbers, we reduce the array size and the number of iterations by about half
Implementation:
def eratosthenes_odd(n):
if n < 2:
return []
size = (n + 1) // 2
is_prime = [True] * size
is_prime[0] = False
limit = int(n ** 0.5) // 2
for i in range(1, limit + 1):
if is_prime[i]:
p = 2 * i + 1
start = (p * p) // 2
for j in range(start, size, p):
is_prime[j] = False
return is_prime
3. Optimization #2 β use bytearray instead of list[bool]
Thought:
* bool in Python is an object
* bytearray is a tightly packed buffer
* less overhead and better fits into the CPU cache
Example:
def eratosthenes_bytearray(n):
is_prime = bytearray(b"\x01") * (n + 1)
is_prime[0:2] = b"\x00\x00"
for i in range(2, int(n ** 0.5) + 1):
if is_prime[i]:
for j in range(i * i, n + 1, i):
is_prime[j] = 0
return is_prime
4. Optimization #3 β a hybrid of the two approaches
def eratosthenes_fast(n):
if n < 2:
return []
size = (n + 1) // 2
is_prime = bytearray(b"\x01") * size
is_prime[0] = 0
limit = int(n ** 0.5) // 2
for i in range(1, limit + 1):
if is_prime[i]:
p = 2 * i + 1
start = (p * p) // 2
is_prime[start::p] = b"\x00" * (((size - start - 1) // p) + 1)
return is_prime
5. Time comparison
Test with
n = 10_000_000:>>> eratosthenes.py
real 0.634s
>>> eratosthenes_odd.py
real 0.245s
>>> eratosthenes_bytearray.py
real 0.801s
>>> eratosthenes_fast.py
real 0.028sConclusions:
* skipping even numbers (#1) gives ~2.6Γ speedup
* bytearray itself doesn't speed up β it's more about memory
* the hybrid (#3) gives ~22.6Γ speedup
Key trick in #3:
is_prime[start::p] = b"\x00" * (((size - start - 1) // p) + 1)
There's no Python loop here β everything is done by a C-level operation on the slice. On such tasks, this makes a huge difference.
General idea: in Python, we often speed up not the asymptotics, but the memory model and the number of passes over the data. Loops + memory β the main factors.
Please open Telegram to view this post
VIEW IN TELEGRAM
β€5
Python Cheat sheet
#python #oop #interview #coding #programming #datastructures
https://t.me/DataScience4
#python #oop #interview #coding #programming #datastructures
https://t.me/DataScience4
β€3π1