Python short notes on modules and applications

ssuseree48e0 32 views 15 slides Jun 29, 2024
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

Python


Slide Content

Unit-5

Modules In programming, a module is a piece of software that has a specific functionality.  Modules refer to the Python file. In Python, large code is divided into small modules contains Python code like Python statements , classes, functions , variables, etc . A file with Python code is defined with extension.py For example: In Test.py , where the test is the module name.

Types of modules Built-in modules Built-in modules come with default Python installation Most significant advantages is its rich library support that contains lots of built-in modules.  Ex: datetime , os , math, sys, random, etc. User-defined modules user defines or create  modules We can create our own module, which contains classes, functions, variables, etc., as per our requirements.

Import Modules import statement is used to import the whole module Syntax: import module name i mport keyword imports both built-in and user-defined modules. Example: module-math

Import multiple modules T o use more than one module, then we can import multiple modules. Syntax: import module1,module2,.. moduleN

Import specific classes or functions from a module To import particular classes or functions, we can use the from...import statement. Imports individual attributes and methods directly into the program . Syntax: from < module_name > import <name(s)> Output: 120

Import with renaming a module To use the module with a different name, we can use from..import …as statement . Syntax Example: random.randrange ( start ,  stop ,  step )

Output: 41 Random Module Functions https ://pynative.com/python/random/

Import All Names Python Math Module Functions: https:// www.w3schools.com/python/module_math.asp https://docs.python.org/3/library/math.html

Create Module Creating a module in python is similar to writing a simple python script using the  . py   extension . Example:   To make a program for a calculator. There will be operations like addition, subtraction, multiplication, division, etc .

Example on creating module Calc.py

Use Python Modules created import  keyword to incorporate the module into our program. from  keyword is used to get only a few or specific function import all the functions using the asterisk(*) and we can simply mention the function name to get the results.

Pandas Pandas  is a powerful Python library that is specifically designed to work on data frames that have "relational" or "labeled" data . Its aim aligns with doing real-world data analysis using Python . Hence, this Python package works well for data manipulation, operating a dataset, exploring a data frame, data analysis, and machine learning-related tasks it simplifies the task related to data frames

Import datasets  - available in the form of spreadsheets, comma-separated values (CSV) files, and more. Data cleansing  - dealing with missing values and representing them as NaN , NA, or NaT. Size mutability  - columns can be added and removed from DataFrame and higher-dimensional objects. Data normalization  – normalize the data into a suitable format for analysis. Data alignment  - objects can be explicitly aligned to a set of labels. Intuitive merging and joining data sets – we can merge and join datasets. Reshaping and pivoting of datasets  – datasets can be reshaped and pivoted as per the need. Efficient manipulation and extraction  - manipulation and extraction of specific parts of extensive datasets using intelligent label-based slicing, indexing, and subsetting techniques. Statistical analysis  - to perform statistical operations on datasets. Data visualization  - Visualize datasets and uncover insights.

Applications The most common applications of Pandas are as follows: Data Cleaning : Pandas provides functionalities to clean messy data, deal with incomplete or inconsistent data, handle missing values, remove duplicates, and standardise formats to do effective data analysis. Data Exploration : Pandas easily summarise statistics, find trends, and visualise data using built-in plotting functions, Matplotlib , or Seaborn integration. Data Preparation : Pandas may pivot, melt, convert variables, and merge datasets based on common columns to prepare data for analysis. Data Analysis : Pandas supports descriptive statistics, time series analysis, group-by operations, and custom functions. Data Visualisation : Pandas itself has basic plotting capabilities; it integrates and supports data visualisation libraries like Matplotlib , Seaborn , and Plotly to create innovative visualisations . Time Series Analysis : Pandas supports  date/time   indexing , resampling, frequency conversion, and rolling statistics for time series data. Data Aggregation and Grouping :  Pandas groupby () function  lets you aggregate data and compute group-wise summary statistics or apply functions to groups. Data Input/Output : Pandas makes data input and export easy by reading and writing CSV, Excel, JSON, SQL databases, and more. Machine Learning : Pandas works well with Scikit -learn for data preparation, feature engineering, and model input data. Web Scraping : Pandas may be used with BeautifulSoup or Scrapy to parse and analyse structured web data for web scraping and data extraction. Financial Analysis : Pandas is commonly used in finance for stock market data analysis, financial indicator calculation, and portfolio optimisation . Text Data Analysis : Pandas' string manipulation, regular expressions, and text mining functions help analyse textual data. Experimental Data Analysis : Pandas makes manipulating and analysing large datasets, performing statistical tests, and visualising results easy.
Tags