title: Clearing user-created variables in Python
body: <p>I am an absolute beginner in Python. I have been using R and Matlab for data analysis for quite some time. One of the great things about these languages (or tools) is that I could run <code>clear all</code> in Matlab or <code>rm(list=ls())</code> in R to clear the variables I created. I could then continue to experiment with my snippet of code.</p>
  
  <p>I am struggling to find such a solution in Python. I researched <a href="https://stackoverflow.com/questions/26545051/is-there-a-way-to-delete-created-variables-functions-etc-from-the-memory-of-th">Is there a way to delete created variables, functions, etc from the memory of the interpreter?</a> and found that there are two ways to accomplish what I am trying to do, but in reality they are not any close. </p>
  
  <p><strong>First method:</strong></p>
  
  <pre><code>%Reset
  </code></pre>
  
  <p>This command deletes all variables and imported setting in PyCharm to run interactive mode. That is, after running above command, I have to re-run </p>
  
  <pre><code>from IPython.core.interactiveshell import InteractiveShell
  InteractiveShell.ast_node_interactivity = "all"
  </code></pre>
  
  <p>This is very annoying.</p>
  
  <p><strong>Second Method:</strong></p>
  
  <p>Another suggested method is to write a <code>for</code> loop and ignore functions starting with <code>__</code>. This also doesn't work for the reasons above. For instance, when I start my PyCharm, I run </p>
  
  <pre><code>from IPython.core.interactiveshell import InteractiveShell
  InteractiveShell.ast_node_interactivity = "all"
  </code></pre>
  
  <p>to ensure that I am able to run a bunch of commands together just as I would do in Matlab and R (In PyCharm, the keyboard shortcut is [Alt + Shift + E; This is called as Execute Selection in Console]). I have noticed that the <code>for</code> loop mentioned in that Stackoverflow thread deletes some of the variables created by running above two lines of code. This means that I won't be able to execute statements by selecting them at a time. I would have to type them individually on the prompt, which is annoying. Another option would be to run above two lines of code every time I execute the <code>for</code> loop, which is annoying as well.</p>
  
  <p>As an aside, I did some research to understand what happens after running above two lines of code. I ran <code>dir()</code> after running above two lines to get a list of all variables existing in my session. </p>
  
  <p>The list looks like this</p>
  
  <pre><code>Out[4]: 
  ['In',
   'InteractiveShell',
   'Out',
   '_',
   '_2',
   '__',
   '___', 
  ......(continued)
   'sys']
  </code></pre>
  
  <p>Is there any way, I can delete user-created variables without deleting above variables? In effect, I want to replicate what <code>rm(list=ls())</code> would do for me in R. </p>
  
  <p>I see that there are a number of people who have asked this question outside StackOverflow such as <a href="https://github.com/spyder-ide/spyder/issues/2563" rel="noreferrer">https://github.com/spyder-ide/spyder/issues/2563</a>. </p>
  
  <p>Thanks for any help. I am using PyCharm with the latest Conda distribution (3.6).</p>