Run Your Multi-Line Python Code in a Single Line in the Python REPL
Run Your Multi-Line Python Code in a Single Line in the Python REPL How to use exec to run multi-line Python in a single line #Python #REPL #Coding #Programming You’re in a browser. No terminal. No setup. Just a tiny Python REPL powered by Pyodide . You type: a, b = 0 , 1 for i in range ( 10 ): a, b = b, a + b print (a) Beautiful. It works. But… your REPL says: “One line only, please.” Now what? Enter: exec() — your one-line superpower. 🔥 The Problem: Multi-Line Code, One-Line Limit Many web-based REPLs (like those in tutorials, docs, or apps) only accept one line of input . That means no indented blocks. No multi-line functions. No joy. But wait — there’s a way. ✅ The Fix: Wrap Code in exec() exec() runs Python code from a string — and that string can contain newlines ( \n ) to simulate real structure. So this: a, b = 0 , 1 for i in range ( 10 ): a, b = b, a + b print (a) Becomes this o...