Python code in Jupyter notebook
Python code in Jupyter notebook | Using Notepad++
How we can run python file in Jupyter notebook?
In this, when we want to run python code that is written in any editor like notepad,notepad++ etc.
Firstly, I will tell you loading the code and running the python file in ipython.
How to load code in Jupyter notebook?
You can load code from file in jupyter notebook by writing this code in ipython –
%load PythonFilename.py
If you have kept your file in different location you will get this type of error-
ValueError: ‘factorial.py’ was not found in history, as a file, url, nor in the user namespace.
This file PythonFilename.py should be in current working directory. It is better to kept the file where you are using your Jupyter notebook.
How to run the python file in Jupyter notebook?
You can run the python file by writing this code-
%runPythonFilename.py
I am giving one example so you can easily understood this concept.
I am creating factorial python code on notepad++ and save as factorial.py.
For Example-
Consider an example for a factorial of a number in python.
n=input("Enter number:")
fact=1
if
int(n)>=1:
for i in range(1,int(n)+1):
fact=fact*i
print("Factorial is :",fact)
I also kept screenshot for code written in python for better understanding.
NOTE — In windows, when you clicks Shift+Enter key — move to next cell with result (output of your code).
To load code in jupyter-
%load factorial.py
To run the python file in jupyter-
%run factorial.py
This example, i input a number: 5 and got result 120.
In mathematics, 5!=1*2*3*4*5=120.
I hope you have understood the running the code in ipython. Now, you can start coding on this platform and also used for data analysis with the help of python.