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

Unit 5

This document provides an overview of Python packages, including how to create them and their benefits for organizing code. It also introduces key libraries such as NumPy and Pandas for data manipulation and analysis, detailing their functionalities and installation processes. Additionally, it covers the basics of creating GUI applications using Tkinter, including various widgets and their usage.

Uploaded by

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

Unit 5

This document provides an overview of Python packages, including how to create them and their benefits for organizing code. It also introduces key libraries such as NumPy and Pandas for data manipulation and analysis, detailing their functionalities and installation processes. Additionally, it covers the basics of creating GUI applications using Tkinter, including various widgets and their usage.

Uploaded by

saileshmehra7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit -5

What is a Python Package?


Python Packages are a way to organize and structure your Python code into reusable components. Think
of it like a folder that contains related Python files (modules) that work together to provide certain
functionality. Packages help keep your code organized, make it easier to manage and maintain, and allow
you to share your code with others. They’re like a toolbox where you can store and organize your tools
(functions and classes) for easy access and reuse in different projects.

How to Create Package in Python?


Creating packages in Python allows you to organize your code into reusable and manageable modules.
Here’s a brief overview of how to create packages:

 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:

1. Create a directory named mypackage.


2. Inside mypackage, create two Python files: module1.py and module2.py.
3. Create an __init__.py file inside mypackage (it can be empty).
4. Add some code to the modules.
5. Finally, demonstrate how to import and use the modules from the 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.

Creating a Numpy 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.

Install Python NumPy

Numpy can be installed for Mac and Linux users via the following pip command:
pip install numpy

NumPy Array Indexing


Knowing the basics of NumPy array indexing is important for analyzing and manipulating the array
object. NumPy in Python offers many ways to do array indexing.

 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.

What is Python Pandas used for?


The Pandas library is generally used for data science, but have you wondered why? This is because the
Pandas library is used in conjunction with other libraries that are used for data science.

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.

Here is a list of things that we can do using Pandas.

 Data set cleaning, merging, and joining.


 Easy handling of missing data (represented as NaN) in floating point as well as non-floating point
data.
 Columns can be inserted and deleted from DataFrame and higher-dimensional objects.
 Powerful group by functionality for performing split-apply-combine operations on data sets.
 Data Visualization.

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.

Follow these steps to install Pandas:

Step 1: Type ‘cmd’ in the search box and open it.


Step 2: Locate the folder using the cd command where the python-pip file has been installed.
Step 3: After locating it, type the command:

pip install pandas

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

Data Structures in Pandas Library


Pandas generally provide two data structures for manipulating data. They are:

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

# Creating empty series


ser = pd.Series()
print("Pandas Series: ", ser)

# 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.

To create a Tkinter Python app, you follow these basic steps:

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.

Create First Tkinter GUI Application


There are two main methods used which the user needs to remember while creating the Python
application with GUI.

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.

The general syntax is:


w=Button(master, option=value)

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.

The general syntax is:


w=Entry(master, option=value)

4. CheckButton
To select any number of options by displaying a number of options to a user as toggle buttons.

The general syntax is:


w = CheckButton(master, option=value)

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.

The general syntax is:


w = RadioButton(master, option=value)

6. Listbox
It offers a list to the user from which the user can accept any number of options.

The general syntax is:


w = Listbox(master, option=value)

7. Scrollbar
It refers to the slide controller which will be used to implement listed widgets.

The general syntax is:


w = Scrollbar(master, option=value)

You might also like