Monday, August 25th, 2025¶

Getting started with Jupyter notebook¶

  • We can execute a cell using SHIFT + ENTER.
  • We can add a new cell using the plus symbol in the top-left corner. Alternatively, you can select a cell and hit A to add a cell above, or B to add a cell below.
  • We can delete a cell by selecting hit, then hitting D, D.
In [1]:
2 + 7
Out[1]:
9
In [3]:
100 + 7
Out[3]:
107

Arithmetic operations¶

Addition:

In [4]:
400 + 3
Out[4]:
403

Subtraction:

In [5]:
300 - 5
Out[5]:
295

Multiplication:

In [6]:
50 * 3
Out[6]:
150

Division:

In [7]:
10 / 2
Out[7]:
5.0

Exponentiation:

In [8]:
2^5
Out[8]:
7

To perform exponentiation, use ** or the pow function instead of ^:

In [10]:
2**5
Out[10]:
32
In [11]:
pow(2,5)
Out[11]:
32

Using the standard division / will return what is called a float.

In [13]:
5 / 2
Out[13]:
2.5
In [12]:
6/3
Out[12]:
2.0
In [14]:
5 / 3
Out[14]:
1.6666666666666667

Integers vs floats¶

  • integers = numbers without any decimal point
  • floats = numbers with a decimal point
  • operations on integers are exact
  • operations on floats are approximate
  • integers can be arbitrarily large, but floats are represented only within a finite range.
In [15]:
2.0 ** 100
Out[15]:
1.2676506002282294e+30
In [16]:
2**100
Out[16]:
1267650600228229401496703205376
In [23]:
2.0**1200
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
Cell In[23], line 1
----> 1 2.0**1200

OverflowError: (34, 'Result too large')
In [24]:
2**1200
Out[24]:
17218479456385750618067377696052635483579924745448689921733236816400740691241745619397484537236046173286370919031961587788584927290816661024991609882728717344659503471655990880884679896520055123906467064419056526231345685268240569209892573766037966584735183775739433978714578587782701380797240772477647874555986712746271362892227516205318914435913511141036261376

Markdown¶

Markdown is a markup language used for creating formatted text. In Jupyter, we create Markdown cells for this text.

In [27]:
This cell will throw an error.
  Cell In[27], line 1
    This cell will throw an error.
         ^
SyntaxError: invalid syntax

This cell will not throw an error.

We can create headings using hashtags, #:

This is a heading¶

This is a sub-heading¶

This is a sub-sub-heading¶

We can generate bulleted (un-numbered) using a space and hyphen:

Here is a list:

  • Item 1
  • This is item 2
  • I'm item 3

We can also nest lists using additional spaces:

Here is a list with nested sub-lists:

  • Item 1
  • Item 2
    • Item 2(a)
    • Item 2(b)
    • Item 2(c)
  • Item 3

We can also create numbered lists using 1., 2., etc. Note: A numbered list will always be enumerated 1, 2, 3... regardless of how you've actually numbered the items.

Here is a numbered list:

  1. Item 1
  2. Item 2
  3. Item 3
  4. Item 6

Back to integers vs. floats¶

The normal division symbol / will always return a floating point number.

In [28]:
6 / 3
Out[28]:
2.0

Sometimes, we would prefer to return an integer number. We can this using the // operation:

In [29]:
6 // 3
Out[29]:
2
In [30]:
7 // 2
Out[30]:
3

The integer division // will always return the quotient from performing long division.

We can get the remainder from integer division using %. This is called modular division.

In [31]:
7 % 2
Out[31]:
1

This cell above shows that the remainder of dividing 7 by 2 is 1.

In [35]:
101 // 43
Out[35]:
2
In [36]:
101 % 43
Out[36]:
15

The two cells above show that 100 divided by 42 has quotient 2 and remainder 16. Let's check this:

In [37]:
43 * 2 + 15
Out[37]:
101

Variable assignment in Python¶

Very often, we want to perform operations on some input that might change. We can define variables using the = symbol:

In [48]:
num = 102
den = 43

We can recall the value of this variable with the variable name:

In [49]:
num
Out[49]:
102
In [50]:
den
Out[50]:
43
In [51]:
quot = num // den
In [52]:
rem = num % den
In [53]:
quot
Out[53]:
2
In [54]:
rem
Out[54]:
16
In [55]:
quot*den + rem
Out[55]:
102