Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python generator cheat sheet download

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age  = age

class Employee(Person):
    def __init__(self, name, age, staff_num):
        super().__init__(name, age)
        self.staff_num = staff_num
Comment

python generator cheat sheet download

class <name>:
    def __init__(self, a):
        self.a = a
    def __repr__(self):
        class_name = self.__class__.__name__
        return f'{class_name}({self.a!r})'
    def __str__(self):
        return str(self.a)

    @classmethod
    def get_class_name(cls):
        return cls.__name__
Comment

python generator cheat sheet download

from functools import wraps

def debug(func):
    @wraps(func)
    def out(*args, **kwargs):
        print(func.__name__)
        return func(*args, **kwargs)
    return out

@debug
def add(x, y):
    return x + y
Comment

python generator cheat sheet download

def get_multiplier(a):
    def out(b):
        return a * b
    return out
Comment

python generator cheat sheet download

>>> multiply_by_3 = get_multiplier(3)
>>> multiply_by_3(10)
30
Comment

python generator cheat sheet download

>>> combinations_with_replacement('abc', 2)  #   a  b  c
[('a', 'a'), ('a', 'b'), ('a', 'c'),         # a x  x  x
 ('b', 'b'), ('b', 'c'),                     # b .  x  x
 ('c', 'c')]                                 # c .  .  x
Comment

python generator cheat sheet download

>>> permutations('abc', 2)                   #   a  b  c
[('a', 'b'), ('a', 'c'),                     # a .  x  x
 ('b', 'a'), ('b', 'c'),                     # b x  .  x
 ('c', 'a'), ('c', 'b')]                     # c x  x  .
Comment

python generator cheat sheet download

>>> counter = count(10, 2)
>>> next(counter), next(counter), next(counter)
(10, 12, 14)
Comment

python generator cheat sheet download

@decorator_name
def function_that_gets_passed_to_decorator():
    ...
Comment

python generator cheat sheet download

<list> = [i+1 for i in range(10)]                   # [1, 2, ..., 10]
<set>  = {i for i in range(10) if i > 5}            # {6, 7, 8, 9}
<iter> = (i+5 for i in range(10))                   # (5, 6, ..., 14)
<dict> = {i: i*2 for i in range(10)}                # {0: 0, 1: 2, ..., 9: 18}
Comment

python generator cheat sheet download

out = [i+j for i in range(10) for j in range(10)]
Comment

python generator cheat sheet download

import re
<str>   = re.sub(<regex>, new, text, count=0)  # Substitutes all occurrences with 'new'.
<list>  = re.findall(<regex>, text)            # Returns all occurrences as strings.
<list>  = re.split(<regex>, text, maxsplit=0)  # Use brackets in regex to include the matches.
<Match> = re.search(<regex>, text)             # Searches for first occurrence of the pattern.
<Match> = re.match(<regex>, text)              # Searches only at the beginning of the text.
<iter>  = re.finditer(<regex>, text)           # Returns all occurrences as match objects.
Comment

python generator cheat sheet download

def count(start, step):
    while True:
        yield start
        start += step
Comment

python generator cheat sheet download

from itertools import count, repeat, cycle, chain, islice
Comment

PREVIOUS NEXT
Code Example
Python :: online python formatter and compiler 
Python :: bebražole 
Python :: finns = False 
Python :: how to stop gambling 
Python :: numpy documentation realpython 
Python :: python calculate variance by hand 
Python :: dataframe groupby rank by multiple column value 
Python :: how to select name parent table in model laravel 
Python :: how to correct spelling in pandas datafeame 
Python :: weight constraints keras cnn 
Python :: how is pythons glob.glob ordered list 
Python :: visual studio code python indent shortcut 
Python :: python censoring pypi 
Python :: concatenate dataframes using one column 
Python :: ios iterate through dictionary 
Python :: Perform a right outer join of self and other. 
Python :: somma array python 
Python :: ole db 
Python :: matplot lib mehrere bilder nebeneinander 
Python :: qlcdnumber set value python 
Python :: How to open hyperlink with target=“_blank” in PyQt5 
Python :: how to i print oin pyhton 
Python :: iterate through keys in dictionary 
Python :: add multiple columns to dataframe if not exist pandas 
Python :: python cows and bulls 
Python :: what is norways politics 
Python :: call a function with prameters inm tkinter buttion 
Python :: merge nouns spacy 
Python :: how tofind records between two values in pyspark 
Python :: python intitialize a 2d matrix 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =