You'll often want to paste code into Jupyter console to see if it runs properly. Because of how Python handles indentation, nested for loops, functions, and if statements will fail if you just copy and paste them in.
In order to paste in code with indents, you'll need to use paste magics:
%cpaste
-- opens a special editing area where you can paste in code normally, without whitespace being a problem. You can type--
alone on a line to exit. After you exit, any code you pasted in will be immediately executed.%paste
-- takes code from your clipboard and runs it in Jupyter. This doesn't work on remote systems, where Jupyter doesn't have access to your clipboard.
Instructions
Copy the code below:
for i in range(10):
if i < 5:
print(i)
else:
print(i * 2)
- Open the Jupyter console by typing
ipython
. - Paste in the above code using
%cpaste
. - Exit Jupyter console by typing
exit