0% found this document useful (0 votes)
2 views44 pages

Intro To Python Updated

Uploaded by

marvboadu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views44 pages

Intro To Python Updated

Uploaded by

marvboadu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Python Using Jupyter Notebook

Dr. Joseph Dadzie

Department of Computer Science


Accra Technical University

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 1 / 44


Course Objectives

At the end of this lecture, students should be able to:


1 Explain the meaning of Python programming.
2 Understand the importance of Jupyter Notebook.
3 Install Python and Jupyter Notebook.
4 Execute Python programs in Jupyter Notebook.
5 Perform simple computations using Python.
6 Use variables and data types.
7 Accept user input and display output.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 2 / 44


Introduction to Python

Python is a high-level programming language.


Python is simple, readable, and beginner-friendly.
It is widely used in:
Data Science
Artificial Intelligence
Machine Learning
Web Development
Scientific Computing
Automation
Python was developed by Guido van Rossum in 1991.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 3 / 44


Why Learn Python?

Advantages of Python
Easy syntax and readability
Large community support
Portable across operating systems
Large collection of libraries
Suitable for research and industry

Industrial Applications
Python is heavily used in Artificial Intelligence, Robotics, Finance, Healthcare, and Engineering.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 4 / 44


What is Jupyter Notebook?

Jupyter Notebook is an interactive programming environment.


It allows users to combine:
1 Program codes
2 Text explanations
3 Mathematical equations
4 Graphs and charts
Jupyter Notebook is popular in:
Data Science
Machine Learning
Academic Research
Teaching and Learning

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 5 / 44


Features of Jupyter Notebook

1 Interactive execution of programs


2 Supports multiple programming languages
3 Easy visualization of results
4 Supports Markdown documentation
5 Excellent for scientific computing

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 6 / 44


Installing Python and Jupyter

Step 1: Download Python


Visit: [Link]
Download the latest version.

Step 2: Install Python


Run the installer.
Tick: Add Python to PATH

Step 3: Install Jupyter Notebook


1 pip install notebook

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 7 / 44


Starting Jupyter Notebook

Using Command Prompt


1 jupyter notebook

Alternative Method
Open Anaconda Navigator
Click on Jupyter Notebook

Outcome
Jupyter opens in a browser window.
Users can create notebooks and execute programs.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 8 / 44


Structure of Jupyter Notebook
A Jupyter Notebook contains:
1 Code Cells

2 Markdown Cells

Code Cells
Used for writing and executing Python programs.

Markdown Cells
Used for writing notes, headings, and explanations.

Shortcut to Execute a Cell:

Shift + Enter
J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 9 / 44
First Python Program

1 print ( " Welcome to Python Programming " )

Output

Welcome to Python Programming

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 10 / 44


Python as a Calculator
Python can perform arithmetic operations.
1 2 + 3
2 10 - 4
3 5 * 6
4 20 / 5
5 2 ** 3

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
** Power
J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 11 / 44
Variables in Python

Variables are used to store data values.


1 x = 10
2 y = 5
3
4 sum = x + y
5
6 print ( sum )

Output

15

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 12 / 44


Rules for Naming Variables

1 Variable names must start with a letter or underscore.


2 Variable names cannot start with a number.
3 Spaces are not allowed.
4 Python distinguishes between uppercase and lowercase letters.

Valid Names Invalid Names


age 2age
student name student name
score1 score@

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 13 / 44


Data Types in Python

Data Type Example


Integer 10
Float 3.14
String ”Python”
Boolean True

1 x = 10
2 y = 3.14
3 name = " Joseph "
4 status = True

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 14 / 44


Input and Output

Python can accept user input.


1 name = input ( " Enter your name : " )
2
3 print ( " Welcome " , name )

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 15 / 44


Simple Example: Area of Rectangle

1 length = float ( input ( " Enter length : " ) )


2 width = float ( input ( " Enter width : " ) )
3
4 area = length * width
5
6 print ( " Area = " , area )

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 16 / 44


Common Jupyter Shortcuts

Shortcut Function
Shift + Enter Run current cell
A Insert cell above
B Insert cell below
DD Delete cell
M Convert to Markdown
Y Convert to Code

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 17 / 44


Applications of Python

1 Data Science
2 Artificial Intelligence
3 Machine Learning
4 Engineering Simulations
5 Financial Analysis
6 Scientific Research

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 18 / 44


Advantages of Jupyter Notebook

Interactive learning environment


Easy experimentation
Supports visualization
Combines text and codes
Excellent for teaching and research

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 19 / 44


Summary

Python is easy to learn and widely used.


Jupyter Notebook provides an interactive coding environment.
Python supports calculations, variables, and user interaction.
Jupyter Notebook is important in Data Science and AI.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 20 / 44


Exercises

1 Write a program to add two numbers.


2 Write a program to calculate the area of a circle.
3 Create variables for your name, age, and department.
4 Write a Python program to convert temperature from Celsius to Fahrenheit.
5 Execute all programs using Jupyter Notebook.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 21 / 44


Main Components of the Notebook Interface

The Jupyter Notebook interface consists of:


Notebook name: identifies the current notebook;
Menu bar: provides commands such as File, Edit, View and Run;
Toolbar: provides quick access to common commands;
Cell area: the working space for code and text;
Kernel: the computational engine that executes Python code.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 22 / 44


Cells in Jupyter Notebook

A cell is the basic unit of work in Jupyter Notebook.

Code Cell
A Code cell is used to write and execute Python commands.

Markdown Cell
A Markdown cell is used to write explanations, headings, lists and formatted notes.

To run a cell, press:


Shift + Enter

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 23 / 44


Executing Python Code

