0 ratings0% found this document useful (0 votes) 360 views90 pagesPython Book by Example
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
eae) eA ae aiee ee
A ate
lala) :
a WwerlciathH S 1
ral 5 rg 1
-t3 ATET _Ee-o at
ase) fe 7, ee op oe ee ae
Eanor ZT
es
(aos
OL
9 TO 10
= 411 APh- he
ine O1LITASTzonn
=] aS =a
eal
indi eee
=r etee a ie
iS Aa
101 ml a
YPY or =
Noor e Pu :
TRE
‘aie 4 oP
=]
oe Peels. meal N'A =T-1a Re loxore!Table of Contents
Chapter 1: Getting Started with Python
« 1.1 Introduction to Python
* 1.2 Setting Up Your Python Environment
* 1.3 Your First Python Program
* 1.4 Understanding Python Syntax and Basics
Chapter 2: Variables and Data Types
* 2.1 Working with Numbers
* 2.2 Managing Text with Strings
* 2.3 Using Lists to Store Data
* 2.4 Exploring Tuples and Sets
Chapter 3: Control Structures
+ 3.1 Making Decisions with If-Statements
+ 3.2 Looping with For and While Loops
+ 3.3 Comprehensions for Efficient Data Handling
Chapter 4: Functions and Modules
* 4.1 Defining and Calling Functions
* 4.2 Parameters, Arguments, and Return Values
+ 4.3 Organizing Code with Modules and Packages
Chapter 5: Exception Handling* 5.1 Understanding Exceptions
* 5.2 Handling Exceptions with Try-Except
* 5.3 Raising Custom Exceptions
Chapter 6: Working with Files
+ 6.1 Reading from and Writing to Files
+ 6.2 Handling Different File Formats (Text, CSV, JSON)
* 6.3 Managing File Contexts with With-Statement
Chapter 7: Object-Oriented Programming
+ 7.1 Understanding Classes and Objects
« 7.2 Building Custom Classes
* 7.3 Special Methods and Inheritance
Chapter 8: Advanced Python Concepts
* 8.1 Iterators and Generators
* 8.2 Decorators and Context Managers
+ 8.3 Lambda Functions and Functional Programming
Chapter 9: Data Handling with Pandas
* 9.1 Introduction to Pandas
* 9.2 DataFrames and Series
* 9.3 Data Manipulation and Analysis
Chapter 10: Data Visualization
+ 10.1 Visualizing Data with Matplotlib+ 10.2 Plotting with Seaborn
* 10.3 Advanced Visualization Techniques
Chapter 11: Python for Web Development
+ 11.1 Exploring Web Frameworks: Flask and Django
+ 11.2 Building a Simple Web Application
* 11.3 Working with APIs
Chapter 12: Python for Networking and Security
* 12.1 Scripting for Network Administration
* 12.2 Basics of Network Security in Python
* 12.3 Writing Secure Python Code
Chapter 13: Automating Everyday Tasks
* 13.1 Scripting for File System Management
* 13.2 Automating Email and Text Messages
* 13.3 Scheduling Tasks with Python
Chapter 14: Testing Your Code
« 14.1 Introduction to Unit Testing
* 14.2 Using the unittest Framework
* 14.3 Test-Driven Development (TDD)
Chapter 15: Concurrent and Parallel Programming
+ 15.1 Understanding Concurrency and Parallelism
* 15.2 Threading and Multiprocessing* 15.3 Asyncio for Asynchronous Programming
Chapter 16: Python for Scientific and Numeric Applications
* 16.1 Using NumPy for Numeric Data
* 16.2 Scientific Computing with SciPy
* 16.3 Introduction to Machine Learning with Scikit-Learn
Chapter 17: Working with Databases
* 17.1 Basics of Database Operations
* 17.2 Using SQLite with Python
* 17.3 Advanced Database Management with SQLAlchemy
Chapter 18: Enhancing Performance
+ 18.1 Profiling and Optimizing Python Code
* 18.2 Using C Extensions for Performance
* 18.3 Leveraging Cython
Chapter 19: Packaging and Distributing Python Code
* 19.1 Setting Up a Proper Python Environment
* 19.2 Creating Packages
* 19.3 Distributing Packages with Pip and Conda
Chapter 20: Trends and Future Directions in Python
* 20.1 The Future of Python Programming
* 20.2 Integrating Python with Other Languages
+ 20.3 Exploring New Python Libraries and FrameworksChapter 1: Getting Started
with Python
1.1 Introduction to Python
Python is a high-level, interpreted programming language known for
its clear syntax, readability, and versatility. It supports multiple
programming paradigms, including procedural, object-oriented, and
functional programming. Python is widely used in various fields such
as web development, data analysis, artificial intelligence, scientific
computing, and more. The language’s design and module
architecture encourage code reuse and modularity.
1.2 Setting Up Your Python Environment
To start programming in Python, you need to set up your
development environment. Here’s how to install Python and set up a
basic development environment:
Windows:
1. Download the Python installer from the official Python website .
2. Run the installer. Ensure to check the box that says “Add Python
3.x to PATH” before clicking “Install Now”.
3. After installation, open Command Prompt and type to check if
it’s installed correctly.
macOS:
1. You can install Python using Homebrew (a package manager for
macOS). First, install Homebrew by running the following in the
Terminal:bin/bash -c "$(curl -fsSi.
-com/Homebrew/instal1/HEAD/insta
https: //raw.githubuserconte:
1l.sh)"
2. Then, install Python by typing:
brew install python
Linux:
1. Most Linux distributions come with Python pre-installed. You
can check the version by typing in the terminal.
2. If it’s not installed, you can install it using your package
manager. For Ubuntu, you would use:
sudo apt-get
sude apt-get in
Integrated Development Environment (IDE): For beginners,
it’s helpful to use an IDE such as PyCharm, VS Code, or Jupyter
Notebook. These tools provide features like code completion, syntax
highlighting, and debugging that help you write code more
efficiently.
1.3 Your First Python ProgramLet’s write your first Python program. Python programs are often
simple to write and understand. Here's a classic “Hello, World!”
example:
1. Open your text editor or IDE.
2. Type the following code:
print ("Hello, World!")
1. Save the file as .
2. Run the script from your command line or terminal by typing:
python hel
You should see printed to the console. This example demonstrates
how to print text in Python.
1.4 Understanding Python Syntax and
Basics
Python syntax is known for being clean and readable. Here are some
basics:
« Indentation: Python uses indentation to define blocks of code.
The standard practice is to use 4 spaces per indentation level.f name:
"+ name +
print ("Hello,
else:
print ("Hello, World!")
* Variables and Types: Python is dynamically typed, so you
don’t need to declare variables before using them. Here’s how
you can use variables:
x # An integer assignment
y = 3.14 # A floating-point number
@ = "Alice" # A string
+ Comments: Use the hash mark for comments, which explain
what the code does.
# This is a comment
print("Hello, World!") # This prints a message
* Basic Operators: Python supports various operators, such as
arithmetic, comparison, and logical operators.
# Arithmetic Operators
print(S + 3) # Addition
print(5 - 3) # Subtraction
print(S * 3) 4 Multiplicationprint(S / 3) # Division
on Operators
print(S > 3) # Greater than
print(S <3) # Less than
al Operat
print (True and False) # Logical AND
print (True or Fals # Logical oR
By understanding these basics, you'll be well on your way to writing
effective Python programs. The subsequent chapters will delve
deeper into Python’s features and libraries, allowing you to explore
more complex projects and applications.Chapter 2: Variables and
Data Types
2.1 Working with Numbers
Python supports several types of numbers, primarily integers and
floating-point numbers. Here’s how you can work with these:
+ Integers: These are whole numbers which can be positive or
negative.
¢ Floating-point numbers: These represent real numbers and
are written with a decimal point.
Example:
tions
print(x + y) # Addition; outputs 13.14
print(x - y) # Subtraction; outputs 6.86
print(x / y) # Division; outpu
t
print(x * y) # Multiplicat
t
print (x % 3)
lus; outputs
x by 3)
+ Complex numbers: These are of the form , where is the real
part and is the imaginary part.z= 2435
print(z.real) # Outputs 2.0
print (z.imag) # Outputs 3.0
2.2 Managing Text with Strings
Strings in Python are sequences of characters used for storing and
manipulating text. They can be enclosed in single quotes (’’) or
double quotes (” “).
Example:
5 = "Hello, World!"
# Accessing characters
print(s{0]) # Outputs 'H!
# Slicing strings
print(s{1:5]) # Outputs ‘ello!
# String methods
print(s.lower()) # Outputs ‘hello, world!
print (s.upper()) # Outpu 10, WORLD!
print (s.replace('World', 'Python')) # Outputs ‘Hello, Python!'
* String Concatenation and Formatting:
first_name = "John"
last_name = "Doe"full_name = first_name +" " # Concatenation
print (full_name) # Outp
print (info) # Outputs '
2.3 Using Lists to Store Data
Lists are ordered collections that are mutable and can contain mixed
data types.
Example:
[1, 2, 3, "Python", 3.14
# Access elements
print (my_list[3]) # Outputs ‘Python’
# Add elements
nd ("new item")
list) Outputs [1, 2, 3, 'Python', 3.14, ‘new item']
nt (my_list[1:4]) # Outputs [2, 3, 'Python’
comprehensions
[aren
or x in range (10))
print (squares) # Outputs [0, 1, 4, 9, 16, 25, 36, 49, 64, 81
2.4 Exploring Tuples and Sets+ Tuples: These are similar to lists but are immutable. They are
created by placing comma-separated values inside parentheses.
Example:
my_tuple = (1, 2, 3, "eython")
print (my_tuple(1]) # Outputs 2
# Trying to modify a tuple will result
# my_tuple[1] = 10 # This would raise a
+ Sets: Sets are unordered collections of unique elements. They
are defined by values separated by commas inside curly braces.
Example:
my_set = (1, 2, 3, 4, 5, 5}
print(my_set) # Outputs (1, 2, 3, 4, 5} (duplicates are removed)
# Adding and removing elements
my_set.add(6)
my_set remove (1
print (my_set) # Outputs {2, 3, 4, 5, 6
‘This chapter has introduced you to the fundamental data types in
Python and how to manipulate them. These basics form the building
biocks for more complex programming tasks that you'll encounter in
later chapters.Chapter 3: Control
Structures
3-1 Making Decisions with If-Statements
If-statements are essential for decision-making in Python. They
allow you to execute certain pieces of code based on whether a
condition is true or not.
Example:
if age >= 18:
print ("You are eligible to vote.")
print ("You are not ¢
gible to vot!
+ Using multiple conditions :
‘int ("You can vote and are a member.")
elif age >= 18:
print ("You can vote but are not a member.")
else:
print ("You are neither eligible to vote nor a member
")« Nested if-statements :
print ("Exe
elif score >= 75
print ("Very good!")
else
print ("Good, but try to do better.")
else:
print ("Sorry, you failed.")
3-2 Looping with For and While Loops
Loops in Python are used to repeatedly execute a block of statements
as long as a condition is met.
+ For Loops :For loops are typically used for iterating over a
sequence (like a list, tuple, dictionary, or string).
Example:
# Iterating over a
fruit wapp
for fruit in fruits:
print (£"I like (fruit}")* While Loops :While loops continue to execute as long as a
certain condition holds true.
Example:
+ Using loops with :An block after a loop executes after the loop
finishes normally (i.e., without hitting a statement).
£
U1, 2,
am in numbe
print (num)
els.
print ("No more numbers."
3-3 Comprehensions for Efficient Data
Handling
Comprehensions provide a concise way to create lists, dictionaries,
and sets from sequences or other iterable objects.+ List Comprehensions :These are used for creating new lists
from existing iterables. A list comprehension consists of
brackets containing an expression followed by a clause.
Exampl
# Squaring numbers in a range
squares = [x**2 for x in range (10)
print (squares)
« Dictionary Comprehensions :Similar to list comprehensions
but for dictionaries. It uses curly braces .
Exampl
# Create a dictionary with number and its square
squar;
print (square_dict)
dict
: x**2 for x in range(5)}
« Set Comprehensions :These are similar to list
comprehensions, but they produce sets, which are collections of
unique elements.
Example:
§ Creating a set of even numbers
evens = {x for x in range(10) if x $2
oF
print (evens)Control structures such as if-statements, loops, and comprehensions
are fundamental to programming in Python, enabling you to write
flexible and efficient code. As you progress, these tools will be
indispensable for handling more complex logic and data operations.Chapter 4: Functions and
Modules
4.1 Defining and Calling Functions
Functions in Python are defined using the keyword and are used to
encapsulate code into reusable blocks. They can take inputs, perform
actions, and return outputs.
Example:
def greet ():
print ("Hello, World!")
ling the func’
greet ()
* Function with parameters :
def greet (name):
print (f"Hello, (name}!")
# Calling the function with a parameter
greet ("Alice")
Functions can also be defined with default parameter values, making
some arguments optional during the function call.def greet (name="World") :
print (f"Hello, (name}!")
greet () uts "Hello, World!"
greet ("Everyone") # Outputs "Hello,
4.2 Parameters, Arguments, and Return
Values
Parameters are variables that are defined in the function signature
and receive values when the function is called. Arguments are the
actual values passed to the function.
+ Positional and Keyword Arguments :
def describe_pet (animal_type, pet_name):
have a (animal_type} named (pet_name}.")
onal argum
ibe_pet ("hamster', 'Harry')
# Keywo:
guments
de:
ibe_pet (pet_name='Willow', animal_type="cat')
« Returning Values :
Functions can return values using the statement, which exits the
function and optionally passes back an expression to the caller.def square (number)
return number
result = square (4)
print(result) # Outputs 16
4.3 Organizing Code with Modules and
Packages
Modules in Python are simply Python files with the extension
containing Python code. Packages are a way of structuring Python’s
module namespace by using “dotted module names”.
* Creating and Using a Module :
Suppose you have a file named with the following code:
4 my_module.py
def make pizza (t
print(f"Making a pizza with {topping)")
You can import and use this module in another file:
import my_module
my_module.make_pizza('pepperoni')
+ Using from...import Statement :You can also import specific attributes from a module using the
keyword:
from my_module import make_pizza
make_pizza ("mushrooms")
+ Packages :
A package is a directory containing Python modules and a file named
. It can be nested to create subpackages.
Suppose you have the following directory structure:
init__.py
dough.py
toppings .py
You can import modules from the package:
from pizza import dough
fron pizza.toppings import add topping
dough .make_dough ()
add_topping (*tomato")Understanding how to define and use functions, as well as organize
larger code bases with modules and packages, is crucial for writing
clean, maintainable, and reusable Python code. This organization
helps in managing larger projects efficiently and keeps the code
logically separated based on functionality.Chapter 5: Exception
Handling
5.1 Understanding Exceptions
Exceptions in Python are errors detected during execution that
disrupt the normal flow of a program. Python provides various built-
in exceptions such as , , , and many others. Understanding these
exceptions is crucial for debugging and for writing robust programs.
Example of encountering an exception:
numbers = [1, 2, 3]
print (numbers[3]) # IndexError: list index out of range
This example tries to access an element that is not present in the list,
causing an .
5.2 Handling Exceptions with Try-Except
To handle exceptions and to prevent them from crashing your
program, Python uses the try-except block. You can catch specific
exceptions and respond appropriately.
Basic usage:
try:
# Code that mi
print (numbers [3])
except IndexError as
# Code that ru:
£ an exception oc}print ("Rrror:", e)
This block will catch the and print an error message, preventing the
program from crashing.
Handling multiple exceptions:
You can also handle multiple exceptions, which allows you to
respond to different exception types differently.
# Code that might throw different except
x = int(input ("Enter a number: "))
result = numbers [x
except Valuer:
(MP a valid integer.")
pri
"That index is out of range.)
ption as e:
print("An unexpected error occurred:", e)
5.3 Raising Custom Exceptions
Sometimes, you might need to create and raise your own exceptions.
This is usually done to enforce certain conditions within your code.
Defining and raising a custom exception:
4 Define a custom exception by subclassing Exception class
class ValueToo#ighError (Exception):
def init f, me# Use
© of the custom exception
def check
ue (x)
if >
raise ValueTooHigh!
or ("Value
too high.")
else:
print ("Value is within the limit.")
try:
_value (200)
ueTooighErrer as e:
except V
print (2.message)
In this example, is a custom exception that is raised when a value
exceeds a predetermined limit. This allows the programmer to
provide meaningful error messages and to control the flow of the
program more effectively.
Exception handling is a fundamental part of developing reliable
Python applications. It not only helps in managing errors and
preventing program crashes but also allows developers to implement
robust error-checking mechanisms that can anticipate and manage
operational anomalies in the code.Chapter 6: Working with
Files
6.1 Reading from and Writing to Files
Python provides built-in functions for reading from and writing to
files on your system. This is essential for programs that need to save
user daia, configurations, results, and more.
Example of writing to a file:
# Writing to a file
with open('example.txt', 'w') as file:
file.write("Kello, World!\n")
file.write("This is another line.")
Example of reading from a file:
# Reading from a file
with open('example.txt', 'r') as file:
content = file.read()
nt)
print (
To read the file line by line:
with open(‘e:
for line in file:
# strip() removes the newlinecharacter at the end of each line
6.2 Handling Different File Formats
(Text, CSV, JSON)
Different types of data require different formats for efficient storage
and retrieval. Python handles various file formats through standard
libraries such as and .
Working with CSV files:
g to a CSV file
with open('example.csv', 'w', newline='') as file:
writ r (file)
writs
writer.writerow(["Bob", 25])
Working with JSON files:
import json
data ="name": "John Doe",
# Writing e
with open('d tw!) as file:
-dump (data, file)
# Reading
with open('d
data_loa
print (data_loaded)
6.3 Managing File Contexts with With-
Statement
The statement in Python is used for exception handling and proper
cleanup when working with resources like file streams. Using
ensures that the file is properly closed after its suite finishes, even if
an exception is raised on the way. It’s much cleaner and more
readable than using try-finally blocks.
Example:
# Correct way to handle file opening and closing
with open('example.txt', 'r') alThis practice not only prevents file corruption or leaks but also
simplifies the code by abstracting away the details of file
management.
Working with files is a fundamental aspect of programming that
allows data to be saved and retrieved across sessions. Python’s built-
in support for handling various file formats and its context-
management features enable developers to work with files efficiently
and safely.Chapter 7: Object-Oriented
Programming
7.1 Understanding Classes and Objects
Object-oriented programming (OOP) is a programming paradigm
based on the concept of “objects”, which can contain data, in the
form of fields (often known as attributes or properties), and code, in
the form of methods. In Python, classes provide a means of bundling
data and functionality together.
Basic Class Definition:
class Dog:
det
def ba.
my_dog
my_dog.ba
Outputs: Rex says woof
In this example, is a class, and is an object or instance of this class.
The method acts as a constructor to initialize the object’s state.7.2 Building Custom Classes
Custom classes are user-defined blueprints from which objects are
created. These often include a mix of methods and attributes.
Example of a more complex class:
class Car:
def init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
sel£.odome
eading = 0
def get_descriptive name(sel£):
long_name = £"{self.year) (self.make} {self.model}"
xn long_name
odometer (self):
print (£"Phis car has {self.odome|
eading} miles on
def update_odometer (self, mileage):
if mileage >= self.odometer_reading:
self. odomet;
reading = mileage
print ("You can't roll back an odometer!™)
Using the class:
my_new_car = Car(‘audi', 'a4", 2021)
print (my_new_car.get_descriptive_name())my_new_car ,update_odometer (23)
my_new_car.read odometer ()
7.3 Special Methods and Inheritance
Special methods allow you to define certain operations that are built
into Python’s syntax. These are also called magic or dunder methods.
Example of special methods:
self. title
self.pages = pages
(self):
"ise:
tle} by {sel£.author}"
Using special methods:
my_book = Book ("George 11", "1984", 328)
print (my book) # Outputs: 1984 by George Orwell
print (len(my_pbook)) # 91 s: 328
Inheritance: Inheritance allows one class to inherit the attributes
and methods of another class. This helps to reuse code.Example of inheritance:
class BlectricCar(Car): # Inherits from Car
def
model, yea!
t__(sel£, mal
ex().__init__(make, model, year)
self.battery size
ribe_battery (self):
nt(£"This car has a (self.battery_size}-kwh battery.")
Using the inherited class:
my_tesla = Ele Car('tesia', ‘model s', 2019)
print (my_tesla.get_descriptive_name())
my_tesla.deseribe battery ()
Understanding classes and objects is fundamental to mastering
Python’s capabilities in object-oriented programming. With custom
classes, special methods, and inheritance, you can write more
organized and powerful Python code that better models real-world
data and behaviors.Chapter 8: Advanced Python
Concepts
8.1 Iterators and Generators
Tterators and generators are advanced tools in Python that allow you
to manage sequences of data more efficiently than by using lists.
Iterators: Iterators are objects that can be iterated upon. An
iterator retrieves its elements by calling the function until no items
are left.
Example of creating an iterator:
s= (1, 2, 3,4
bj = iter (numbers)
print (next (iter_obj)) # Outputs 1
print (next (iter_obj)) # Outputs 2
Generators: Generators are a simple way to create iterators using
functions. Instead of returning a value, a generator yields a series of
values.
Example of a generator:
def countdown (num):
print
"Starting countdown")
while num > 0:
yield num
num -= 1for number in countdown (5):
print (number)
This generator yields numbers from 5 down to 1, one at a time,
pausing at each until the next value is requested.
8.2 Decorators and Context Managers
Decorators and context managers are powerful features in Python for
modifying and managing the behavior of functions and code blocks.
Decorators: A decorator is a function that takes another function
and extends its behavior without explicitly modifying it.
Example of a decorator:
def debug (func) :
apper (*a
une (*args, **kwargs)
Function: (fu:
return wrapper
@debug
def add(x, y):
return x + y
print (add(5, 3))
-__name_},
Argumen:
‘The decorator enhances any function with debug output about its
arguments and the result.Context Managers: Context managers allow you to allocate and
release resources precisely when you want. The most common way to
create a context manager is by using the statement.
Example of a context manager:
from contextlib import contextmanager
@contextmanager
def managed_file (name) :
try:
file = open(name, 'w')
yield file
finally:
file.close()
with man: File (‘hell
£.write (‘hello,
This context manager handles opening and closing a file.
8.3 Lambda Functions and Functional
Programming
Lambda functions are small anonymous functions defined with the
lambda keyword. They can have any number of arguments but only
one expression.
Example of a lambda function:
multiply = lambda x, y: x * y
print (multiply(2, 3)) # OutpFunctional programming concepts in Python also include functions
like , , and for processing sequences.
Using and:
numbers
(1, 2, 3, 4 8
¥ Double
h number using map()
doubled = list (map(lambda x: x * 2, numbers))
print (doubled) f Outputs [2, 4, 6 8, 20
# Filter to get only even numbers
(2ilter (lambda x: x $ 2 == 0, numbers))
print (evens) # Outputs (2, 4
This chapter covers some of the more complex but incredibly
powerful aspects of Python, including iterators, generators,
decorators, context managers, and lambda functions. These features
enable more efficient data handling, elegant code modification,
resource management, and use of functional programming
techniques.Chapter 9: Data Handling
with Pandas
9.1 Introduction to Pandas
Pandas is a powerful Python library for data manipulation and
analysis, providing data structures and operations for manipulating
numerical tables and time series. It’s built on top of the NumPy
library and is crucial in data analysis and machine learning tasks.
Installation of Pandas: To get started with Pandas, you need to
install it using pip:
pip install pandas
9.2 DataFrames and Series
The two primary data structures of Pandas are Series and
DataFrames.
Series: A Series is a one-dimensional array-like object containing a
sequence of values (similar to a NumPy array) and an associated
array of data labels, called its index.
Example of creating a Series:
import pandas as pd
data = p es({1, 3, 5, 7, 91)
print (dataDataFrames: A DataFrame is a two-dimensional, size-mutable,
potentially heterogeneous tabular data structure with labeled axes
(rows and columns). It’s generally the most commonly used pandas
object.
Example of creating a DataFrame:
anada', ‘Germany’, 'UK', 'France'],
ngton, D
'Population': (328, 37, 83, 66, 67]
df = pa
caFrame (data)
print (df)
9.3 Data Manipulation and Analysis
Pandas provides numerous functions for data manipulation and
analysis. These include operations for indexing, deleting, and
filtering data, as well as performing statistical analysis and handling
missing data.
Indexing and Selecting Data:
# Selecting a column
print (af ["Capital'])
Selecting a row by index
print (af.iloc[2]) # Selects the row of# Selecting a specific value
print (df.at(2, 'Capital']) # Outputs ‘Berlin!
Filtering Dat
# Filtering rows where Population is greater than 50 million
filtered _df = df[df{'Population'] > 50]
print (filtered df)
Handling Missing Data: Pandas makes it simple to handle
missing data and replace it with some default value or a computed
value.
# Assuming ‘data’ contains missing values
data = pd.Series([1, None, 3, None, 5])
# Filling missing data
filled_data = data.filina(0)
print (filled_data)
Statistical Analysis: Pandas also provides ways to do a quick
statistical analysis of your data.
# Describe gives a quick statistical summary of your DataFrame
print (df.describe())
# Mean of a particular column
print (af [*Population"].mean()}Pandas is an indispensable tool in the Python data science toolkit. It
excels in performance and productivity for users working with data
and has become a pivotal feature of financial, social sciences, and
engineering analytics workflows. This chapter provides a foundation
for starting with basic data handling tasks in Pandas and prepares
you for more complex data manipulation and analysis techniques.Chapter 10: Data
Visualization
10.1 Visualizing Data with Matplotlib
Matplotlib is a comprehensive library for creating static, animated,
and interactive visualizations in Python. It is especially useful for
making 2D plots from data in arrays.
Installation of Matplotlib:
pip install natp
Basic Plotting: Here's how to create a simple line chart with
Matplotlib:
import matplotlib.pyplot as plt
# Data
x= (1, 2, 3, 4 5)
y= (2, 3, 5, 7, 11
# Plot
plt.plot (x, y)
plt.title('Simple Line Plot')
plt-xlabel('X Axis Label")
abel
¥ Axis Label')10.2 Plotting with Seaborn
Seaborn is a Python data visualization library based on Matplotlib. It
provides a high-level interface for drawing attractive and informative
statistical graphics.
Installation of Seaborn:
pip install seaborn
Example of a Scatter Plot: Seaborn makes it easy to create
beautiful plots with minimal code:
import seaborn as sns
# Data
tips )
+ er plot
atterplot (x="total_bill", y="tip",
plt.ti
plt.
("Tip by Total Bill")
ow ()
10.3 Advanced Visualization Techniques
Advanced techniques in data visualization include creating complex
multi-plot grids, interactive plots, and using geographical mapping
data.
Subplots: Creating multiple plots in the same figure with
Matplotlib:# Create a figure with 2x2 grid of Axes
fig, ax = plt.subplots(2, 2, figsize=(10, 10))
ax(0, 0] -plot(x, y)
ax[0, O].set_title(*First Plot")
ax[0, 1]-plot(x, y, 'tab:orange')
ax[0, 1].set_title('Second Plot’)
ax[1, O].plot(x, yy ‘tal
ax(1, 0].set_title ("Third Plot")
rgreen")
ax[1, 1].plot(x, y, 'tab:red")
ax(1, 1].set_title(*Fourth Plot")
plt.show()
Interactive Plots with Plotly: Plotly is another library that allows
for interactive plots which are especially useful for web applications.
Installation of Plotly:
pip install plotly
Example of an interactive plot:
import plotly.express as px
df = px.data.iris() # Use Plotly's bu
fig = px.scatter (df,
sepal_width"fig. show()
Geographical Data Visualization: Visualizing data on maps can
be achieved using libraries such as and .
Example with GeoPandas:
import geopandas as gpd
# Load a GeoDataFrame containing regions (e.g., countries or
states)
gat = gpd.read_file (gpd.datasets.get_path ('naturalearth_low
# Plot
gaf-plot ()
plt.show()
This chapter introduces basic and advanced techniques for
visualizing data in Python. Visualization is crucial for interpreting
data and making decisions based on analysis. Tools like Matplotlib,
Seaborn, and Plotly provide powerful ways to present data clearly
and effectively, from simple charts to complex interactive plots and
geographical mapping.Chapter 11: Python for Web
Development
11.1 Exploring Web Frameworks: Flask
and Django
Python offers several powerful frameworks for web development,
with Flask and Django being the most popular. Flask provides a
lightweight and flexible approach, making it suitable for small to.
medium applications, while Django offers a more feature-complete
environment, which is ideal for larger projects.
Flask: Flask is a micro web framework that is easy to get started
with and is suitable for small projects and microservices.
Installation of Flask:
pip install Flask
Django: Django is a high-level Python web framework that
encourages rapid development and clean, pragmatic design. It
includes an ORM, routing, authentication, and more out-of-the-box.
Installation of Django:
pip install Django
11.2 Building a Simple Web ApplicationCreating a Basic Flask App: Here’s how to create a basic web
application that displays “Hello, World!” on your browser.
from £
sk import Flask
app = Flask(_name_)
@app.route('/")
rn "Hello, World!'
app. run (debug=True)
Run this script, and visit in your browser to see the greeting.
Creating a Basic Django App: Setting up a simple Django
application involves more steps, reflecting its “batteries-included”
approach.
1. Create a project:
‘tproject myproject
2. Navigate into the project directory:
cd myproject
3. Start an app:python manage.py startapp myapp
4. Create a view in:
from django. http import HttpResponse
def hello (request!
return BttpResponse("Hello, World!
5. Map the view to a URL in (create the file if it doesn’t exist):
from django.urls import path
from .views import hello
urlpatterns = [
path('', hello),
6. Include the app’s URLconf in the project’s :
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path(‘admin/', admin.site.urls),
path("", include('myapp.urls")),7. Run the server:
python manage.py runserver
Visit in your browser.
11.3 Working with APIs
Using Flask to Create a Simple API: Flask can be used to build
RESTful APIs. Here’s an example of a simple API that returns JSON
data.
@app. route ('/api/data')
OF
rn jsonify({'key': 'value',
app. run (debug=True)
When you visit , you will see the JSON response.
Interacting with External APIs Using Library: To work with
external APIs, you can use Python’s library.
Installation:pip install requ
Example of making a GET request:
import requests
response = requests.get ("https://api.example.com/data')
data
print (¢
This fetches data from an external API and prints the JSON
response.
This chapter provides an overview of developing web applications
and APIs using Python. Whether you choose Flask for simplicity and
control or Django for its extensive features, Python’s frameworks
support robust web development practices. Moreover, interacting
with APIs using Python is straightforward, thanks to libraries like .