Unit 5
Unit 5
Create a Directory: Start by creating a directory (folder) for your package. This directory will
serve as the root of your package structure.
Add Modules: Within the package directory, you can add Python files (modules) containing your
code. Each module should represent a distinct functionality or component of your package.
Init File: Include an __init__.py file in the package directory. This file can be empty or can
contain an initialization code for your package. It signals to Python that the directory should be
treated as a package.
Subpackages: You can create sub-packages within your package by adding additional directories
containing modules, along with their own __init__.py files.
Importing: To use modules from your package, import them into your Python scripts using dot
notation. For example, if you have a module named module1.py inside a package named
mypackage, you would import its function like this: from mypackage.module1 import greet.
Distribution: If you want to distribute your package for others to use, you can create a setup.py
file using Python’s setuptools library. This file defines metadata about your package and specifies
how it should be installed.
Code Example
Here’s a basic code sample demonstrating how to create a simple Python package:
Python Numpy
Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional
array object, and tools for working with these arrays. It is the fundamental package for scientific
computing with Python.
Besides its obvious scientific uses, Numpy can also be used as an efficient multi-dimensional container of
generic data.
Arrays in Numpy
Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of
positive integers. In Numpy, number of dimensions of the array is called rank of the array.A tuple of
integers giving the size of the array along each dimension is known as shape of the array. An array class
in Numpy is called as ndarray. Elements in Numpy arrays are accessed by using square brackets and can
be initialized by using nested Python Lists.
It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers.
In NumPy, dimensions are called axes. The number of axes is rank.
NumPy’s array class is called ndarray. It is also known by the alias array.
Arrays in Numpy can be created by multiple ways, with various number of Ranks, defining the size of the
Array. Arrays can also be created with the use of various data types such as lists, tuples, etc. The type of
the resultant array is deduced from the type of the elements in the sequences.
Numpy can be installed for Mac and Linux users via the following pip command:
pip install numpy
Slicing: Just like lists in Python, NumPy arrays can be sliced. As arrays can be multidimensional,
you need to specify a slice for each dimension of the array.
Integer array indexing: In this method, lists are passed for indexing for each dimension. One-to-
one mapping of corresponding elements is done to construct a new arbitrary array.
Boolean array indexing: This method is used when we want to pick elements from the array
which satisfy some condition.
Pandas Introduction
Pandas is a powerful and open-source Python library. The Pandas library is used for data manipulation
and analysis. Pandas consist of data structures and functions to perform efficient operations on data.
Pandas is a powerful and versatile library that simplifies the tasks of data manipulation in Python.
Pandas is well-suited for working with tabular data, such as spreadsheets or SQL tables.
The Pandas library is an essential tool for data analysts, scientists, and engineers working with structured
data in Python.
It is built on top of the NumPy library which means that a lot of the structures of NumPy are used or
replicated in Pandas.
The data produced by Pandas is often used as input for plotting functions in Matplotlib.
You must be wondering, Why should you use the Pandas Library. Python’s Pandas library is the best tool
to analyze, clean, and manipulate data.
Installing Pandas
The first step in working with Pandas is to ensure whether it is installed in the system or not. If not, then
we need to install it on our system using the pip command.
Importing Pandas
After the Pandas have been installed in the system, you need to import the library. This module is
generally imported as follows:
import pandas as pd
1. Series
2. DataFrame
Pandas Series
A Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string,
float, Python objects, etc.). The axis labels are collectively called indexes.
The Pandas Series is nothing but a column in an Excel sheet. Labels need not be unique but must be of a
hashable type.
The object supports both integer and label-based indexing and provides a host of methods for performing
operations involving the index.
Creating a Series
Pandas Series is created by loading the datasets from existing storage (which can be a SQL database, a
CSV file, or an Excel file).
Pandas Series can be created from lists, dictionaries, scalar values, etc.
import pandas as pd
import numpy as np
# simple array
data = np.array([‘p’,’y’,’t’,’h’,’o’,’n’])
ser = pd.Series(data)
print("Pandas Series:\n", ser)
Output
Pandas Series: Series([], dtype: float64)
Pandas Series:
0 p
1 y
2 t
3 h
4 o
5 n
dtype: object
Pandas DataFrame
Pandas DataFrame is a two-dimensional data structure with labeled axes (rows and columns).
Creating DataFrame
Pandas DataFrame is created by loading the datasets from existing storage (which can be a SQL database,
a CSV file, or an Excel file).
Pandas DataFrame can be created from lists, dictionaries, a list of dictionaries, etc.
Python Tkinter
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI
methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI
toolkit shipped with Python. Python Tkinter is the fastest and easiest way to create GUI applications.
Creating a GUI using Tkinter is an easy task.
1. Import the tkinter module: This is done just like importing any other module in Python. Note
that in Python 2.x, the module is named ‘Tkinter’, while in Python 3.x, it is named ‘tkinter’.
2. Create the main window (container): The main window serves as the container for all the GUI
elements you’ll add later.
3. Add widgets to the main window: You can add any number of widgets like buttons, labels,
entry fields, etc., to the main window to design the interface as desired.
4. Apply event triggers to the widgets: You can attach event triggers to the widgets to define how
they respond to user interactions.
Tk()
To create a main window, tkinter offers a method ‘Tk(screenName=None, baseName=None,
className=’Tk’, useTk=1)’. To change the name of the window, you can change the className to the
desired one. The basic code used to create the main window of the application is:
mainloop()
There is a method known by the name mainloop() is used when your application is ready to run.
mainloop() is an infinite loop used to run the application, wait for an event to occur, and process the event
as long as the window is not closed.
Example
import tkinter
m = tkinter.Tk()
'''
widgets are added here
'''
m.mainloop()
Ouput:
Tkinter Widget
There are a number of widgets which you can put in your tkinter application. Some of the major widgets
are explained below:
1. Label
It refers to the display box where you can put any text or image which can be updated any time as per the
code.
The general syntax is:
w=Label(master, option=value) {master is the parameter used to represent the parent window.}
Example
from tkinter import *
root = Tk()
w = Label(root, text=’Python Tkinter’')
w.pack()
root.mainloop()
Ouput:
2. Button
To add a button in your application, this widget is used.
master is the parameter used to represent the parent window. There are number of options which are used
to change the format of the Buttons. Number of options can be passed as parameters separated by
commas.
3. Entry
It is used to input the single line text entry from the user.. For multi-line text input, Text widget is used.
4. CheckButton
To select any number of options by displaying a number of options to a user as toggle buttons.
5. RadioButton
It is used to offer multi-choice option to the user. It offers several options to the user and the user has to
choose one option.
6. Listbox
It offers a list to the user from which the user can accept any number of options.
7. Scrollbar
It refers to the slide controller which will be used to implement listed widgets.