'How to organize Python code into collapsable / expandable chunks?

In Pycharm, there's "code structurure" side bar which provides a tree to navigate through the code, but, it is only useful when the code has classes and methods and objects. If nothing of that is in code then it is useless. My question is: is there any way in which I dictate that this is a block, and I want to be able to collapse it and expand it? Something similar to Jupyter where the code is inherently divided to cells.

Currently, I'm doing this:

# ---------------------------------- chunck x blah blah -----------------------

EDIT: Most of comments say that I'm dumb and I don't know how to code efficiently and that I should use functions and classes. Guys, I know how to use those, that's not my question. Thanks.



Solution 1:[1]

Turns out that the answer is very simple: Select the code, right click, do custom folding

enter image description here

Solution 2:[2]

You can select a region, and press ctr+alt+t, then select <editor-fold...>. This will surround the region with a comment that makes the region collapsible. You can also do this manually by adding the following around the region:

# <editor-fold desc="This text is shown when collapsed">

# </editor-fold>

Solution 3:[3]

I sometimes use True conditional statements to create collapsible blocks in PyCharm and other IDEs. This also helps me to visually relate all indented code, access it when needed, and collapse it, when I'm focusing on other parts of my code.

if True:
   # block code goes here

A fancier way is to use a descriptive string in the condition. The description stays visible for a collapsed block. You can also disable these with negation anytime, if needed.

if 'Define similarities':
    Dot = lambda x, y: x @ y
    CosSim = lambda x, y: x @ y / (x @ x)**0.5 / (y @ y)**0.5

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Deft
Solution 2
Solution 3