Python has really useful features that make writing code more efficient than ever.

Here are 5 tips for writing better and more compact Python code.

🧵🧵🧵👇👇👇
1. Generator

A generator is a special type of function which does not return a single value, instead it returns an iterator object with a sequence of values.

https://www.tutorialsteacher.com/python/python-generator
2. Decorator

A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure.

https://book.pythontips.com/en/latest/decorators.html
3. Ternary operator

A Ternary operator allows you to quickly test a condition instead of a multiline if statement.

https://book.pythontips.com/en/latest/ternary_operators.html
4. Setattr and getattr

The setattr function sets the value of the specified attribute of the specified object. The getattr method returns the value of the named attribute of an object.
5. Enumerate

Enumerate adds a counter to an iterable and returns it in a form of enumerate object.

https://www.geeksforgeeks.org/enumerate-in-python/
You've reached the end of this 🧵

Please RT and ❤️ it if you found it helpful, so you can share it with others.
You can follow @dannysteenman.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.