Wednesday, January 22nd¶

In [1]:
1 + 1
Out[1]:
2
In [2]:
1 + 4
Out[2]:
5

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
  • We can delete a cell by selecting hit, then hitting D, D

Arithmetic operations¶

In [3]:
1 + 3
Out[3]:
4
In [4]:
1 - 3
Out[4]:
-2
In [5]:
4*5
Out[5]:
20
In [6]:
4 / 5
Out[6]:
0.8
In [7]:
3^4
Out[7]:
7

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

In [8]:
3**4
Out[8]:
81
In [9]:
pow(3,4)
Out[9]:
81

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

In [10]:
5 / 2
Out[10]:
2.5
In [11]:
4 / 2
Out[11]:
2.0
In [12]:
2.0 ** 1000
Out[12]:
1.0715086071862673e+301
In [13]:
2**1000
Out[13]:
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
In [14]:
2.0**1500
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_23904\2939582728.py in <module>
----> 1 2.0**1500

OverflowError: (34, 'Result too large')
In [15]:
2**1500
Out[15]:
35074662110434038747627587960280857993524015880330828824075798024790963850563322203657080886584969261653150406795437517399294548941469959754171038918004700847889956485329097264486802711583462946536682184340138629451355458264946342525383619389314960644665052551751442335509249173361130355796109709885580674313954210217657847432626760733004753275317192133674703563372783297041993227052663333668509952000175053355529058880434182538386715523683713208549376

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.

Markdown¶

This is some text within a Markdown cell.

In [16]:
This is some text in a code cell, and will throw an error
  File "C:\Users\Luke\AppData\Local\Temp\ipykernel_23904\3831271697.py", line 1
    This is some text in a code cell, and will throw an error
                 ^
SyntaxError: invalid syntax

We can double-click on a markdown cell to edit it.

We can create headings using hashtags, #:

  • # Heading gives a top-level heading
  • ## Sub-heading gives a sub-heading
  • ### Sub-sub-heading gives a sub-sub-heading

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

  • Item 1
  • Item 2
  • Item 3
  • item 1
  • item 2
  • item 3

We can also nest lists:

  • item 1
    • A
    • B
    • C
  • item 2
    • X
    • Y
  • item 3
  • item 4

We can also create numbered lists using 1., 2., etc.

  1. First item
  2. Second item
  3. Third item
  4. Fourth item

Back to integers vs. floats¶

In [17]:
5 / 2
Out[17]:
2.5
In [18]:
4 / 2
Out[18]:
2.0

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

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

In [19]:
4 // 2
Out[19]:
2
In [20]:
5 // 2
Out[20]:
2

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 [21]:
5 % 2
Out[21]:
1
In [22]:
123 % 7
Out[22]:
4
In [23]:
123 // 7
Out[23]:
17

The above calculation demonstrates that $123 = 7\cdot 17 + 4$.

In [24]:
(123/7 - 17) * 7
Out[24]:
4.000000000000011
In [25]:
8 / 3
Out[25]:
2.6666666666666665

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 [26]:
n = 130

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

In [27]:
n
Out[27]:
130
In [28]:
n // 7
Out[28]:
18
In [29]:
n % 7
Out[29]:
4
In [30]:
n
Out[30]:
130
In [ ]: