What many coders using Jupyter notebooks do not know is that Jupyter notebooks provide you the exclamation mark operator that allows you to execute commands on the underlying operating system.
In Jupyter notebooks, the exclamation mark
! executes commands from the underlying operating system. For example, to run the list directory command
ls in your Jupyter notebook, call
!ls in any cell.
Here are three examples of using three operating system commands in your Jupyter notebook. You can try them yourself in your browser by clicking on the image:
You can even install dependencies from inside your Python script using the exclamation mark/point/bang
! at the beginning of your command:
!pip install numpy
In my Jupyter notebook, this requirement is already satisfied:
Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (1.19.5)
Example Exclamation Mark Operator to Check Python Version in Jupyter Notebook
To check the Python version, run
in your Jupyter notebook cell.
This is the operating system command you’d use to check your Python version in your terminal or command line—prefixed with an exclamation mark. This only works in Jupyter notebooks but not in normal Python scripts.
Here’s how this looks like in our interactive Jupyter notebook:
And here’s for copy&paste:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.