0% found this document useful (0 votes)
18 views

Unit 7: Problem Solving Real World Programming Problems

Uploaded by

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

Unit 7: Problem Solving Real World Programming Problems

Uploaded by

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

UNIT 7

Problem Solving
• Real world programming problems
Weather Forecasting

• It is a process in which humans/machines


try to predict the conditions of the weather
for a given location.
• Now you might be wondering, how is
weather forecasting going to benefit
society?
• And why do I even need to build it?
Positive impacts of weather prediction

• Tourism is strongly affected by weather of


a certain place
• It strongly impacts the safety and operation
of transportation of all forms.
• We even decide our outfits based on the
weather conditions
• No doubt, it plays a major role in the
farming business.
Implementation using Python

Code Implementation
• We will be needing the requests library of
python, which we need to install in the
system using the pip command in the CMD
of your computer.

pip install requests


• Now, we need to make sure that we
introduce the application to the users
properly. Look at the code snippet below.
You can change the texts according to your
preferences.

print("\t\tWelcome to the Weather Forecaster!\n\n")


print("Just Enter the City you want the weather
report for and click on the button! It's that
simple!\n\n")
• Next, let’s take the input of the city name
from the user using the code snippet
below.
city_name = input("Enter the name of the City : ")

Now, we have the basic stuff ready and we are all set to move to
the major part of the project. We will start off by importing
the requests module that we just installed in our system earlier.

import requests
• We will create a function that will take the name of the city
the user enters and prints the report for us. Look at the code
of the function below.

To generate the weather report we will


make use of wttr. Now, you might be
wondering what wttr is? wttr is a
console-oriented weather forecast
service that comes with a number of
information representation ways to
make sure you get the weather data in
Finally, we just need to request the data from the wttr the best form possible.
link generated with the help of the requests module.
We are making use of the try-except block of Python
to make sure that we handle possible errors
beforehand.
Complete Code
Output
Some tricks for problem solving

Frequency in Python
• We often use loops to print the count of
each element in the list or so. But, we can
also use the counter from the collections
module to print out the count of each
element in the input data.
• For this you have to import
collections module offered by python
Let’s see how it works.
String Formatting

• You might have used format() function in python to format a


string. But, do you know without using those functions and
crazy methods, you can easily format a string?
List Iteration

• Commonly, we use loops to iterate over the list and print out
the elements in it. But, we will see how we can quickly iterate
over multiple lists and print out the elements in them.

This method can take 2 lists at a time. Not more than


that. It will throw error if you use more than two lists
Read File with Base Python

• Using pandas is the easiest way to read and write a CSV file.
But, you should also be aware of this method, where we use
base python to read and write CSV files.
Regular Expression (Regex)

• Regular expressions are the sequence of


patterns that are used to extract patterns in
data.
• You can also use this for matching and
searching patterns. Awesome right!
• Let’s see Regex in action.
Some Kaggle Datasets for problem solving practice

For Data Visualization


• Data visualization is one of the crucial parts of a data science
project. To understand data better, you need to visualize it to
uncover hidden insights.
• Python offers packages like Matplotlib, Seaborn and Pandas
to help you in visualizing data in a best way possible.

Population Data (1955-2020)


• This data contains the population information of the world countries
from year 1955 to 2020. You can use this data to visualize multiple
attributes. such as Population, Area, Coastline, Population density
and much more.
Using Pandas advanced plotting functions, you can easily play with this data.
For Data Analysis
• Pokémon Data
• Netflix Movies and TV Shows 2021

For text classification


• IMDB Dataset (you will enjoy using NLP)

For Recommender Systems


• Movie Lens Dataset
Some important built-in functions in python
for Data Science
Python Filter
• The filter function is a python’s built-in function that you can
use to iterate over the elements. It will return the items as
per the defined condition.
Python Map function
• Python Map() function will take another function and an iterable
object. It will return the elements which satisfy the given condition.
• The best thing about the map function is, it can be used without
Loops.
InsInstance
• Isinstance() is a built-in python function that you can use to
check if an element belongs to a specific class or not.
• This function will return a Boolean output.
Python Round function

• The Python round() function is used to round off the decimal


numbers. It will be of great use when you working with data
manipulation and calculations.
Python Zip

• Python zip() is another python built-in function that can be


used to assign different elements from different iterators.
• This function will provide output which includes a tuple.
Most Popular Python Packages

What is a package?
• Complex tasks are better solved step by step, one subtask at
a time. That's why programmers create and use modules, or
sets of related code saved in separate files and aimed at
solving specific tasks.
• When you have many different modules, you'll definitely want
to group and organize them. A Python package is a directory
of a collection of modules. Just as you organize your
computer files into folders and sub-folders, you can organize
modules into packages and sub-packages.
• Each package should contain a file named __init__.py. This
file usually includes the initialization code for the
corresponding package.
NumPy
• NumPy is the primary tool for scientific computing in Python.
• It combines the flexibility and simplicity of Python with the
speed of languages like C and Fortran.

NumPy is used for:


• Advanced array operations (e.g. add, multiply, slice, reshape,
index).
• Comprehensive mathematical functions.
• Random number generation.
• Linear algebra routines.
• Fourier transforms, etc.

• With NumPy, you are getting the computational power of compiled


code, while using accessible Python syntax. No wonder that there is
a huge ecosystem of Python packages and libraries drawing on the
power of NumPy. These include such popular packages as pandas,
Seaborn, SciPy, OpenCV, and others.
pandas

• If you work with tabular, time series, or matrix data, pandas


is your go-to Python package.
• It is known as a fast, efficient, and easy-to-use tool for data
analysis and manipulation.
• It works with data frame objects; a data frame is a dedicated
structure for two-dimensional data. Data frames have rows
and columns just like database tables or Excel spreadsheets.

Among other things, pandas can be used for:


• Reading/writing data from/to CSV and Excel files and SQL
databases.
• Reshaping and pivoting datasets.
• Slicing, indexing, and subsetting datasets.
• Aggregating and transforming data.
• Merging and joining datasets.
Matplotlib

• Matplotlib is the most common data exploration and


visualization library.
• You can use it to create basic graphs like line plots,
histograms, scatter plots, bar charts, and pie charts.
• You can also create animated and interactive visualizations
with this library.
• Matplotlib is the foundation of every other visualization
library.
• The library offers a great deal of flexibility with regards to
formatting and styling plots.
• You can freely choose how to display labels, grids, legends,
etc. However, to create complex and visually appealing plots,
you'll need to write quite a lot of code.
Seaborn

• Seaborn is a high-level interface for drawing attractive statistical


graphics with just a few lines of code.
• We'll use the famous IRIS FLOWER DATASET in our example. For
those not familiar with it, this dataset includes four features – the
length and the width of the sepals and petals – for three species of
iris (Iris setosa, Iris virginica, and Iris versicolor). We want to see
how these four features relate to one another depending on the iris
species.
• Here's how seaborn's pairplot function solves this task. Notice that
you can create a complex and visually appealing plot with just three
lines of code:
you can easily create
complex heatmaps, violin
plots, joint plots, multi-
plot grids, and many
other types of plots with
this library.
scikit-learn

• scikit-learn is an efficient and beginner-friendly tool for


predictive data analysis. Among other things, you can use
scikit-learn to:
• Identify which category an object is likely to belong to (used
in fraud detection, image recognition, cancer detection, etc.).
• Predict a continuous variable based on available features
(used in predicting house prices and inflation).
• Group similar objects into clusters (used in customer
segmentation, social network analysis, etc.).
• scikit-learn makes machine learning with Python accessible
to people with minimal programming experience. With just a
few lines of code, you can model your data using algorithms
like random forest, support vector machines (SVM), k-means,
spectral clustering, and more.
Requests

• This library is designed to make HTTP requests with Python


more responsive and user friendly. The intuitive JSON method
offered by Requests helps you avoid manually adding query
strings to URLs.

With Requests, you can:


• Customize, inspect, authorize, and configure HTTP requests.
• Add parameters, headers, and multi-part files.
• Decompress data automatically.
• Upload multiple files at the same time.
urllib3

• urllib3 is another user-friendly HTTP client for Python. It is


currently THE MOST DOWNLOADED PYPI PACKAGE, and it
powers Requests and some other popular Python packages.

urllib3 provides many critical features missing from the


standard libraries:
• Thread safety.
• Connection pooling.
• Retrying requests.
• Dealing with HTTP redirects.
• Full test coverage.
NLTK

• Natural Language Toolkit (NLTK) is one of the leading Python


platforms for processing language data.
• It is a set of language processing libraries and programs that
provide a toolkit for:
• Classification.
• Tokenization.
• Stemming.
• Tagging.
• Parsing.
• Semantic reasoning.
• NLTK is a go-to tool for computational linguistics in Python.
It's highly valued by linguists, engineers, researchers, and
industry users.
Pillow

• If you work with image data, make sure to check out the
Pillow package. It is a fork of PIL (Python Image Library) that
developed into an easy-to-use and efficient tool for image
manipulation in Python.

With Pillow, you can:


• Open and save images of different file types (JPEG, PNG, GIF,
PDF, etc.).
• Create thumbnails for images.
• Use a collection of image filters (e.g. SMOOTH, BLUR,
SHARPEN).
• This is a great image manipulation tool for beginners, and it
has fairly powerful image processing capabilities.
pytest

• This package provides a variety of modules for testing new


code, including small unit tests and complex functional tests
for applications and libraries.

• Simple syntax and an extensive feature set make pytest one


of the most-loved Python packages among programmers.

• This test automation framework provides:


• Built-in support for test discovery.
• Modular fixtures for test setup (e.g. setting up the
database connection, URL, input data).
• Rich plugin architecture (315+ external plugins).
• Built-in unit tests.
THANKS

You might also like