This content originally appeared on DEV Community and was authored by Sam Sonter
Hello everybody, Today I am going state and explain few python tricks with examples.
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.
This content originally appeared on DEV Community and was authored by Sam Sonter
Sam Sonter | Sciencx (2021-11-09T05:31:57+00:00) PYTHON TRICKS FOR WRITING CLEAN AND EASY READABLE CODE.. Retrieved from https://www.scien.cx/2021/11/09/python-tricks-for-writing-clean-and-easy-readable-code/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.