1. Intro to Notebooks and Python First Steps#

Prof Eatai Roth
DS256 F2025
Gettysburg College

1.1. Jupyter Notebook Intro#

Jupyter (stands for Julia-Python-R) Notebooks are coding environments that let you write, annotate, and run code a chunk at a time. These chunks are contained in cells.

1.1.1. Cells#

The notebooks we’ll use in this class comprise two kinds of cells:

  • Markdown - text annotation (like this cell).

  • Code - cells that will run code (Python).

The cell type is noted in the lower right corner (and you can change the cell type from there).

1.1.2. Kernels#

Before running code, you’ll have to select a Kernel (“Select Kernel” upper right corner of the coding window); if you forget, VS Code will prompt you to select a kernel when it gets to the first code cell.

You can think of a kernel as a computational session and VS Code wants to know which version of Python you’d like to use to execute your code. It will present a list of installed Python distributions; you should select the one that has ‘Anaconda’ in the path.

The kernel also keeps track of all the variables you’ve created in the session. If you want to run your code from a clean slate, you can restart the kernel to clear all variables.

1.1.3. Keyboard Shortcuts#

Some useful keyboard shortcuts:

  • SHIFT+RETURN (SHIFT+ENTER on PC) - runs a cell and moves focus to the next cell

  • Esc -> A - adds a cell above the current cell (-> means followed by)

  • Esc -> B - adds a cell below the current cell

  • Esc -> M - converts a cell to Markdown

  • Esc -> Y - converts a cell to Code

1.2. Let’s play around in the notebook!#

Firstly, you can use a Jupyter notebook as a calculator. Other than convenience, we’ll hardly ever do that in our projects. But let’s do some arithmatic.

# In a code cell, anything that comes after a # is comment. We use comments to explain our code or leave love letters to our future selves.
# You'll be surprised just how quickly you forget what you were thinking when you originally wrote code, so help yourself out and comment.
# When your code runs, comments are ignored; it's just for humans.

# Let's introduce some math operators.

# Addition and subtraction
a = 4 + 5
b = 111-99

print(f'a = {a}\nb = {b}')

# Multiplication and division
c = 5 * 4
d = 20/4


# Exponentiation
e = 0.5**2

print(f'c = {c}\nd = {d}\ne = {e}')

# Run the cell with SHIFT+ENTER
a = 9
b = 12
c = 20
d = 5.0
e = 0.25
# Increment and Decrement

a +=10
b -= 5

# Geometric (?) Increment and Decrement
c *= 100
d /= 4

print(f'a (after increment) = {a}')
print(f'b (after decrement) = {b}')
print(f'c (after multiplication) = {c}')
print(f'd (after division) = {d}')

# Pay attention to how the values below relate to the values above.
a (after increment) = 19
b (after decrement) = 7
c (after multiplication) = 2000
d (after division) = 1.25

1.3. What is computer code?#

We’ve already gotten a glimpse of programming above. We used variables (e.g. a, b, c, d, e), operators (e.g. +, -, *, /, **), built-in functions (e.g. print())

ELINSTB

A programming language is a set of instructions followed EXPLICITLY by a computer. Like a natural language (between people…or people and LLMs), a programming language has nouns (stored data) and verbs (actions).

*A little bit deeper about Python

Python is an object-oriented language and everything in Python is an object. What’s that mean? Objects are members of a group of similar things; the groups are called classes. And every object has special properties (attributes) and functions (methods) specific to that class.

So ‘a’ is an object? Yes, ‘a’ is an object that is of a numeric data type. What about ‘print()’, I thought that was a verb? Yes, even ‘print’ is an object, a function object. Think of print as a printer; that’s a thing that prints (a blender blends, a television televises, etc.)

2. Variables#

(Read WTOP Ch 3)

Variables store the data we use in programs. You can think of a variable as a box with a label on it (or a mailbox with an address…that happens to be a location in your computer’s memory). Variables hold our data and keep track of the values (see above), but what kinds of data can variables hold. For now, we’ll start with three simple data types.

  • Numeric - integers (round numbers) and floats (decimal numbers)

  • String - any text (e.g. a single character, a password, an entire book). Strings are denoted by quotes, either ‘hello’ or “hello” is fine.

  • Boolean - True or False. True and False are keywords and will be color-highlighted when used.

2.1. Numeric#

Numeric values can be either integers or floats. In first assigning the variable, it is an integer or a float depending on whether or not the number has a decimal point. You can mix and match ints and floats when doing arithmetic.

# Numeric

# Integers
x = 7
y = 7.
z = 7.0

print(f'x is a {type(x)}')
print(f'y is a {type(y)}')
print(f'z is a {type(z)}')
x is a <class 'int'>
y is a <class 'float'>
z is a <class 'float'>

2.2. Strings#

Strings are textual data (including numbers and characters).

# Strings

s1 = 'hello'
s2 = 'goodbye123'
space = ' '
s1 + ' and ' + s2
'hello and goodbye123'
(s1 + space) * 3
'hello hello hello '
s1 - s1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[7], line 1
----> 1 s1 - s1

TypeError: unsupported operand type(s) for -: 'str' and 'str'

2.3. Booleans#

True and False statements.

A = True
B = False

# Logical Operators (and, or, not, ==, >, <, >=, <=)

C = 5 >= 10
D = (s1=='hello') and (x<10)