This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def func(): | |
received = yield 'hello' # received is a generator | |
return received | |
print(list(func())) # prints ['hello'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Method/Attribue | Purpose | |
---|---|---|
split() | Split the string into a list; splitting it whenever the RE matches | |
sub() | Find all substrings where the RE matches and replace them with a different string | |
subn() | Does the same thing as sub() but returns the new string and the number of replacements. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Metacharacters | Description | |
---|---|---|
| | 'or' operator. | |
^ | matches at the beginning of lines. | |
$ | matches at the end of a line. Can be end of a string or any location followed by a new line. | |
\A | matches only at the start of the string. | |
\z | matches only at the end of the string. | |
\b | this is a zero-width assertion that matches only at the beginning or end of a word. | |
\B | this is a zero-width assertion and is opposite of \b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Long Flag | Short Flag | Meaning | |
---|---|---|---|
ASCII | A | Makes several escapes like \w \b \s \d match only on ASCII characters. | |
DOTALL | S | Make . match any character including newlines. | |
IGNORECASE | I | Do case-insensitive matches. | |
LOCALE | L | Do a locale-aware match. | |
MULTILINE | M | Multi-line matching affecting ^ and $. | |
VERBOSE | X (for 'extended') | Enable verbose REs which can be organized more cleanly and understandably. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Regular String | Raw String | |
---|---|---|
'happy*' | r'happy*' | |
'\\new' | r'\new' | |
'\\w+\\s+' | r'\w+\s+' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Characters | Stage | |
---|---|---|
'\new' | Text string to be matched | |
'\\new' | Escaped backslash for `re.compile()` | |
'\\\\new' | Escaped backslashes for a string literal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import Thread | |
from time import perf_counter | |
def sum_up_to(start: int, end: int): | |
total = 0 | |
print('start...') | |
for i in range(start, end): | |
total += i | |
print('end...') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import Thread | |
from time import perf_counter | |
def sum_up_to(start: int, end: int): | |
total = 0 | |
print('start...') | |
for i in range(start, end): | |
total += i | |
print('end...') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
import weakref | |
import gc | |
class Serval: | |
def __init__(self, name, cousin = None): | |
self.name = name | |
self.cousin = cousin | |
class Cat: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import weakref | |
import gc | |
import ctypes | |
class PriceyObject: | |
def __init__(self, name): | |
self.name = name | |
def __repr__(self): |
NewerOlder