# Python Training Material with Electrical Applications
## Prelab Tutorial
### **1. Python Data Types and Their Applications**
**Python Concepts:**
- **Data Types:**
- `int`: Used for integer numbers.
- `float`: Used for real numbers.
- `str`: Used for text or characters.
- `bool`: Represents True or False values.
**Electrical Applications:**
- **Integer:** Represent the number of components in a circuit.
- **Float:** Voltage, current, and resistance values that require decimal precision.
- **String:** Labels for circuit elements like "Resistor" or "Capacitor."
**Sample Program:**
```python
# Python script to illustrate data types
x = 10 # Integer
y = 3.14 # Float
z = "Resistor" # String
is_active = True # Boolean
print(f"Integer: {x}, Float: {y}, String: {z}, Boolean: {is_active}")
```
---
### **2. Operator Precedence and Its Applications**
**Python Concepts:**
- **Arithmetic Operators:** `+`, `-`, `*`, `/`, `**` (exponentiation).
- **Precedence Rules:** Multiplication and division take precedence over addition and subtraction.
**Electrical Applications:**
- Operator precedence helps calculate complex circuit parameters, such as power, energy, and
impedance.
**Sample Program:**
```python
# Operator precedence examples
print(5 + 3 * 2) # Output: 11
print(2 ** 3 ** 2) # Output: 512 (right-to-left precedence)
print(2 * 3 ** 2) # Output: 18
print((23) ** 2) # Output: 529
```
**Electrical Example:**
Calculate power dissipated in a resistor where \( P = I^2 R \):
```python
I = 2 # Current in amperes
R = 5 # Resistance in ohms
P = I ** 2 * R
print(f"Power dissipated: {P} W")
```
---
### **3. Type Conversion Functions and Their Applications**
**Python Concepts:**
- Convert between data types using functions like `int()`, `float()`, and `str()`.
**Electrical Applications:**
- Convert user inputs (often in strings) to numeric types for calculations.
**Sample Program:**
```python
# Type conversion example
voltage = "5.5"
current = "2"
V = float(voltage)
I = int(current)
P=V*I
print(f"Power dissipated: {P} W")
```
---
### **4. Math Module Functions and Applications**
**Python Concepts:**
- **Math Module Functions:**
- `math.pi`: Constant π (pi).
- `math.sqrt()`: Square root calculation.
- `math.cos()`, `math.sin()`: Trigonometric functions.
**Electrical Applications:**
- **Cosine and Sine:** Used to analyze AC signals and phasors.
- **Square Root:** Calculating RMS (Root Mean Square) values.
**Sample Program:**
```python
import math
radius = 5
area = math.pi * radius ** 2
print(f"Area of a circle: {area:.2f}")
value = 16
print(f"Square root: {math.sqrt(value)}")
angle = math.pi / 4 # 45 degrees in radians
print(f"Cos(45°): {math.cos(angle):.2f}, Sin(45°): {math.sin(angle):.2f}")
```
**Electrical Example:**
Calculate impedance for an AC circuit with resistance \( R \) and reactance \( X \):
```python
R=3
X=4
Z = math.sqrt(R**2 + X**2) # Impedance
print(f"Impedance: {Z:.2f} Ohms")
```
---
## Assignments
1. Write a Python script to define variables for current, resistance, and voltage. Use these to calculate
and print power dissipated in the circuit.
2. Modify the operator precedence program to include a formula for energy stored in an inductor \( E =
\frac{1}{2} L I^2 \).
3. Write a program to convert a string input representing a resistance value into a float and calculate
power using \( P = \frac{V^2}{R} \).
4. Using the math module, calculate and print the RMS value of an AC voltage signal where \( RMS =
V_{peak} / \sqrt{2} \).
---
## Quiz (Multiple Choice, 25 Questions)
### Sample Questions:
1. Which data type is suitable for representing resistance in ohms?
- (a) int
- (b) float
- (c) str
- (d) bool
**Answer:** (b) float
2. What is the output of the following expression: `5 + 3 * 2`?
- (a) 16
- (b) 11
- (c) 10
- (d) None of the above
**Answer:** (b) 11
3. Which function is used to calculate square roots in Python?
- (a) `sqrt()`
- (b) `math.sqrt()`
- (c) `root()`
- (d) None of the above
**Answer:** (b) `math.sqrt()`
4. What does the following code output?
```python
import math
print(math.pi)
```
- (a) 3.142
- (b) 3.14
- (c) 3.141592653589793
- (d) None of the above
**Answer:** (c) 3.141592653589793
5. Calculate the impedance for a circuit with R = 3 ohms and X = 4 ohms.
- (a) 7 ohms
- (b) 5 ohms
- (c) 4 ohms
- (d) None of the above
**Answer:** (b) 5 ohms
---