Running Python online in Colab and offline in Jupyter Notebook
Introduction to Data Science and Machine Learning · Md Mahbub E Noor · University of Barishal
print("Hello, Data Science!")A program is a list of instructions, written in a language the computer can follow.
Python reads your code one line at a time, from the top, and does exactly what each line says.
A missing bracket or a misspelled name stops it. Precision is a skill you will build.
Done once per PC, usually by the lab administrator, using the offline Anaconda installer.
| What | Details |
|---|---|
| Anaconda Distribution | Free installer that includes Python, Jupyter Notebook, pandas, scikit-learn and matplotlib |
| Why Anaconda | Every library this course needs is already inside, so no internet is needed afterwards |
| Check it worked | Start menu shows Anaconda Navigator and Anaconda Prompt |
| Data files | Copy course files (for example sms.tsv for Lecture 5) to each PC or a shared folder |
| Students with laptops | Download the installer once at home or from the course share, then install offline |
Start menu, Anaconda Navigator, then Launch under Jupyter Notebook. Or type jupyter notebook in Anaconda Prompt.
A browser tab opens at localhost:8888. localhost means your own PC: no internet used. Open your course folder.
New, then Notebook (pick Python 3 if asked). Rename it Lecture2_1 and run a cell with Shift + Enter.
localhost:8888 (your own PC)print("Hello, Data Science!")Hello, Data Science!
Cell: type code here
The output appears right below the cell
localhost:8888/notebooks/Lecture2_1.ipynbprint("Hello, Data Science!")Hello, Data Science!
[1]: = this cell ran first. [ ]: = not run yetcolab.research.google.comprint("Hello, Data Science!")Hello, Data Science!
Learn the first two shortcuts by heart. The rest you can look up.
| Action | Jupyter Notebook | Google Colab |
|---|---|---|
| Run, go to next cell | Shift + Enter | Shift + Enter |
| Run, stay on this cell | Ctrl + Enter | Ctrl + Enter |
| Run, add a new cell below | Alt + Enter | Alt + Enter |
| Run with the mouse | Run button in the toolbar | Play button beside the cell |
| Run every cell | Run menu > Run All Cells | Runtime menu > Run all |
| Stop a cell that runs forever | Stop (square) button | Click the spinning play button |
print() shows text and results2 + 3 on the last line displays 5print("Hello, Data Science!")
2 + 3Hello, Data Science! 5
# Lecture 2.1 My **first** notebook. - Code cells run Python - Text cells explain it
Lecture 2.1
My first notebook.
# makes a heading, **word** makes bold, - makes a listPython remembers what has RUN, not what is written on screen. Cell A: x = 5. Cell B: print(x * 2).
# Cell A x = 5 # Cell B print(x * 2)
| step | what you do | x in memory | B prints |
|---|---|---|---|
| 1 | Run A | 5 | (nothing) |
| 2 | Run B | 5 | 10 |
| 3 | Edit A to x = 7, do not run | 5 | (nothing) |
| 4 | Run B | 5 | 10 (surprise!) |
| 5 | Run A | 7 | (nothing) |
| 6 | Run B | 7 | 14 |
Next row: arrow key or the ▶ button. Predict it first!
Restarting wipes Python's memory. Running all then rebuilds it from the top, in order, exactly as written.
| Tool | Where to find it |
|---|---|
| Jupyter Notebook | Kernel menu > Restart Kernel and Run All Cells |
| Google Colab | Runtime menu > Restart session and run all (older versions say Restart runtime) |
| When | Before every homework submission, and whenever results look impossible |
| Why it matters | If a notebook only works when run in some special order, it does not really work |
Ctrl + S saves Lecture2_1.ipynb in the folder you opened. Copy it to a USB drive or the course share.
Saved automatically in Google Drive. File > Download > .ipynb gives you a file.
Colab: File > Upload notebook. Jupyter: put the .ipynb in your folder and click it. Data files must travel too.
All of these work offline with the same Anaconda install. We stay with notebooks in this course.
| Way | What it looks like | Used for |
|---|---|---|
| Interactive shell | >>> 2 + 3 | Quick one-line experiments |
| Script file | python hello.py | Programs that run from start to end |
| IDLE / Thonny | Simple editor + Run button | Learning to write scripts |
| VS Code, PyCharm | Full code editor | Larger projects, professional work |
(base) C:\Users\student\course> python hello.py Hello, Data Science!
7 * 6 alone# text cell (Markdown)
# Nadia Rahman, ID 1042
print("Hello, lab!")
7 * 6| You see | Usually means | Fix |
|---|---|---|
| 'jupyter' is not recognized | Typed in the normal Command Prompt | Use Anaconda Prompt, or Anaconda Navigator |
| Browser: localhost refused to connect | The Jupyter server window was closed | Start Jupyter again; keep the black window open |
| NameError after reopening a notebook | Python's memory was cleared | Run all cells from the top |
| FileNotFoundError: sms.tsv | Data file not next to the notebook | Copy it into the notebook's folder |
| Nothing happens, [*] stays | A cell is still running or stuck | Stop button, or restart the kernel |
Shift + Enter | Run cell, go to next |
Ctrl + Enter | Run cell, stay |
[1]: [ ]: [*]: | Ran first / not run / running |
Restart + Run all | Fix execution-order problems |
Ctrl + S | Save the notebook |
# Heading | Markdown heading in a text cell |
.ipynb | Notebook file: works in both tools |
localhost:8888 | Jupyter on your own PC, offline |
Due before Lecture 2.2 · [date]
Open Colab (or Jupyter if you have a laptop with Anaconda). Create HW2_1_<your ID>.
Add a heading, a bold word, and a 3-item list about why you took this course.
Run one cell with Shift + Enter, one with Ctrl + Enter, one with Alt + Enter. In a text cell, describe the difference.
Recreate the Cell A / Cell B trap. Write down what B printed and why, then fix it with restart and run all.
Download the notebook as .ipynb and open it in the other tool (or on the lab PC next class).
Watch it again at home to revise.