Open In App

InteractiveConsole runcode() in Python

With the help of InteractiveConsole.runcode() method, we can get the output of the single or multiple lines of code, if we want to see the result of some part of code then we can use InteractiveConsole.runcode() method.

Syntax : InteractiveConsole.runcode(code)



Return : Return the output of the code section.

Example #1 :
In this example we can see that by using InteractiveConsole.runcode() method, we are able to get the result of some part of code without running the whole code by using this method.




# import code
from code import InteractiveConsole
  
code = 'print("GeeksForGeeks")'
# Using InteractiveConsole.runcode() method
InteractiveConsole().runcode(code)

Output :



GeeksForGeeks

Example #2 :




# import code
from code import InteractiveConsole
  
code = 'a = 10; print(a + 20)'
# Using InteractiveConsole.runcode() method
InteractiveConsole().runcode(code)

Output :

30

Article Tags :