5 Obscure Python Syntaxes You Should Try

Advertisements
Photo by Godfrey Nyangechi on Unsplash
my_variable = 1 if <condition> else 2
my_variable = (2, 1)[condition]
print(
"this is a " + \
"really long " + \
"string"
)
print((
"this is a "
"really long "
"string"
))
import operatoroperator.eq(1, 1)
operator.contains('string', 'ing')
True
if 'ing' in 'string'
my_var = 'hello'
print(f'{my_var!r}')
'hello'
my_var = f'{"hello"!r}'
my_list = [1,2,3,4,5,6,7,8]my_list[::2]>>> [1, 3, 5, 7]
my_list = [1,2,3,4,5,6,7,8]my_list[::-1]>>> [8, 7, 6, 5, 4, 3, 2, 1]

Source

Leave a comment

Your email address will not be published. Required fields are marked *