Department of CSE- Artificial Intelligence and Machine Learning
Python Programming
(BCC-402)
UNIT -5
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Syllabus
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Introduction to Matplotlib
What is Matplotlib?
A comprehensive library for creating static, animated, and interactive visualizations
in Python.
Why Use Matplotlib?
Widely used in data science for plotting data, simple interface, and highly
customizable.
Installing Matplotlib:
pip install matplotlib
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Basic Plotting with Matplotlib
Line Plots:
The simplest plot type, used to display data points connected by lines.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Customizing Plots in Matplotlib
Adding Titles and Labels:
Use plt.title(), plt.xlabel(), and plt.ylabel() to add context.
Adding Legends:
Use plt.legend() to label different data series.
Changing Line Styles and Colors:
plt.plot(x, y, color='red', linestyle='--', marker='o')
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Advanced Plotting Techniques
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Working with Subplots
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Plot Styles
Available Styles:
Use plt.style.available to list all styles.
Applying Styles:
plt.style.use('ggplot')
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Adding Annotations to Plots
Purpose of Annotations:
Highlight important data points or areas in the plot.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Creating Interactive Plots
Introduction to Widgets:
Using sliders, buttons, and other widgets to make plots interactive.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Saving Figures
Saving Plots as Images:
Use plt.savefig('filename.png') to save your plots.
Different Formats:
Save plots as PNG, PDF, SVG, etc.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Case Study: Visualizing Sales Data
Problem Statement:
Visualize the monthly sales data of a company.
Approach:
Use line plots, bar charts, and histograms to represent different aspects of the
data.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Numpy
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
What is Numpy?
Fundamental package for numerical computation in Python.
Key Features:
Provides support for arrays, matrices, and a large collection of mathematical
functions.
Installing Numpy:
pip install numpy
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Working with Numpy Arrays
Creating Arrays:
One-dimensional and multi-dimensional arrays.
Example Code:
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Basic Array Operations
Arithmetic Operations:
Add, subtract, multiply, and divide arrays.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Essential Numpy Functions
Mathematical Functions:
sin, cos, exp, log, etc.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Multi-dimensional Arrays
Content:
Creating and Reshaping Arrays:
Working with 2D and 3D arrays.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Understanding Broadcasting
What is Broadcasting?
The way Numpy handles operations between arrays of different shapes.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Linear Algebra with Numpy
Matrix Operations:
Matrix multiplication, transposition, inversion.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Statistical Operations in Numpy
Descriptive Statistics:
Mean, median, standard deviation.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Numpy Performance
Comparing Numpy Arrays with Python Lists:
Numpy arrays are implemented in C and are much faster for numerical operations
compared to Python lists.
Numpy operations are vectorized, meaning they can apply operations across
entire arrays without the need for explicit loops.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Numerical Simulations with Numpy
Problem Statement:
Simulate the motion of a projectile under the influence of gravity.
Approach:
Use Numpy to efficiently calculate the position of the projectile at each time step.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Pandas
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
What is Pandas?
A powerful library for data manipulation and analysis, built on top of Numpy.
Pandas introduces two primary data structures: Series and DataFrame.
Installing Pandas:
pip install pandas
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Dataframes
Creating DataFrames:
DataFrames are 2-dimensional, size-mutable, and heterogeneous tabular data
structures with labeled axes.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Data Importing
Reading from CSV:
One of the most common data formats. Pandas makes it easy to read CSV files
into DataFrames.
Pandas can also read Excel, SQL, JSON, and more.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Data Cleaning in Pandas
Handling Missing Data:
Missing data is common in real-world datasets. Pandas provides methods to handle
these cases.
Example Code:
df.fillna(0) # Replace missing values with 0
df.dropna() # Remove rows with missing values
Removing Duplicates:
Use df.drop_duplicates() to remove duplicate rows.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Transforming Data in Pandas
Applying Functions:
Use apply(), map(), and applymap() to apply functions to DataFrame elements.
Grouping Data:
Group data by specific columns using groupby().
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Merging and Joining DataFrames
Combine two DataFrames using merge() based on common columns or indices.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Working with Time Series Data
Pandas has strong support for working with time series data, including date
parsing, resampling, and shifting.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Visualization with Pandas
Pandas integrates well with Matplotlib, allowing you to generate plots directly from
DataFrames.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Integrating Pandas with Numpy
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Case Study: Data Analysis with Pandas
Problem Statement:
Analyze a dataset containing information about sales, customers, and products.
Approach:
Step 1: Load the dataset into a Pandas DataFrame.
Step 2: Perform data cleaning to handle missing values and correct data types.
Step 3: Analyze sales trends by grouping data by time, products, and regions.
Step 4: Visualize key metrics such as total sales, average sales per customer, and
sales distribution across different products.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Tkinter
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
What is Tkinter?
Tkinter is Python’s standard library for creating Graphical User Interfaces (GUIs).
Why Use Tkinter?
It’s easy to use and comes with Python out of the box. Suitable for simple GUI
applications.
Installing Tkinter:
Tkinter is included in most Python distributions. If not, install it using:
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Creating Your First Tkinter Window
Basic Window Setup:
Every Tkinter application needs to create a root window.
Tk() initializes the Tkinter window, and mainloop() keeps the window open until
manually closed.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Introduction to Tkinter Widgets
Common Widgets:
Labels: Display text or images.
Buttons: Trigger actions.
Entry: Accept user input.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Handling Events in Tkinter
What is Event Handling?
Events in Tkinter include button clicks, key presses, and mouse movements.
Binding Events:
Use bind() to associate an event with a function.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Managing Layout in Tkinter
Layout Options:
Pack: Simple layout, stacks widgets vertically or horizontally.
Grid: Aligns widgets in a grid, similar to an HTML table.
Place: Absolute positioning of widgets.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Creating Menus and Toolbars
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Using Dialogs in Tkinter
Common Dialogs:
Message boxes: Display alerts or information.
File dialogs: Open or save files.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Drawing with the Canvas Widget
What is the Canvas Widget?
A versatile widget for drawing shapes, lines, and images.
What are Widgets?
Widgets in Tkinter are the elements of GUI application which provides various
controls (such as Labels, Buttons, ComboBoxes, CheckBoxes, MenuBars,
RadioButtons and many more) to users to interact with the application.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Widgets Description
Label It is used to display text or image on the screen
Button It is used to add buttons to your application
Canvas It is used to draw pictures and others layouts like texts, graphics etc.
ComboBox It contains a down arrow to select from list of available options
It displays a number of options to the user as toggle buttons from which user can select any
CheckButton
number of options.
RadioButton It is used to implement one-of-many selection as it allows only one option to be selected
Entry It is used to input single line text entry from user
Frame It is used as container to hold and organize the widgets
Message It works same as that of label and refers to multi-line and non-editable text
Scale It is used to provide a graphical slider which allows to select any value from that scale
Scrollbar It is used to scroll down the contents. It provides a slide controller.
SpinBox It is allows user to select from given set of values
Text It allows user to edit multiline text and format the way it has to be displayed
Course Code: BCC
Menu 302 Course
It is used to create Name:
all kinds of menuPython Programming
used by an application
Department of CSE- Artificial Intelligence and Machine Learning
Creating and Managing Multiple Windows
Why Use Multiple Windows?
Separate different parts of your application into distinct windows.
Toplevel() creates a new top-level window independent of the main window.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Building a Simple Calculator
Objective:
Build a basic calculator with buttons for digits, operations, and a display for the
result.
Steps:
Design the layout using the grid method.
Implement button functions to perform arithmetic operations.
Update the display as buttons are pressed.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
IDEs
Here is the list of Top Python IDEs for 2024:
PyCharm
IDLE
Visual Studio Code
Atom
Sublime Text
Spyder
PyDev
Jupyter
Thonny
PyScripter
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
PyCharm
PyCharm is an Integrated Development Environment specifically for the development of applications in Python and Java.
Its functionalities include analysis of the code, a graphical debugger, etc. PyCharm helps to easily connect with a
database. It contains an integrated unit tester and other testing frameworks such as pytest, nose, doctest, tox, and trial.
PyCharm has got a lot of shortcuts to search and navigate different files and projects
IDLE
IDLE (Integrated Development and Learning Environment) is a dedicated platform or software to develop Python
applications. It is a full-fledged editor that is used to create, modify, and execute python code. It can also be used like the
Python Shell to execute single statements.
Visual Studio Code
Visual Studio Code referred to as VS Code is a lightweight source code editor used to develop projects in various
programming languages such as Python, HTML, C, C#, JavaScript, etc. The User Interface is well-documented and user-
friendly with custom themes.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Jupyter
Jupyter Notebook is another Integrated Development Environment specifically used for Data Science. It
can be executed on a local desktop or can be installed on a remote server. Installing Anaconda would
be the first step, to begin with in using Jupyter. It is an interactive computational environment.
Pandas, Matplotlib, Seaborn, Numpy, etc. could easily work with data and plot charts. The documents
created in Jupyter might contain visualizations, equations, narrative text, etc. Jupyter is open-sourced
and free to use.
PyScripter
PyScripter is a lightweight Integrated Development Platform designed specifically for Python. Integrated
Python Interpreter, Syntax highlighting, Integrated testing framework, regular expression testing, find
and replace options, debugging, etc. are the few features that stand out.
Course Code: BCC 302 Course Name: Python Programming
Department of CSE- Artificial Intelligence and Machine Learning
Thank You
Course Code: BCC 302 Course Name: Python Programming