Intermediate Python: Advanced Libraries & Concepts Matplotlib, NumPy, Threading, and Sockets
Introduction • • This presentation covers essential intermediate Python topics. • • We'll explore data visualization, numerical computing, concurrency, and networking. • • Libraries covered: Matplotlib, NumPy, Threading, and Sockets.
Matplotlib: Data Visualization • • Matplotlib is used for creating static, animated, and interactive visualizations. • • Common plots: Line graphs, bar charts, histograms, scatter plots. • • Key functions: plt.plot(), plt.bar(), plt.hist(), plt.scatter(). • • Customization options for labels, colors, and styles.
Matplotlib Example Code • ```python • import matplotlib.pyplot as plt • x = [1, 2, 3, 4] • y = [10, 20, 25, 30] • plt.plot(x, y, marker='o', linestyle='-') • plt.xlabel('X-axis') • plt.ylabel('Y-axis') • plt.title('Sample Line Plot') • plt.show()
NumPy: Numerical Computing • • NumPy is a powerful library for numerical operations. • • Provides support for multi-dimensional arrays and matrices. • • Optimized for performance compared to Python lists. • • Common functions: np.array(), np.linspace(), np.mean(), np.std().
NumPy Example Code • ```python • import numpy as np • arr = np.array([1, 2, 3, 4, 5]) • print('Mean:', np.mean(arr)) • print('Standard Deviation:', np.std(arr)) • ```
Threading: Concurrency in Python • • The threading module allows parallel execution of tasks. • • Useful for I/O-bound operations but not CPU-intensive tasks. • • Key functions: threading.Thread(), start(), join(). • • Avoids blocking operations by running multiple tasks concurrently.
Threading Example Code • ```python • import threading • import time • def print_numbers(): • for i in range(5): • print(i) • time.sleep(1)
Sockets: Networking in Python • • The socket module allows network communication between computers. • • Supports TCP and UDP protocols. • • Common functions: socket(), bind(), listen(), accept(), send(), recv(). • • Used in building servers, clients, and peer- to-peer applications.
Sockets Example: Simple Server • ```python • import socket • server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) • server.bind(('localhost', 12345)) • server.listen(1) • conn, addr = server.accept() • print('Connected by', addr)
Conclusion • • Matplotlib is used for data visualization. • • NumPy provides efficient numerical operations. • • Threading enables concurrent execution. • • Sockets allow network communication. • • Mastering these topics helps in real-world Python applications!

COMPE361 Python Midterm 2 Notes Covering Socket, Threading, Matlib, and NUMPY

  • 1.
    Intermediate Python: Advanced Libraries& Concepts Matplotlib, NumPy, Threading, and Sockets
  • 2.
    Introduction • • Thispresentation covers essential intermediate Python topics. • • We'll explore data visualization, numerical computing, concurrency, and networking. • • Libraries covered: Matplotlib, NumPy, Threading, and Sockets.
  • 3.
    Matplotlib: Data Visualization •• Matplotlib is used for creating static, animated, and interactive visualizations. • • Common plots: Line graphs, bar charts, histograms, scatter plots. • • Key functions: plt.plot(), plt.bar(), plt.hist(), plt.scatter(). • • Customization options for labels, colors, and styles.
  • 4.
    Matplotlib Example Code •```python • import matplotlib.pyplot as plt • x = [1, 2, 3, 4] • y = [10, 20, 25, 30] • plt.plot(x, y, marker='o', linestyle='-') • plt.xlabel('X-axis') • plt.ylabel('Y-axis') • plt.title('Sample Line Plot') • plt.show()
  • 5.
    NumPy: Numerical Computing •• NumPy is a powerful library for numerical operations. • • Provides support for multi-dimensional arrays and matrices. • • Optimized for performance compared to Python lists. • • Common functions: np.array(), np.linspace(), np.mean(), np.std().
  • 6.
    NumPy Example Code •```python • import numpy as np • arr = np.array([1, 2, 3, 4, 5]) • print('Mean:', np.mean(arr)) • print('Standard Deviation:', np.std(arr)) • ```
  • 7.
    Threading: Concurrency inPython • • The threading module allows parallel execution of tasks. • • Useful for I/O-bound operations but not CPU-intensive tasks. • • Key functions: threading.Thread(), start(), join(). • • Avoids blocking operations by running multiple tasks concurrently.
  • 8.
    Threading Example Code •```python • import threading • import time • def print_numbers(): • for i in range(5): • print(i) • time.sleep(1)
  • 9.
    Sockets: Networking inPython • • The socket module allows network communication between computers. • • Supports TCP and UDP protocols. • • Common functions: socket(), bind(), listen(), accept(), send(), recv(). • • Used in building servers, clients, and peer- to-peer applications.
  • 10.
    Sockets Example: SimpleServer • ```python • import socket • server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) • server.bind(('localhost', 12345)) • server.listen(1) • conn, addr = server.accept() • print('Connected by', addr)
  • 11.
    Conclusion • • Matplotlibis used for data visualization. • • NumPy provides efficient numerical operations. • • Threading enables concurrent execution. • • Sockets allow network communication. • • Mastering these topics helps in real-world Python applications!