UNIT–V: Python Packages & GUI Programming (Tkinter)
Detailed Notes (Theory + Programs + AKTU PYQs with Solutions)
Reference framework: Topics aligned with Wesley J. Chun, “Core Python Applications Programming”, 3rd Edition,
Pearson (2016).
These notes are written to provide [Link] students a comprehensive, exam-oriented and practical understanding of
Unit V topics: NumPy, Pandas, Matplotlib and Tkinter. Each concept includes theory, explanation, example programs,
common pitfalls, complexity/behaviour notes, and AKTU-style solved questions. The language is kept conversational
— as if prepared by faculty — and the material is mapped to typical university-level examinations.
1. NUMPY — Detailed Theory and Programs
1.1 Overview NumPy (Numerical Python) is a library providing the ndarray object, functions for fast operations on
arrays, linear algebra routines, random number capabilities, and tools for integrating C/C++/Fortran code. Key idea:
vectorized computation — apply operations to whole arrays without Python-level loops.
Example Program: NumPy basics
import numpy as np
# create arrays
a = [Link](1,13).reshape(3,4)
print("Array a:\n", a)
# slicing and views
row2 = a[1]
col3 = a[:,2]
print("Row 2:", row2)
print("Col 3:", col3)
# broadcasting
v = [Link]([1,0,1,0])
print("a + v =\n", a + v)
# linear solve
A = [Link]([[3,1],[1,2]])
b = [Link]([9,8])
x = [Link](A,b)
print("Solution x:", x)
2. PANDAS — Detailed Theory and Programs
2.1 Overview Pandas builds on NumPy and provides labeled, tabular data structures: Series (1-D) and DataFrame
(2-D). It is ideal for data cleaning, preparation, aggregation and quick analysis.
Example Program: Pandas basics
import pandas as pd
# create DataFrame
data = {'Roll':[1,2,3,4],
'Name':['Amit','Neha','Riya','Sam'],
'Marks':[85,90,78,92]}
df = [Link](data)
print([Link]())
# add Grade
df['Grade'] = df['Marks'].apply(lambda x: 'A' if x>=90 else ('B' if x>=80 else 'C'))
print(df)
# group and aggregate
print([Link]('Grade')['Marks'].agg(['count','mean']))
3. MATPLOTLIB — Detailed Theory and Programs
3.1 Overview [Link] provides a MATLAB-like plotting framework. Concepts: Figure, Axes, Artist (elements
drawn).
Example Program: Matplotlib basics
import [Link] as plt
subjects = ['Math','Physics','Chem','Eng']
marks = [78,85,72,90]
fig, ax = [Link]()
[Link](subjects, marks)
ax.set_xlabel('Subjects')
ax.set_ylabel('Marks')
ax.set_title('Marks by Subject')
[Link]()
4. TKINTER — Detailed Theory and Programs
4.1 Overview and architecture Tkinter is a thin object-oriented layer on top of Tcl/Tk. A Tkinter app typically creates a
root window, attaches widgets, and runs the mainloop to handle events.
Example Program: Tkinter Login Form (with validation)
import tkinter as tk
from tkinter import messagebox
def do_login():
u = entry_user.get().strip()
p = entry_pass.get().strip()
if u == 'admin' and p == 'pass123':
[Link]('Login', 'Welcome, admin!')
else:
[Link]('Login', 'Invalid credentials')
root = [Link]()
[Link]('Login Demo')
[Link](root, text='Username').grid(row=0, column=0, padx=5, pady=5)
[Link](root, text='Password').grid(row=1, column=0, padx=5, pady=5)
entry_user = [Link](root)
entry_pass = [Link](root, show='*')
entry_user.grid(row=0, column=1, padx=5, pady=5)
entry_pass.grid(row=1, column=1, padx=5, pady=5)
[Link](root, text='Login', command=do_login).grid(row=2, column=1, pady=10)
[Link]()
Advanced Example: Embed Matplotlib in Tkinter
import tkinter as tk
from [Link].backend_tkagg import FigureCanvasTkAgg
import [Link] as plt
import numpy as np
root = [Link]()
[Link]('Plot in Tkinter')
fig, ax = [Link]()
x = [Link](0,2*[Link],100)
[Link](x, [Link](x))
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack()
[Link]()
[Link]()
5. AKTU Previous Year Questions (Selected) — Detailed Answers
Q1 (2M) : What is NumPy? Mention two advantages. Answer: NumPy is a Python library for numerical computing that
provides the ndarray object and fast implementations of vectorized operations. Advantages: 1) Speed: ufuncs are
implemented in C and operate on whole arrays without Python loops. 2) Memory efficiency and convenience for linear
algebra and broadcasting operations.
Q2 (5M) : Create a DataFrame of 3 students and demonstrate selection, adding a grade column, and filtering students
with marks >= 85. Answer: Code: import pandas as pd df = [Link]({'Roll':[1,2,3], 'Name':['Amit','Neha','Riya'],
'Marks':[88,76,91]}) # add Grade df['Grade'] = df['Marks'].apply(lambda x: 'A' if x>=90 else ('B' if x>=80 else 'C')) #
selection top = df[df['Marks'] >= 85] print(top) Explanation: - We create DataFrame from dict; apply adds Grade
vectorized; boolean indexing filters rows efficiently.
Q3 (10M) : Write a Tkinter program for a basic calculator (add, sub, mul, div) with explanation. Answer: Approach: -
Use Entry widget for display, Buttons for digits and operations. - Maintain current expression in a StringVar and
evaluate on '=' press using safe parsing (avoid eval in production; use ast or operator). Code (conceptual outline
provided in notes). Explanation covers event binding, grid layout, input validation, and handling division by zero.
Reference & Further Reading
1. Wesley J. Chun, Core Python Applications Programming, 3rd Edition, Pearson, 2016 — recommended chapters:
modules & packages, data handling, GUI programming (Tkinter), and interfacing with C/Fortran for performance. 2.
NumPy and Pandas official documentation for API details and latest features. 3. Matplotlib documentation for plotting
details and examples.