Python Programming : A Classroom Approach.

2. Your First Python Program

 

2.1 Jupyter Notebook :

Let’s see how we can use Jupyter Notebook to write our first program.

Open Jupyter Notebook: If you’ve installed Anaconda, you can launch Jupyter Notebook from the Anaconda Navigator. Otherwise, open your command line/terminal, navigate to the directory where you want to store your notebooks, and type jupyter notebook and press Enter. This should open Jupyter Notebook in your web browser.

Create a New Notebook: In the Jupyter Notebook interface, click on the "New" button and select "Python 3" (or the appropriate Python version you have installed). This will create a new, blank notebook.

Writing the Code: In the first cell of the notebook, type the following code:

print("Hello, world!")

Running the Code: To run the code in the cell, click on the "Run" button in the toolbar or press Shift + Enter. The output "Hello, world!" should appear below the cell.

Understanding Syntax and Basic Program Structure (in Jupyter Notebook): The same principles of Python syntax and program structure apply in Jupyter Notebook as they do in a regular Python script. The key difference is that you can execute code in individual cells and see the results immediately. This makes Jupyter Notebook a great tool for experimenting and learning. Also, you can test small-scale changes in a program without re-running the entire script.

Troubleshooting Common Errors (in Jupyter Notebook): If you encounter an error in a cell, the error message will be displayed below the cell. You can then edit the code in the cell and run it again. Because Jupyter Notebook keeps variables defined throughout a notebook session, be careful when reassigning values, as this could throw a TypeError.