Example of a simple Python command:


1 print ( " Welcome to Jupyter Notebook " )

Expected output:
1 Welcome to Jupyter Notebook

This demonstrates how Python instructions are entered and executed in a notebook cell.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 24 / 44


Basic Arithmetic Operations

Jupyter Notebook may also be used for numerical computation.


1 5 + 3
2 10 - 4
3 6 * 7
4 20 / 5
5 2 ** 3

These commands illustrate addition, subtraction, multiplication, division and exponentiation.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 25 / 44


Variables in Python

Variables are used to store values for later use.


1 name = " Joseph "
2 age = 25
3
4 print ( name )
5 print ( age )

In Data Science, variables are used to store data values, labels, models and results.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 26 / 44


Markdown for Academic Documentation

Markdown cells allow users to document computational work.


1 # My First Jupyter Notebook
2
3 This notebook introduces basic Python commands .
4
5 # # Learning Points
6 - Creating a notebook
7 - Running Python code
8 - Writing Markdown notes

Good documentation improves clarity, interpretation and reproducibility.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 27 / 44


Python Libraries: Conceptual Introduction

A Python library is a collection of pre-written functions, classes and modules designed to


perform specific tasks.
Libraries help users to:
reduce programming effort;
perform complex computations efficiently;
analyse and visualise data;
build statistical and machine learning models;
support reproducible computational workflows.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 28 / 44


Importance of Libraries in Data Science

Python libraries are central to modern Data Science because they support:
data collection and importation;
data cleaning and transformation;
exploratory data analysis;
statistical analysis;
data visualisation;
machine learning and prediction.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 29 / 44


Selected Python Libraries for Data Science

Library Primary Function


NumPy Numerical computation
Pandas Data manipulation and analysis
Matplotlib Graphical visualisation
Seaborn Statistical visualisation
SciPy Scientific computing
Scikit-learn Machine learning
Statsmodels Statistical modelling
TensorFlow Deep learning
Keras Neural network modelling
OpenCV Image processing

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 30 / 44


NumPy: Numerical Python

NumPy provides support for arrays, matrices and numerical operations.

1 import numpy as np
2
3 values = np . array ([2 , 4 , 6 , 8])
4
5 print ( values . mean () )
6 print ( values . sum () )

NumPy is useful in numerical analysis, simulation and matrix-based computation.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 31 / 44


Pandas: Data Analysis

Pandas is used for handling structured data.

1 import pandas as pd
2
3 data = {
4 " Name " : [ " Ama " , " Kojo " , " Yaw " ] ,
5 " Score " : [75 , 82 , 69]
6 }
7
8 df = pd . DataFrame ( data )
9 print ( df )

Pandas is commonly used for tables, spreadsheets and dataset analysis.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 32 / 44


Matplotlib: Data Visualisation

Matplotlib is used to produce graphs and charts.


1 import matplotlib . pyplot as plt
2
3 x = [1 , 2 , 3 , 4]
4 y = [2 , 4 , 6 , 8]
5
6 plt . plot (x , y )
7 plt . title ( " Simple Line Graph " )
8 plt . show ()

Visualisation helps reveal patterns, trends and relationships in data.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 33 / 44


Seaborn: Statistical Visualisation

Seaborn is built on Matplotlib and is useful for statistical graphics.

1 import seaborn as sns

It supports:
distribution plots;
correlation heatmaps;
regression plots;
categorical data visualisation.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 34 / 44


Scikit-learn: Machine Learning

Scikit-learn provides tools for machine learning.


It supports:
classification;
regression;
clustering;
dimensionality reduction;
model evaluation.
1 from sklearn . linear_model import LinearRegression

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 35 / 44


SciPy: Scientific Computing

SciPy extends Python for scientific and technical computing.


1 import scipy

It is useful for:
optimisation;
integration;
statistical functions;
signal processing.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 36 / 44


Statsmodels: Statistical Modelling

Statsmodels is used for formal statistical analysis.


1 import statsmodels . api as sm

Applications include:
regression analysis;
analysis of variance;
hypothesis testing;
time series modelling.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 37 / 44


Deep Learning Libraries

TensorFlow and Keras are widely used for deep learning.


They are applied in:
neural networks;
image recognition;
natural language processing;
speech recognition;
recommendation systems.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 38 / 44


Installing Python Packages

Python packages may be installed using pip.


1 pip install numpy
2 pip install pandas
3 pip install matplotlib

Within Jupyter Notebook, the command may be written as:


1 ! pip install numpy

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 39 / 44


Data Science Workflow

A standard Data Science workflow involves:


1 Problem definition;
2 Data collection;
3 Data cleaning;
4 Exploratory data analysis;
5 Data visualisation;
6 Model development;
7 Model evaluation;
8 Interpretation and decision-making.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 40 / 44


Practical Exercise

Students should complete the following in Jupyter Notebook:


1 Create a new Python notebook.
2 Import NumPy, Pandas and Matplotlib.
3 Create a simple Pandas DataFrame.
4 Compute the mean of a numerical variable.
5 Produce a simple line graph.
1 import numpy as np
2 import pandas as pd
3 import matplotlib . pyplot as plt

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 41 / 44


Common Beginner Errors

Common errors include:


forgetting to run a cell;
writing Python code in a Markdown cell;
misspelling library names;
forgetting quotation marks around text;
using a package before importing it;
failing to save the notebook.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 42 / 44


Example of an Import Error

Incorrect command:
1 import panda

Correct command:
1 import pandas as pd

The correct library name is pandas, not panda.

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 43 / 44


Questions and Discussion

J. Dadzie (ATU) Introduction to Python Using Jupyter Notebook 44 / 44

You might also like