Cool Python tricks you are not using, but you should.
Nice little tricks in Python: Ternary operator, Short Circuit, Comprehension, etc. is same as is same as is same as instead of is same as To fetch the last element of any iterable like list, tuple or set. More tricks will be added soon. The cover picture is copyrighted to the author. Cool Python tricks you are not using, but you should.
Ternary operator
=
=
=
Short circuit
= or 15
=
= 15
=
Comparison
Reverse an iterable
# => [4, 3, 2, 1]
Unpacking
=
* , =
,
=
=
=
swapping two variables
= ,
, # this is tuple unpacking
Last element of an iterable
For list/tuple
>>> =
>>>
>>> * , =
# if you do not want to use the non-last elements
>>>
>>> =
>>>
For set
>>> =
>>>
: is not
>>> , , =
>>>
>>> * , =
>>>
>>>
# q is not a set
Code formatting
-l
: I use 120 as the maximum characters I accept in a line-t py38
: Python 3.8 specific custom formatting. Related Articles