45
PYTHON TRICKS FOR WRITING CLEAN AND EASY READABLE CODE.
TERNARY OPRATORS/CONDITIONALS
Ternary oprators/conditionals evaluate code on True or False. It allows a single line replacing multi-line if and else statements. This basicaily replaces multi-line code but still makes the code understandable.
Re-write using Ternary
Now we have both if/else statement on a single line. Making the code simple and easier to read.
ENUMERATE FUNCTION
This is a built-in python function that eases the task of interating through lists/tuples.
Enumerated objects can be used directly in loops or converted into tuples using list() method. Here is an example:
I am now going to state a cleaner code using Enumerate function
WRITING SIMPLIFIED NUMERIC LITERALS
I will show use to use _ to simplify large numeric literals in code.
In the following example i will use an underscore help read large numeric values and an f string for easy read of its output.
45