#!/usr/bin/env python # coding: utf-8 # # Running Python in Jupyter Notebook # There are different ways to execute Python script. Most common ways: # # - 🖥 Running Python script in terminal/command line in Computer. # - ⚙️ Running Python script in command line in Server. # - 🤖 Running Python script in command line in embedded system. # - 📒 Running Python code snippets in Jupyter Notebook. # - 📟 Turnning Python script into exe and execute in Windows. # - 📱 Running Python in iOS and ipadOS. # - 📳 Running Python in Android. # - 🔁 An intergration of methods above with other systems, such as Windows Scheduler, iOS Widget, Siri Shortcut. # ## 📒 An introduction to running code in Jupyter notebook # In this notebook, we explore how to use Jupyter Notebook to run Python code. # For example, the following code output the reuslt of the calculation # In[1]: 1+2 # In Jupyter notebook, code is spearated into `cells`. # In[2]: example = "This is another cell" # Jupyter notebook shows the result of last line of each cell, if it is not a variable assignment. We call it "Inspection". # **Cell** is the most special thing in Jupyter notebook. # 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. # In[3]: example # 👆🏻 See? We can inspect `example` variable that is stored previously. # 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. # # Now here is the code that asks for input. # In[4]: name = input("What is your name? ") print(f"Hello {name} :-)") # ## ⌨️ Using Jupyter Notebook via Keyboards # 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. # ### Moving up and down # Moving selection up and down. # # - You can move the selection up by using `up arrow` or `K`. # - You can move the selection down by using `down arrow` or `J`. # # ### Editing cell # `ENTER` to edit the current selected cell. # The cell selection shows blue border by default. It turns into green border when toggled into **editing** mode. # ### Executing cell # Here are some keyboard shortcuts. # `CTRL+ENTER` to execute the current cell. # # `SHIFT+ENTER` to execute the current cell and move to next cell # ### Code or Markdown # There are mainly two types of cells: **Code** and **Markdown**. # Code is for Python execution and Markdown is for copy writings. # # - You can toggle a cell into Markdown by pressing `M`. # - You can toggle a cell into Code by pressing `Y`. # ### Inserting cells # Inserting cells below or above the current selection. # # - We can use `A` to insert a cell above the current cell. # - We can use `B` to insert a cell below the current cell. # ## Summary # # 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.