{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Running Python in Jupyter Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are different ways to execute Python script. Most common ways:\n", "\n", "- 🖥 Running Python script in terminal/command line in Computer.\n", "- ⚙️ Running Python script in command line in Server.\n", "- 🤖 Running Python script in command line in embedded system.\n", "- 📒 Running Python code snippets in Jupyter Notebook.\n", "- 📟 Turnning Python script into exe and execute in Windows.\n", "- 📱 Running Python in iOS and ipadOS.\n", "- 📳 Running Python in Android.\n", "- 🔁 An intergration of methods above with other systems, such as Windows Scheduler, iOS Widget, Siri Shortcut." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 📒 An introduction to running code in Jupyter notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook, we explore how to use Jupyter Notebook to run Python code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, the following code output the reuslt of the calculation" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1+2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Jupyter notebook, code is spearated into `cells`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "example = \"This is another cell\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Jupyter notebook shows the result of last line of each cell, if it is not a variable assignment. We call it \"Inspection\"." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Cell** is the most special thing in Jupyter notebook." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In normal programming environment, code is executed **from top to bottom at once**. In Jupyter notebook, code execution is separated, but the state is kept." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This is another cell'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "👆🏻 See? We can inspect `example` variable that is stored previously." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can take input by using `input()` in Python. It works in Jupyter notebook too. And BE WARE that if you execute the cell twice before entering the input, you will make the kernel dead-loop. If the dead-loop happens, you may choose `kernel` > `Restart` to escape the dead-loop.\n", "\n", "Now here is the code that asks for input." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What is your name? Thomas\n", "Hello Thomas :-)\n" ] } ], "source": [ "name = input(\"What is your name? \")\n", "print(f\"Hello {name} :-)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ⌨️ Using Jupyter Notebook via Keyboards" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Jupyter notebook comes with some keyboard shortcuts for quicker operation without leaving the hands away from keyboard. In the following, I will show some essential keyboard shortcuts." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Moving up and down" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Moving selection up and down.\n", "\n", "- You can move the selection up by using `up arrow` or `K`.\n", "- You can move the selection down by using `down arrow` or `J`.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Editing cell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`ENTER` to edit the current selected cell." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The cell selection shows blue border by default. It turns into green border when toggled into **editing** mode." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Executing cell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are some keyboard shortcuts." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`CTRL+ENTER` to execute the current cell.\n", "\n", "`SHIFT+ENTER` to execute the current cell and move to next cell" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Code or Markdown" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are mainly two types of cells: **Code** and **Markdown**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Code is for Python execution and Markdown is for copy writings.\n", "\n", "- You can toggle a cell into Markdown by pressing `M`.\n", "- You can toggle a cell into Code by pressing `Y`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Inserting cells" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inserting cells below or above the current selection.\n", "\n", "- We can use `A` to insert a cell above the current cell.\n", "- We can use `B` to insert a cell below the current cell." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "In this notebook, we learned to operate Jupyter notebook and some keyboard shortcuts to master it. In next notebook, we will learn the Python variables." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }