5 surprising and lesser-known features of Python

The else Clause on Loops

Yes, loops (for and while) can have an else block — and it only runs if the loop doesn’t exit via break

Functions Can Have Attributes

You can dynamically assign attributes to functions, just like objects.

Underscore _ as a Throwaway Variable or Last Expression

– In the REPL (like python or ipython), _ holds the last result. – You can use _ as a throwaway variable in loops or unpacking:

Chaining Comparisons Naturally

Python allows chaining comparisons in a very readable way.

You Can import Anything — Even Scripts at Runtime

With the importlib module, you can dynamically import files at runtime.

Create Your real AI Images in Seconds!