python solution
python solution
1.
a) Write simple Python program to calculate equivalent registers connected in series and parallel.
Accept values of R1, R2 and R3 from the user.
b) Write simple Python program to calculate value of voltage by applying Ohm’s law. Accept value of
Current(I) and Resistance(R) from the user.
Solution: a
1. def equivalent_resistors():
2. # Accepting values of R1, R2, and R3 from the user
3. R1 = float(input("Enter the value of R1: "))
4. R2 = float(input("Enter the value of R2: "))
5. R3 = float(input("Enter the value of R3: "))
6.
7. # Calculating equivalent resistance in series
8. series_resistance = R1 + R2 + R3
9.
10. # Calculating equivalent resistance in parallel
11. parallel_resistance = 1 / ((1 / R1) + (1 / R2) + (1 / R3))
12.
13. # Printing the results
14. print(f"Equivalent resistance in series: {series_resistance} ohms")
15. print(f"Equivalent resistance in parallel: {parallel_resistance} ohms")
16.
17. # Calling the function
18. equivalent_resistors()
Solution: b
1. def calculate_voltage(i, r): return i * r
2. def calculate_resistance(v, i): return v / i
3. def calculate_current(v, r): return v / r
4. while True:
5. option = input("1. Voltage\n2. Resistance\n3. Current\n4. Exit\n> ")
6. if option == "4": break
7. try:
8. if option == "1":
9. print(f"Voltage = {calculate_voltage(float(input('Current (A): ')), float(input('Resistance (Ω):
'))):.2f} V")
10. elif option == "2":
11. print(f"Resistance = {calculate_resistance(float(input('Voltage (V): ')), float(input('Current (A):
'))):.2f} Ω")
12. elif option == "3":
SOLUTION
13. print(f"Current = {calculate_current(float(input('Voltage (V): ')), float(input('Resistance (Ω):
'))):.2f} A")
14. else:
15. print("Invalid option.")
16. except ValueError:
17. print("Enter numerical values.")
18. except ZeroDivisionError:
19. print("Division by zero is not allowed.")
2.
Write program to check whether entered frequency is radio frequency or audio frequency.
Solution:
1. def check_frequency():
2. frequency = float(input("Enter the frequency in Hz: "))
9. check_frequency()
3.
a) Write program to display various radio frequency bands using if..elseif ladder.
Solution: a
1. def radio_frequency_band():
2. frequency = float(input("Enter the frequency in Hz: "))
3. bands = [
4. (3e3, 30e3, "Very Low Frequency (VLF)"),
5. (30e3, 300e3, "Low Frequency (LF)"),
6. (300e3, 3e6, "Medium Frequency (MF)"),
7. (3e6, 30e6, "High Frequency (HF)"),
8. (30e6, 300e6, "Very High Frequency (VHF)"),
9. (300e6, 3e9, "Ultra High Frequency (UHF)"),
10. (3e9, 30e9, "Super High Frequency (SHF)"),
SOLUTION
11. (30e9, 300e9, "Extremely High Frequency (EHF)"),
12. ]
13. for low, high, name in bands:
14. if low <= frequency < high:
15. print(name)
16. return
17. print("Frequency out of radio frequency range")
18.
19. radio_frequency_band()
Solution: b
1. def resistor_color_code():
2. colors = {
3. "black": 0,
4. "brown": 1,
5. "red": 2,
6. "orange": 3,
7. "yellow": 4,
8. "green": 5,
9. "blue": 6,
10. "violet": 7,
11. "gray": 8,
12. "white": 9
13. }
20. resistor_color_code()
4.
Solution: a
SOLUTION
1. def factorial(n):
2. if n == 0 or n == 1:
3. return 1
4. else:
5. return n * factorial(n - 1)
6.
7. def swap(a, b):
8. a, b = b, a
9. return a, b
10.
11. # Main Program
12. if __name__ == "__main__":
13. # Factorial Calculation
14. number = int(input("Enter a number to calculate its factorial: "))
15. result = factorial(number)
16. print(f"The factorial of {number} is {result}")
17.
18. # Swapping Variables
19. x = input("Enter the first value: ")
20. y = input("Enter the second value: ")
21.
22. # Perform the swap
23. x, y = swap(x, y)
24. print(f"After swapping, first value is: {x}, second value is: {y}")
Solution:
1. # a) Create
2. print("Creating tuples:")
3. tuple1 = (1, 2, 3, 4, 5)
4. tuple2 = ('a', 'b', 'c', 'd')
5. print("Tuple 1:", tuple1)
6. print("Tuple 2:", tuple2)
7.
8. # b) Access
9. print("\nAccessing elements from tuples:")
10. print("First element of tuple1:", tuple1[0])
11. print("Last element of tuple2:", tuple2[-1])
12. print("Slice of tuple1 (index 1 to 3):", tuple1[1:4])
SOLUTION
13.
14. # c) Update (indirectly)
15. print("\nUpdating tuple elements (indirectly):")
16. # Convert tuple to list, update, and convert back to tuple
17. list1 = list(tuple1)
18. list1[0] = 10
19. tuple1 = tuple(list1)
20. print("Updated tuple1:", tuple1)
21.
22. # d) Delete
23. print("\nDeleting a tuple:")
24. del tuple1
25. print("Tuple1 deleted. Attempting to print tuple1 will result in an error.")
26. try:
27. print(tuple1)
28. except NameError as e:
29. print(e)
Solution:
9. # Main program
10. if __name__ == "__main__":
11. print("Simple Calculator")
x = [1, 2, 3, 4]
plt.plot(x, y)
plt.xlabel(“X-axis”)
plt.ylabel(“Y-axis”)
plt.grid(True)
plt.show()
Print(a + b)
# Output:
# [[11 21 31]
# [12 22 32]
# [13 23 33]]