Python for Electrical Engineers – From Noob to Pro
Part 1: Basics of Python
- Variables, Data Types, Input/Output
- Operators, Conditions, Loops
# Ohm's Law Calculator
def ohms_law(v=None, i=None, r=None):
if v is None: return i * r
if i is None: return v / r
if r is None: return v / i
print("Current:", ohms_law(v=12, r=6), "A")
Exercises:
1. Write a program to check if a number is even or odd.
2. Write a program to calculate power (P = V × I).
Part 2: Data Structures
- Strings, Lists, Tuples, Sets, Dictionaries
# Resistor color codes
resistors = {"red": 2, "green": 5, "blue": 6}
print("Green resistor value:", resistors["green"], "Ω")
Exercises:
1. Store 5 student marks in a dictionary and print the topper.
2. Write a list program to find the average voltage readings.
Part 3: Functions & Modules
- Functions, Arguments, Return
- Importing modules (math, random)
import math
# RMS of a signal
def rms(values):
return math.sqrt(sum(v**2 for v in values) / len(values))
print("RMS:", rms([2, 4, 6, 8]))
Exercises:
1. Write a function for series resistance.
2. Write a function for parallel resistance.
Part 4: NumPy & File Handling
- NumPy arrays
- Generate sine, cosine, noise
- Read/Write CSV
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 1000)
v = np.sin(2*np.pi*50*t) # 50Hz sine
plt.plot(t, v)
plt.title("Sine Wave")
plt.show()
Exercises:
1. Generate a 60 Hz sine wave and plot.
2. Save it to CSV and reload it using Pandas.
Part 5: Visualization (Matplotlib)
- Line plots, multiple plots
- Styling, labels, grid
plt.plot(t, v, label="Voltage")
plt.xlabel("Time (s)")
plt.ylabel("Voltage (V)")
plt.legend()
plt.show()
Exercises:
1. Plot voltage and current on same graph.
2. Plot histogram of noise signal.
Part 6: Signal Processing (SciPy)
- FFT (Fourier Transform)
- Filtering
- RMS, Peak, THD
from scipy.fft import fft, fftfreq
N = 1000
T = 1/1000
yf = fft(v)
xf = fftfreq(N, T)[:N//2]
plt.plot(xf, 2.0/N * np.abs(yf[:N//2]))
plt.title("FFT Spectrum")
plt.show()
Exercises:
1. Apply FFT on noisy sine wave.
2. Design a low-pass filter to remove noise.
Part 7: Mini Project – Digital Waveform Analyzer
- Generate & capture signals
- FFT + filtering
- Compute RMS, THD
- (Optional) Classify signals using ML