Skip to content

Instantly share code, notes, and snippets.

View mportesdev's full-sized avatar

Michal Porteš mportesdev

View GitHub Profile
@mportesdev
mportesdev / fizzbuzz_oneliner.rst
Last active August 14, 2020 11:17
FizzBuzz oneliner in Python

FizzBuzz oneliner in Python

>>> fizz_buzz = ['Fizz'*(i % 3 == 0) + 'Buzz'*(i % 5 == 0) or str(i) for i in range(1, 10_001)]

>>> print('\n'.join(fizz_buzz[:30]))
1
2
@mportesdev
mportesdev / python_source_import.rst
Last active December 11, 2023 03:29
Importing Python source code from a script without the .py extension

Importing Python source code from a script without the .py extension

Generally, to import a python module programatically when you know the file's path, you could do something like this:

import importlib.util
import sys
@mportesdev
mportesdev / gotchas.rst
Last active October 31, 2020 18:18
Little Python gotchas

Little Python gotchas

The last n elements

@mportesdev
mportesdev / pythagorean_triples.md
Last active June 29, 2020 12:51
Pythagorean triples

Find Pythagorean triples (x, y, z) such that x**2 + y**2 == z**2 and x + y + z == n.

import math
from itertools import combinations


def triples(n):
    for x, y in combinations(range(1, n), 2):
 z = n - x - y
@mportesdev
mportesdev / python-3.8-assignment-expressions.md
Last active September 11, 2020 07:47
Python 3.8 assignment expressions – first encounter

Python 3.8 vs PEP 572

According to PEP 572, section Scope of the target, the following examples of assignment expressions should be invalid and raise TargetScopeError.

Example 1

[i := i+1 for i in range(5)]

Python 3.8.0a1 (tags/v3.8.0a1:e75eeb00b5, Feb  3 2019, 20:47:39) [MSC v.1916 64 bit (AMD64)] on win32