LECTURE 2.1

Getting started

Running Python online in Colab and offline in Jupyter Notebook

Today's key line of code
print("Hello, Data Science!")

Where we are: Lecture 2, part 1 of 3

Part 1 of 3L2Python basics
L3Decisions and loops
L4Counting words
L5Real data
L6Build the model

Today you will

  1. 1Understand what a program is and what Python does with it
  2. 2Open Jupyter Notebook offline in the lab, and Colab at home
  3. 3Run cells in several ways and write text cells
  4. 4Understand execution order, and fix it with restart and run all
  5. 5Save your work and move notebooks between lab and home
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal2 / 20

What is a program?

1

Instructions

A program is a list of instructions, written in a language the computer can follow.

2

Top to bottom

Python reads your code one line at a time, from the top, and does exactly what each line says.

3

Exact, not smart

A missing bracket or a misspelled name stops it. Precision is a skill you will build.

Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal3 / 20

Two places to write Python

Jupyter Notebook (lab, offline)

  • Runs on your own computer
  • No internet needed
  • Installed once with Anaconda
  • Files saved in a folder on the PC

Google Colab (home, online)

  • Runs on Google's computers
  • Needs internet and a Google account
  • Nothing to install
  • Files saved in your Google Drive
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal4 / 20

One-time lab setup: Anaconda

Done once per PC, usually by the lab administrator, using the offline Anaconda installer.

WhatDetails
Anaconda DistributionFree installer that includes Python, Jupyter Notebook, pandas, scikit-learn and matplotlib
Why AnacondaEvery library this course needs is already inside, so no internet is needed afterwards
Check it workedStart menu shows Anaconda Navigator and Anaconda Prompt
Data filesCopy course files (for example sms.tsv for Lecture 5) to each PC or a shared folder
Students with laptopsDownload the installer once at home or from the course share, then install offline
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal5 / 20

Start Jupyter Notebook in the lab

  1. 1
    Launch

    Start menu, Anaconda Navigator, then Launch under Jupyter Notebook. Or type jupyter notebook in Anaconda Prompt.

  2. 2
    Your folder

    A browser tab opens at localhost:8888. localhost means your own PC: no internet used. Open your course folder.

  3. 3
    New notebook

    New, then Notebook (pick Python 3 if asked). Rename it Lecture2_1 and run a cell with Shift + Enter.

localhost:8888 (your own PC)
File Edit View Run Kernel Settings
[1]:
print("Hello, Data Science!")
Hello, Data Science!

Cell: type code here

The output appears right below the cell

Shift + Enter = run
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal6 / 20

Tour of a Jupyter notebook

localhost:8888/notebooks/Lecture2_1.ipynb
File Edit View Run Kernel Settings Help
+ Run Stop Restart Code
Lecture 2.1: my first notebook
A text cell: notes for humans.
[1]:
print("Hello, Data Science!")
Hello, Data Science!
[ ]:
 
Menus: Run and Kernel are the ones you need most
Toolbar: add cell, run, stop, restart
Text cell (Markdown): headings and notes
Code cell: type Python here
[1]: = this cell ran first. [ ]: = not run yet
Output appears right below its cell
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal7 / 20

The same notebook in Colab

colab.research.google.com
File Edit View Insert Runtime Tools Help
+ Code + Text
Lecture 2.1: my first notebook
A text cell: notes for humans.
▶
print("Hello, Data Science!")
Hello, Data Science!
 
Runtime menu = Jupyter's Kernel menu
+ Code and + Text add cells
Text cells work the same way
Play button runs the cell; the run number appears after running
Output below the cell, as in Jupyter
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal8 / 20

Ways to run a cell

Learn the first two shortcuts by heart. The rest you can look up.

ActionJupyter NotebookGoogle Colab
Run, go to next cellShift + EnterShift + Enter
Run, stay on this cellCtrl + EnterCtrl + Enter
Run, add a new cell belowAlt + EnterAlt + Enter
Run with the mouseRun button in the toolbarPlay button beside the cell
Run every cellRun menu > Run All CellsRuntime menu > Run all
Stop a cell that runs foreverStop (square) buttonClick the spinning play button
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal9 / 20

Your first code cell

  • print() shows text and results
  • Notebooks also show the value of the last line automatically
  • Here 2 + 3 on the last line displays 5
  • The same code works in Jupyter and Colab
Code
print("Hello, Data Science!")
2 + 3
Output
Hello, Data Science!
5
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal10 / 20

Text cells: notes for humans

You type (text cell)

Markdown
# Lecture 2.1
My **first** notebook.

