Home

# INTRODUCTION TO PYTHON JUPYTER NOTEBOOK

Ivan Josipovic - October 25, 2021

Ivan Josipovic – Data Analytics

What is Jupyter Notebook?

Jupyter Notebook is an open-source data analytics software which works in your internet browser.

It allows data analysts and scientists to create one single document which may be comprised of data visualizations, comments, math equations and other media. Thus greatly speeding up and enabling data to be processed, visualized, analyzed and reported on, all using one of the most vastly utilized programming languages in the world; Python!

How do I download and install Jupyter Notebook (for Windows)?

Simple really, follow along and lets do this!

2.         Click on this website:  www.anaconda.com

3.         Navigate to “Anaconda Individual Edition” download.

4.         For Windows, Click on Download Anaconda Individual Edition.

6          Once installed, in the search box next to the Windows start menu, type: Anaconda.

7.         Anaconda Navigator should show in the results. Click on it.

8.         Once open, there is a selection of tools and programs. Click on Jupyter Notebook.

9.         It should have opened up in your default internet browser, as a new tab.

10.       You will see a number of folders shown. Select the most appropriate folder where you wish to store all your Jupyter Notebook code documents. For ease of use, perhaps create a new folder on your desktop and then in the Jupyter Notebook browser tab, navigate to that folder.

11.       Click on New. Select Python, and a new tab will open with your new blank document.

LETS START WITH THE BASICS OF PYTHON IN JUPYTER NOTEBOOK

Variable Types

There are 4 types of variables we will be working with python in Jupyter.

These variable types are:

• Integer
• Float
• String
• Logical

Integer also known as a whole number, in python it is represented as ‘int’.

For example lets assign the value of 2 to the variable x:

X = 2

When we run x, it would return the value of 2.

(to run a line of code in Jupyter Notebook, hold SHIFT and press ENTER)

In other words we have stated that in the variable name of x, like a basket called x, we have placed the value of 2 within it. To be recalled whenever we say “x”.

In Jupyter, we can check the variable type simply by typing:

type()

*in the brackets we would type in the variable name, so that we can check what type of value it holds!

Float is a number with a decimal place. In other programming languages it might be called a double. However, in Python it is called a Float.

For example lets assign the value of 4.5 to the variable y:

y = 4.5

When we run y, 4.5 is returned!

String is a single character surrounded by quotation marks with the length of 1.

For example lets assign the string “hello” to the variable a:

a = “hello”

and the string “there” to the variable b:

b = “there”

we can test this out what this returns:

a + b

(SHIFT + ENTER)

Would return ‘hellothere’.

But this looks messy! Ok no stress… Lets add something to our statement:

a + “ “ + b = ‘hello there’ (please note, a space between quotation marks results in exactly that when added to variables)

Logical is a value defined as being either True or False.

For example:

A = True

When we run A, we would get True.

n = 4 > 5

This is saying the statement ‘4 is greater than 5’ is assigned to the variable n.

When n is run, we would get:

False

Because we know that there is no way that 4 could be larger than 5.

Likewise if we run m:

m = 10 > 3

We would get True in return!

So far we have been simply running the variable name in order to return its assigned value.

However, this is an incorrect way of doing so.

The correct way would be:

print()

Whatever variable you wish to run and return, you would place in those brackets!

Working with Variables

How do we work with variables you might be asking..

Well much same like basic maths arithmetic.

a = 10

b = 5

c = a + b         (addition)

print(c) = 15

d = a – b         (subtraction)

print(d) = 5

e = a * b          (multiplication)

print(e) = 50

f = a / b           (division)

print(f) = 2

One can also add strings together! For example:

greet = “hello”

name = “Steve”

message = greet + “ “ + name

print(message) = hello Steve

Boolean Operators

These are the symbols or operators we use in a logical test statement to determine whether a value is TRUE or FALSE.

== equals

!= or <> not equal

< less than

> greater than

<= less than or equal to

>= greater than or equal to

and

or

not

Examples below show the use of Boolean Operators.

5 == 5  (five is equal to five)

= True

3 == 5  (3 equals 5 which is of course not true and returns a False)

= False

7 != 4   (seven is not equal to four)

= True

5 > 6    (five is greater than 6)

= False

3 < 4    (three is less than four)

= True

show = 4 < 5

show2 = not(6 > 2)    (not turns whatever is true to false and vice versa)

print(show2)

= False

show or show2          (true value for show or show2)

= True

show and show2       (and requires both called variables to be true)

= False

IF Statement

An if statement is used to execute code once, only if the statement is met by the designated True or False logic.

For example:

apples = 23

bananas = 46

if bananas > apples:

answer = “There are more bananas than apples”

This would return

There are more bananas than apples

Because yes, 46 which is assigned to bananas, is a greater number than 23 which is assigned to apples!

IF ELSE Statement

The if else statement is used to return two possible outcomes once code is executed, depending if it meets the True or False criteria.

For example:

apples = 23

bananas = 46

if apples > bananas:

answer = “There are more apples than bananas”

else:

answer = “Check your eyes bud, there are more bananas than apples”

This would return

Check your eyes bud, there are more bananas than apples Because in the if condition, we were saying if there are more apples than bananas, do this. But there are more bananas than apples, so do that.

Nested Statements

A nested statement is exactly what it sounds like, a statement within another statement. To spare your eyes seeing an image of an  item within a nest, look to the example below.

apples = 23

bananas = 23

if apples > bananas:

answer = “There are more apples than bananas”

else:

if apples < bananas:

answer = “There are more bananas than apples”

else:

answer = “Stop counting fruit”

This would return

Stop counting fruit

Because apples and bananas were an equal count, and not greater than one another, hence the third answer returning.

Chained Statements

Chained statements are when you use if/elif/else flow controls, indented the same, to run code.

apples = 23

bananas = 23

if apples > bananas:

answer = “There are more apples than bananas”

elif apples >= bananas:

answer = “Why are you counting fruit? Get back to work”

else:

answer = “There are more bananas than apples”

This would return

Why are you counting fruit? Get back to work Because in the elif (python shortened version of “else if”) it says if apples are greater or equal to bananas, which yes, they are equal in number to one another.

While Loop

While loops are used to execute code, only if the condition met. This code is “looped” until the conditions are false, otherwise it runs infinitely.

Example:

(please note when commenting in Jupyter, precede all text with # symbol)

# while condition:

#executable code

(also important to note is that code below while must be indented with one press of the TAB key)

count = 0        (0 is assigned to the variable count)

while count <= 12:    (while 0 is less than or equal to 12)

print(count)   (then return count and run through the next line of code)

count = count + 1 (whatever count returns, add +1 each time)

(keep running through loop until count is equal to 12) Returns:

1

2

3

4

5

6

7

8

9

10

11

12

STOP

For Loop

A for loop is used to repeat a sequence of code.

Example below:

for i in range(4):

print(“This is a loop”)

Will return

This is a loop

This is a loop

This is a loop

This is a loop

Because for the variable i in the range 0,1,2,3 will print “This is a loop” four times.

Please remember that in Python, the very first index is 0 and not 1!

While you might count 1,2,3,4.

Python counts 0,1,2,3 which represents the range count of 4 places.

More lessons to come!