%save sessionName linesToKeep
This will save script in line numbers you specify in linesToKeep,for example, 1-20 34-50 64 into a file called “sessionName.py” in your current working directory.
If you are not so specific on which lines you want to keep, you can save everything.
%save sessionName ~0/
This saves everything from the current session, denoted as “~0″ into file “sessionName.py” in your cwd (current working directory).
%save pastSession ~1/
This saves everything from the past session, denoted as “~1” into file “pastSession.py” in your cwd.