- Code cells run Python
- Text cells explain it

It displays as

Lecture 2.1

My first notebook.

  • Code cells run Python
  • Text cells explain it
  • Change a cell to text: Jupyter Esc then M, or the Code/Markdown menu; Colab uses + Text
  • # makes a heading, **word** makes bold, - makes a list
  • Every homework notebook starts with a text cell: your name and ID
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal11 / 20

Execution order: the notebook trap

Python remembers what has RUN, not what is written on screen. Cell A: x = 5. Cell B: print(x * 2).

Code
# Cell A
x = 5

# Cell B
print(x * 2)
stepwhat you dox in memoryB prints
1Run A5(nothing)
2Run B510
3Edit A to x = 7, do not run5(nothing)
4Run B510 (surprise!)
5Run A7(nothing)
6Run B714

Next row: arrow key or the ▶ button. Predict it first!

Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal12 / 20

The cure: restart and run all

Restarting wipes Python's memory. Running all then rebuilds it from the top, in order, exactly as written.

ToolWhere to find it
Jupyter NotebookKernel menu > Restart Kernel and Run All Cells
Google ColabRuntime menu > Restart session and run all (older versions say Restart runtime)
WhenBefore every homework submission, and whenever results look impossible
Why it mattersIf a notebook only works when run in some special order, it does not really work
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal13 / 20

Saving, and moving between lab and home

1

Jupyter: a folder

Ctrl + S saves Lecture2_1.ipynb in the folder you opened. Copy it to a USB drive or the course share.

2

Colab: Google Drive

Saved automatically in Google Drive. File > Download > .ipynb gives you a file.

3

Move either way

Colab: File > Upload notebook. Jupyter: put the .ipynb in your folder and click it. Data files must travel too.

Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal14 / 20

Other ways to run Python (for later)

All of these work offline with the same Anaconda install. We stay with notebooks in this course.

WayWhat it looks likeUsed for
Interactive shell>>> 2 + 3Quick one-line experiments
Script filepython hello.pyPrograms that run from start to end
IDLE / ThonnySimple editor + Run buttonLearning to write scripts
VS Code, PyCharmFull code editorLarger projects, professional work
Anaconda Prompt: running a script file
(base) C:\Users\student\course> python hello.py
Hello, Data Science!
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal15 / 20

Your turn 10 minutes

  1. 1Start Jupyter and create a notebook named Lecture2_1
  2. 2Add a text cell with your name, ID, and today's date
  3. 3Add a code cell that prints a greeting, then a cell with 7 * 6 alone
  4. 4Restart and run all, then save with Ctrl + S
Code
# text cell (Markdown)
# Nadia Rahman, ID 1042

print("Hello, lab!")
7 * 6
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal16 / 20

Setup problems and fixes

You seeUsually meansFix
'jupyter' is not recognizedTyped in the normal Command PromptUse Anaconda Prompt, or Anaconda Navigator
Browser: localhost refused to connectThe Jupyter server window was closedStart Jupyter again; keep the black window open
NameError after reopening a notebookPython's memory was clearedRun all cells from the top
FileNotFoundError: sms.tsvData file not next to the notebookCopy it into the notebook's folder
Nothing happens, [*] staysA cell is still running or stuckStop button, or restart the kernel
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal17 / 20

Today's toolkit

Shift + EnterRun cell, go to next
Ctrl + EnterRun cell, stay
[1]: [ ]: [*]:Ran first / not run / running
Restart + Run allFix execution-order problems
Ctrl + SSave the notebook
# HeadingMarkdown heading in a text cell
.ipynbNotebook file: works in both tools
localhost:8888Jupyter on your own PC, offline
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal18 / 20

Homework 2.1

Due before Lecture 2.2 · [date]

1

Set up at home

Open Colab (or Jupyter if you have a laptop with Anaconda). Create HW2_1_<your ID>.

2

Text cells

Add a heading, a bold word, and a 3-item list about why you took this course.

3

Run in every way

Run one cell with Shift + Enter, one with Ctrl + Enter, one with Alt + Enter. In a text cell, describe the difference.

4

Break the order

Recreate the Cell A / Cell B trap. Write down what B printed and why, then fix it with restart and run all.

5

Move it

Download the notebook as .ipynb and open it in the other tool (or on the lab PC next class).

Submit on [LMS / Google Classroom]: your notebook file HW2_1_<your ID>.ipynb, or a Colab link set to “Anyone with the link can view”.
Md Mahbub E NoorLecture 2.1: Getting startedUniversity of Barishal19 / 20