cb=compile('print("Hello")', '', 'single')
exec(cb) # Hello
print('plus2net')
fob=open("D:\sample.txt",'r')
str=fob.read()
fob.close()
cb=compile(str,'sample.txt','single')
exec(cb)
Output
plus2net
str="sum([1,2,3,4])"
cb=compile(str,'','single')
exec(cb)
Output
10
import ast
ast_obj = ast.parse("print('plus2net')")
code = compile(ast_obj, filename="", mode="exec")
exec(code) # plus2net
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.