Skip to content

Instantly share code, notes, and snippets.

View BurhanH's full-sized avatar
:octocat:
Focusing

Baur Urazalinov BurhanH

:octocat:
Focusing
View GitHub Profile
@BurhanH
BurhanH / are_elements_equal.py
Created February 22, 2019 15:49
How to check if all elements in a list are equal
lst = ['a', 'a', 'a']
print(len(set(lst)) == 1)
# True
print(all((x == lst[0]) for x in lst))
# True
print(lst.count(lst[0]) == len(lst))
# True
@BurhanH
BurhanH / lambda_as_async_function.py
Created February 22, 2019 16:04
How to use a lambda expression as an asynchronous function
import asyncio
f = asyncio.coroutine(lambda x: x**2)
asyncio.get_event_loop().run_until_complete(f(12))
# 144
@BurhanH
BurhanH / zip_example.py
Created February 22, 2019 16:25
zip example. many iterations at the same time
# -*- coding: utf-8 -*-
eng = ['one', 'two', 'three']
ger = ['eins', 'zwei', 'drei']
rus = ['один', 'два', 'три']
for e, g, r in zip(eng, ger, rus):
print('{e} = {g} = {r}'.format(e=e, g=g, r=r))
# one = eins = один
@BurhanH
BurhanH / unicode_variables.py
Created February 22, 2019 17:10
Unicode variable names in Python 3
# -*- coding: utf-8 -*-
import math
π = math.pi
print(π)
# 3.141592653589793
τ = math.tau
@BurhanH
BurhanH / make_screenshot.py
Created February 22, 2019 17:24
How to make a screenshot on Python (no Selenium)
# -*- coding: utf-8 -*-
# pip install pyautogui
import pyautogui
screen = pyautogui.screenshot('screenshot.png')
# region_screen = pyautogui.screenshot('region_screenshot.png', region=(0, 0, 300, 400))
@BurhanH
BurhanH / itertools.py
Created February 22, 2019 17:58
itertools examples
# -*- coding: utf-8 -*-
from itertools import permutations, combinations, chain
for p in permutations([1, 2, 3]):
print(p)
# (1, 2, 3)
# (1, 3, 2)
# (2, 1, 3)
@BurhanH
BurhanH / recursion.py
Created February 22, 2019 18:09
recursion examples. Fibonacci sequence and Factorial sequence
# -*- coding: utf-8 -*-
def fib(n):
if n < 2:
return 1
return fib(n-1) + fib(n-2)
print(fib(6))
@BurhanH
BurhanH / concat.py
Created February 22, 2019 18:51
string concatenation examples
# -*- coding: utf-8 -*-
nums = [str(n) for n in range(10)]
print("".join(nums))
# 0123456789
foo = 'foo'
bar = 'bar'
foobar = '{foo}{bar}'.format(foo=foo, bar=bar)
@BurhanH
BurhanH / string_interpolation.py
Created February 25, 2019 21:37
string interpolation, Python 3.6 and above
# -*- coding: utf-8 -*-
name = 'User'
f'Hello, {name}!'
#'Hello, User!'
# most powerfull part is
a = 10
b = 4
f'Ten plus four is {a + b}'
@BurhanH
BurhanH / string_interpolation2.py
Created February 25, 2019 21:49
string interpolation 2, Python 3.6 and above
# -*- coding: utf-8 -*-
def hello(name, question):
return f"Hello, {name},! How is it {question}?"
hello('Stranger', 'going')
#"Hello, Stranger! How is it going?"
# Similar approach
# def hello(name, question):