Mathew K Analytics

Lesson 1 · Python For Time Series

01 Introduction to Python in Jupyter Notebook

In this lesson, we will learn how to use Python in Jupyter Notebooks, explore basic syntax, and work on a simple project. No experience required. All steps…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb
 

Welcome to Python Jupyter Notebook Setup!#

In this lesson, we will learn how to use Python in Jupyter Notebooks, explore basic syntax, and work on a simple project.

No experience required. All steps are explained for beginners.

What is a Jupyter Notebook?#

Jupyter Notebooks let you write and run code in small pieces, called 'cells'. They are great for learning, exploring, and sharing Python code.

You can add notes with text or use code cells to try things out.

# Before we begin, let us silence any warning messages.
import warnings
warnings.filterwarnings("ignore")
# Let us check Python's version. Notebooks often show the version being used.
import sys
print("Python version:", sys.version)
Python version: 3.12.1 (tags/v3.12.1:2305ca5, Dec  7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)]
# Let us try a simple print statement to see how output works.
print("Hello, world!")
Hello, world!

Code Cells and Text Cells#

In Jupyter, there are two main cell types:

  • Code cells: where you write and run Python code
  • Markdown cells: for explanations, like this one

You run a cell by clicking inside it and pressing Shift+Enter.

# Let us assign our first variable.
name = "Alice"
print("Nice to meet you,", name)
Nice to meet you, Alice
# Variables can be numbers too.
x = 3
y = 7
sum_xy = x + y
print("The sum is:", sum_xy)
The sum is: 10
# Let us try inputting your name yourself.
your_name = input("What is your name? ")
print("Hello,", your_name)
Hello, Jordan
 

Data Setup#

Let us load a small real-world dataset. We will use airline passenger numbers as an example. This time series data is popular for learning.

# Download and look at the airline passengers dataset.
import pandas as pd
url = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv"
df = pd.read_csv(url)
print("Shape of data:", df.shape)
df.head()
Shape of data: (144, 2)
Month Passengers
0 1949-01 112
1 1949-02 118
2 1949-03 132
3 1949-04 129
4 1949-05 121
# Let us make a quick plot of the data.
import matplotlib.pyplot as plt
plt.figure(figsize=(10,4))
plt.plot(df['Month'], df['Passengers'])
plt.xlabel('Month')
plt.ylabel('Number of Passengers')
plt.title('Airline Passengers Over Time')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
No description has been provided for this image

Try Making Your Own Data Table#

You can make a small table, called a dataframe, from a Python dictionary.

Here is a quick example.

# Make a dataframe from scratch.
sample_data = {
    "Name": ["Ava", "Ben", "Chloe"],
    "Score": [88, 92, 75]
}
df2 = pd.DataFrame(sample_data)
print("Sample table:")
df2
Sample table:
Name Score
0 Ava 88
1 Ben 92
2 Chloe 75
# Select just one column: the names.
names_column = df2["Name"]
print("Names:", list(names_column))
Names: ['Ava', 'Ben', 'Chloe']
# Add a new column to our small table.
df2["Passed"] = df2["Score"] >= 80
print(df2)
    Name  Score  Passed
0    Ava     88    True
1    Ben     92    True
2  Chloe     75   False
# Remove a column from the table.
df2 = df2.drop("Passed", axis=1)
print(df2)
    Name  Score
0    Ava     88
1    Ben     92
2  Chloe     75
# Basic error: try to access a column that does not exist.
try:
    print(df2["Grade"])
except KeyError as e:
    print("Oops! Column not found:", e)
    
Oops! Column not found: 'Grade'
# Sort the table by score from highest to lowest.
df_sorted = df2.sort_values(by="Score", ascending=False)
print(df_sorted)
    Name  Score
1    Ben     92
0    Ava     88
2  Chloe     75
# Filter the table: show only scores above 80.
passed_students = df2[df2["Score"] > 80]
print(passed_students)
  Name  Score
0  Ava     88
1  Ben     92
# Loop through the names in the table and greet each person.
for n in df2["Name"]:
    print("Hello,", n)
    
Hello, Ava
Hello, Ben
Hello, Chloe
# Challenge: Add a new row to the table using user input.
new_name = input("Enter a new student's name: ")
new_score = int(input("Enter the new score: "))
df2 = pd.concat([df2, pd.DataFrame({"Name": [new_name], "Score": [new_score]})], ignore_index=True)
print(df2)
    Name  Score
0    Ava     88
1    Ben     92
2  Chloe     75
3  Dylan     85
 

Recap#

  • We learned how to print messages, work with variables, and ask for input.
  • We loaded and looked at real data.
  • We made a small table and practiced sorting, filtering, and updating rows.

You can now use Jupyter Notebooks to explore Python and try new ideas.

Want more practice or ideas?#

  • Try creating your own small table about friends or pets.
  • Add a column for birthday months or favorite colors.
  • Try sorting or filtering by new rules.

If you enjoyed this video, be sure to like and subscribe for more beginner-friendly tutorials.

Happy coding!

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.