You are probably simply running the code by using the green arrow or any equivalent way of doing so. The problem is that this runs the script as a separate process and once it finishes, nothing remains from its environment.
If you want to run like in IDLE, where you execute the script and then can access and modify the environment (variables and functions defined) - you want to execute in console. You have a few ways to do that:
Execute Selection in Console - This will execute any highlighted code in the console:
Note that if no code is highlighted, this option will become Execute Line in Console.
One option below that is the Run File in Console. This will, obviously, run the whole script in the console:

Notice how now you have all the variables defined in the script available in the variables view on the right side.
The last way is to enable this in the Run configurations:
Open the Edit Configurations...:

Then, under Execution, mark the Run with Python console option:

Now you can run the file regularly, by selecting Run File or using the green triangle.
Note: using method (2) will automatically enable the configuration described in method (3). So basically after running the file once in the console (as described in method (2)), you can go back to running the file regularly and all future runs will be in the console as well (until you un-check the box in the run configuration).