IPython - Importing Python Shell Code



IPython can read from standard Python console with default >>> prompt and another IPython session. The following screenshot shows a for loop written in standard Python shell −

Python 3.14.2 (tags/v3.14.2:df79316, Dec  5 2025, 17:18:21) [MSC v.1944 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> for x in range(11):
...     print(x)
...
0
1
2
3
4
5
6
7
8
9
10
>>>

Copy the code (along with Python prompt) and paste the same in IPython input cell. IPython intelligently filters out the input prompts (>>> and ...) or IPython ones (In [N]: and ...:)

In [1]: >>> for x in range(11):
   ...: ...     print(x)
   ...: ...
   ...:
0
1
2
3
4
5
6
7
8
9
10

In [2]:

Similarly, code from one IPython session can be pasted in another. The first screenshot given below shows definition of SayHello() function in one IPython window −

In [2]: def SayHello():
   ...:     print("Hello World")
   ...:

Now, let us select the code and paste in another IPython shell and call SayHello() function.

In [1]: In [2]: def SayHello():
   ...:    ...:     print("Hello World")
   ...:    ...:
   ...:

In [2]: SayHello()
Hello World
Advertisements