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

peenerbeaner2023 3 views 11 slides Mar 07, 2025
Slide 1
Slide 1 of 11
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

About This Presentation

Notes very helpful for passing midterm 2 of COMPE361 at SDSU


Slide Content

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) t = threading.Thread(target=print_numbers) t.start() t.join() print('Thread finished') ```

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) conn.sendall(b'Hello, Client!') conn.close() ```

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!