Week-8
1. Install NumPy package with pip and explore.
Python NumPy is an open-source project that can be used freely. NumPy stands for Numerical
Python.
With NumPy, you can easily create arrays, which is a data structure that allows you to store
multiple values in a single variable.
>>>> pip install numpy
If the import is successful, then you will see the following output.
>>> import numpy
>>> numpy.__version__
Example:
Creating an Array:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Output:
[1 2 3 4 5]
❖ We can also pass a tuple in the array function to create an array. 2
import numpy as np
arr = np.array((1, 2, 3, 4, 5))
print(arr)
Output:
[1 2 3 4 5]
2. Import numpy, Plotpy and Scipy and explore their functionalities.
a. Open IDLE.
b. In the Python shell, type:
import os
os.system('pip install matplotlib')
You can do the same for Scipy:
import os
os.system('pip install numpy scipy')
1. NumPy – Numerical Python
Use: Fast array operations, linear algebra, statistics, etc.
import numpy as np
# Create a 1D array
arr = np.array([1, 2, 3, 4, 5])
print("Array:", arr)
# Mean and Standard Deviation
print("Mean:", np.mean(arr))
print("Standard Deviation:", np.std(arr))
# 2D array and matrix multiplication
A = np.array([[1, 2], [3, 4]])
B = np.array([[2, 0], [1, 2]])
print("Matrix multiplication:\n", np.dot(A, B))
OUTPUT:
2. Matplotlib (Plotpy) – Plotting Library
Use: For creating static, animated, and interactive visualizations.
import numpy as np
import matplotlib.pyplot as plt
# Simple Line Plot
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y, label='sin(x)')
plt.title("Sine Wave")
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.legend()
plt.grid(True)
plt.show()
OUTPUT:
3. SciPy – Scientific Computing
Use: Built on NumPy. Supports optimization, integration, interpolation, statistics, and more.
Example: Integration and Optimization
from scipy import integrate, optimize
# Define a simple function
def f(x):
return x ** 2
# Integrate f(x) from 0 to 2
result, error = integrate.quad(f, 0, 2)
print("Integral of x^2 from 0 to 2:", result)
# Find the minimum of a function
min_result = optimize.minimize(f, x0=2)
print("Minimum of x^2:", min_result.x)
OUTPUT:
3. Write a program to implement Digital Logic Gates – AND, OR, NOT, EX-OR
#Function for AND Gate
def AND(A, B):
return A & B
print("AND LOGIC OUTPUT")
print("Output of 0 AND 0 is:", AND(0, 0))
print("Output of 0 AND 1 is:", AND(0, 1))
print("Output of 1 AND 0 is:", AND(1, 0))
print("Output of 1 AND 1 is:", AND(1, 1))
# Function for OR Gate “ | “ this symbol is called bar or pipe.
def OR(A, B):
return A | B
print("OR LOGIC OUTPUT")
print("Output of 0 OR 0 is:", OR(0, 0))
print("Output of 0 OR 1 is:", OR(0, 1))
print("Output of 1 OR 0 is:", OR(1, 0))
print("Output of 1 OR 1 is:", OR(1, 1))
#Function for NOT Gate
def NOT(A):
return ~A+2
print("NOT LOGIC OUTPUT")
print("Output of NOT for 0 is: ", NOT(0))
print("Output of NOT for 1 is: ", NOT(1))
# Function for XOR Gate
def XOR(A, B):
return A ^ B
print("XOR LOGIC OUTPUT")
print("Output of 0 XOR 0 is:", XOR(0, 0))
print("Output of 0 XOR 1 is:", XOR(0, 1))
print("Output of 1 XOR 0 is:", XOR(1, 0))
print("Output of 1 XOR 1 is:", XOR(1, 1))
4. Write a program to implement Half Adder, Full Adder, and Parallel Adder.
# Function for Half Adder
#Half Adder which has 2 inputs (bit_a, bit_b) and gives two outputs the XOR for sum and
the AND for carry.
def half_adder(bit_a, bit_b):
return (xor(bit_a, bit_b), bit_a and bit_b)
# Program for Half adder
def xor(bit_a, bit_b):
A1 = bit_a and (not bit_b)
A2 = (not bit_a) and bit_b
return int(A1 or A2)
print("**************************************")
print(" XOR LOGIC OUTPUT")
print("Output of 0 XOR 0 is:", xor (0, 0))
print("Output of 0 XOR 1 is:", xor (0, 1))
print("Output of 1 XOR 0 is:", xor (1, 0))
print("Output of 1 XOR 1 is:", xor (1, 1))
######### Enter Input Binary data ###################
def half_adder(bit_a, bit_b):
return (xor(bit_a, bit_b), bit_a and bit_b)
print("**************************************")
print(" Half adder OUTPUT")
print("Output of sum and carry is:", half_adder(0, 0))
print("Output of sum and carry is:", half_adder (0, 1))
print("Output of sum and carry is:", half_adder (1, 0))
print("Output of sum and carry is:", half_adder (1, 1))
OUTPUT:
#function for full adder
def full_adder(bit_a, bit_b, carry=0):
sum1, carry1 = half_adder(bit_a, bit_b) # Half Adder 1
sum2, carry2 = half_adder(sum1, carry)# Half Adder 2
return (sum2, carry1 or carry2)
# Program: Full adder using two half adders
def xor(bit_a, bit_b):
A1 = bit_a and (not bit_b)
A2 = (not bit_a) and bit_b
return int(A1 or A2)
print("**************************************")
print(" XOR LOGIC OUTPUT")
print("Output of 0 XOR 0 is:", xor (0, 0))
print("Output of 0 XOR 1 is:", xor (0, 1))
print("Output of 1 XOR 0 is:", xor (1, 0))
print("Output of 1 XOR 1 is:", xor (1, 1))
######### Enter Input Binary data #####################
def half_adder(bit_a, bit_b):
return (xor(bit_a, bit_b), bit_a and bit_b)
print("**************************************")
print(" Half adder OUTPUT")
print("Output of sum and carry is:", half_adder(0, 0))
print("Output of sum and carry is:", half_adder (0, 1))
print("Output of sum and carry is:", half_adder (1, 0))
print("Output of sum and carry is:", half_adder (1, 1))
def full_adder(bit_a, bit_b, carry=0):
sum1, carry1 = half_adder(bit_a, bit_b) # Half Adder 1
sum2, carry2 = half_adder(sum1, carry)# Half Adder 2
return (sum2, carry1 or carry2)
print("**************************************")
print(" full adder OUTPUT")
print("Output of sum and carry is:", full_adder(0, 0,0))
print("Output of sum and carry is:", full_adder (0,0, 1))
print("Output of sum and carry is:", full_adder (0,1,0))
print("Output of sum and carry is:", full_adder (0,1, 1))
print("Output of sum and carry is:", full_adder(1, 0,0))
print("Output of sum and carry is:", full_adder (1,0,1))
print("Output of sum and carry is:", full_adder (1,1,0))
print("Output of sum and carry is:", full_adder (1,1,1))
OUTPUT:
5. Write a GUI program to create a window wizard having two text labels, two text fields and
two buttons as Submit and Reset.
from tkinter import *
# Submit button action
def submit():
print("Name:", name_entry.get())
print("Email:", email_entry.get())
# Reset button action
def reset():
name_entry.delete(0, END)
email_entry.delete(0, END)
# Create main window
root = Tk()
root.title("Simple Form")
root.geometry("300x150") # Set window size
root.configure(bg='lightblue') # Set background color
# Name label and text field
Label(root, text="Name", bg='lightblue').grid(row=0, column=0)
name_entry = Entry(root)
name_entry.grid(row=0, column=1)
# Email label and text field
Label(root, text="Email", bg='lightblue').grid(row=1, column=0)
email_entry = Entry(root)
email_entry.grid(row=1, column=1)
# Submit and Reset buttons
Button(root, text="Submit", command=submit).grid(row=2, column=0)
Button(root, text="Reset", command=reset).grid(row=2, column=1)
# Run the window
root.mainloop()
OUTPUT: