PYTHON TRICKS FOR WRITING CLEAN AND EASY READABLE CODE.

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 an…


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.

Image description

Re-write using Ternary

Image description

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:

Image description

I am now going to state a cleaner code using Enumerate function

Image description

      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.

Image description


This content originally appeared on DEV Community and was authored by Sam Sonter


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » PYTHON TRICKS FOR WRITING CLEAN AND EASY READABLE CODE.." Sam Sonter | Sciencx - Tuesday November 9, 2021, https://www.scien.cx/2021/11/09/python-tricks-for-writing-clean-and-easy-readable-code/
HARVARD
Sam Sonter | Sciencx Tuesday November 9, 2021 » PYTHON TRICKS FOR WRITING CLEAN AND EASY READABLE CODE.., viewed ,<https://www.scien.cx/2021/11/09/python-tricks-for-writing-clean-and-easy-readable-code/>
VANCOUVER
Sam Sonter | Sciencx - » PYTHON TRICKS FOR WRITING CLEAN AND EASY READABLE CODE.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/09/python-tricks-for-writing-clean-and-easy-readable-code/
CHICAGO
" » PYTHON TRICKS FOR WRITING CLEAN AND EASY READABLE CODE.." Sam Sonter | Sciencx - Accessed . https://www.scien.cx/2021/11/09/python-tricks-for-writing-clean-and-easy-readable-code/
IEEE
" » PYTHON TRICKS FOR WRITING CLEAN AND EASY READABLE CODE.." Sam Sonter | Sciencx [Online]. Available: https://www.scien.cx/2021/11/09/python-tricks-for-writing-clean-and-easy-readable-code/. [Accessed: ]
rf:citation
» PYTHON TRICKS FOR WRITING CLEAN AND EASY READABLE CODE. | Sam Sonter | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.