import xlrd
loc=("your own excel path with double slash at drive name like C:\\")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
print ("The value of Row 0 and Column 0 is", sheet.cell_value(0, 0))
print("No of Rows in the current sheet is",sheet.nrows)
print("No of Columns in the current sheet is",sheet.ncols)
print("Extracting all columns name in the current sheet")
for i in range(sheet.ncols):
print(sheet.cell_value(0, i))
print("Extracting first column in the current sheet")
for i in range(sheet.nrows):
print(sheet.cell_value(i, 0))
print("Extracting rowwise content in the current sheet")
for i in range(sheet.nrows):
print(sheet.row_values(i))
Solve Problems by Coding Solutions - A Complete solution for python programming
Python programt for finding ports opened in the website domain
import socket;
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('www.example.com', port number))
if result == 0:
print ("Port is open")
else:
print ("Port is not open")
Python program to print date and time
import datetime;
print("Today Date & Time:\n");
print(datetime.datetime.today());
print("Today Date & Time:\n");
print(datetime.datetime.today());
Python program to print Hostname, IP, MAC address
import uuid
import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("The Name of this device is: " + hostname)
print("The IP Address of this device is: " + IPAddr)
print ("The MAC address of this device is:",hex(uuid.getnode()))
import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("The Name of this device is: " + hostname)
print("The IP Address of this device is: " + IPAddr)
print ("The MAC address of this device is:",hex(uuid.getnode()))
Python programto find Sine and Cosine Values plot using Matplotlib
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,1)
data1 = np.sin(x)
data2 = np.cos(x)
fig, ax1 = plt.subplots()
color = 'tab:red'
ax1.set_xlabel('x')
ax1.set_ylabel('sine', color=color)
ax1.plot(x, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx()
color = 'tab:green'
ax2.set_ylabel('cosine', color=color)
ax2.plot(x, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)
plt.show()
#Simple Implementation
#---------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
c= np.cos(x)
s= np.sin(x)
plt.plot(x,c)
plt.plot(x,s)
plt.show()
import matplotlib.pyplot as plt
x=np.linspace(0,1)
data1 = np.sin(x)
data2 = np.cos(x)
fig, ax1 = plt.subplots()
color = 'tab:red'
ax1.set_xlabel('x')
ax1.set_ylabel('sine', color=color)
ax1.plot(x, data1, color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx()
color = 'tab:green'
ax2.set_ylabel('cosine', color=color)
ax2.plot(x, data2, color=color)
ax2.tick_params(axis='y', labelcolor=color)
plt.show()
#Simple Implementation
#---------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 256, endpoint=True)
c= np.cos(x)
s= np.sin(x)
plt.plot(x,c)
plt.plot(x,s)
plt.show()
Solving linear mathematical equations with two variable
import numpy as np
A=np.array([[1.5,1],[3.75,4]])
B=np.array([1800,900])
x=np.linalg.solve(A,B)
print("Values of A and B variables:",x)
h=np.allclose(np.dot(A, x), B)
print("Substitution of two variables in equation to validate:",h)
A=np.array([[1.5,1],[3.75,4]])
B=np.array([1800,900])
x=np.linalg.solve(A,B)
print("Values of A and B variables:",x)
h=np.allclose(np.dot(A, x), B)
print("Substitution of two variables in equation to validate:",h)
Operations on Matrices using Python program
import numpy as np
A = np.array([[4,1,7],[2,1,8],[3 ,7,1]])
B = np.array([[6,1,1],[2,1,5],[2,3,1]])
C=A.dot(B)
print("Values of First 2D Matrix\n",A)
print("Values of Second 2D Matrix\n",B)
print("---------------------------------------------")
print("Multiplication of Matrices\n",C)
print("\n")
Ainv=np.linalg.inv(A)
Binv=np.linalg.inv(B)
print("Inverse of First Matrix\n",Ainv)
print("Inverse of Second Matrix\n",Binv)
print("\n")
AI=Ainv.dot(A)
BI=Binv.dot(B)
print("Multiplication of First Matrix and their Inverse\n",AI)
print("Multiplication of Second Matrix and their Inverse\n",BI)
AD=np.linalg.det(A)
BD=np.linalg.det(B)
print("\n")
print("Determinant of First Matricx:",AD)
print("Determinant of Second Matricx:",BD)
print("\n")
ADi=np.diag(A)
BDi=np.diag(B)
print("Diagonal Elements of First Matrix:",ADi)
print("Diagonal Elements of Second Matrix:",BDi)
SADi=np.trace(A)
SBDi=np.trace(B)
print("\n")
print("Sum of Diagonal Elements of First Matrix:",SADi)
print("Sum of Diagonal Elements of Second Matrix:",SBDi)
print("\n")
CA=np.cov(A)
CB=np.cov(B)
print("Covariance matrix of First Matrix\n",CA)
print("Covariance matrix of Second Matrix\n",CB)
print("\n")
ECA=np.linalg.eigh(CA)
ECB=np.linalg.eigh(CB)
print("First array represents eigenvalues and second array represents eigenvectors")
print("\n")
print("covariance matrix eigenvalues eigenvectors of First Matrix\n",ECA)
print("\n")
print("covariance matrix eigenvalues eigenvectors of Second Matrix\n",ECB)
A = np.array([[4,1,7],[2,1,8],[3 ,7,1]])
B = np.array([[6,1,1],[2,1,5],[2,3,1]])
C=A.dot(B)
print("Values of First 2D Matrix\n",A)
print("Values of Second 2D Matrix\n",B)
print("---------------------------------------------")
print("Multiplication of Matrices\n",C)
print("\n")
Ainv=np.linalg.inv(A)
Binv=np.linalg.inv(B)
print("Inverse of First Matrix\n",Ainv)
print("Inverse of Second Matrix\n",Binv)
print("\n")
AI=Ainv.dot(A)
BI=Binv.dot(B)
print("Multiplication of First Matrix and their Inverse\n",AI)
print("Multiplication of Second Matrix and their Inverse\n",BI)
AD=np.linalg.det(A)
BD=np.linalg.det(B)
print("\n")
print("Determinant of First Matricx:",AD)
print("Determinant of Second Matricx:",BD)
print("\n")
ADi=np.diag(A)
BDi=np.diag(B)
print("Diagonal Elements of First Matrix:",ADi)
print("Diagonal Elements of Second Matrix:",BDi)
SADi=np.trace(A)
SBDi=np.trace(B)
print("\n")
print("Sum of Diagonal Elements of First Matrix:",SADi)
print("Sum of Diagonal Elements of Second Matrix:",SBDi)
print("\n")
CA=np.cov(A)
CB=np.cov(B)
print("Covariance matrix of First Matrix\n",CA)
print("Covariance matrix of Second Matrix\n",CB)
print("\n")
ECA=np.linalg.eigh(CA)
ECB=np.linalg.eigh(CB)
print("First array represents eigenvalues and second array represents eigenvectors")
print("\n")
print("covariance matrix eigenvalues eigenvectors of First Matrix\n",ECA)
print("\n")
print("covariance matrix eigenvalues eigenvectors of Second Matrix\n",ECB)
Temperature Conversion Table
def Fah():
F=int(input('Enter the temperature on Fahrenheit(F)'))
C=(F - 32) * 5/9
K=(F - 32) * 5/9 + 273.15
print("Fahrenheit Value :",F)
print("Celsius Value :",C)
print("Kelvin Value:",K)
def Cel():
C=int(input('Enter the temperature on Celsius(C)'))
F=(C * 9/5) + 32
K=C + 273.15
print("Fahrenheit Value :",F)
print("Celsius Value :",C)
print("Kelvin Value:",K)
def Kel():
K=int(input('Enter the temperature on Kelvin(K)'))
F=(K - 273.15) * 9/5 + 32
C=K - 273.15
print("Fahrenheit Value :",F)
print("Celsius Value :",C)
print("Kelvin Value:",K)
print("\n")
print('1.Fahrenheit to Celsius & Kelvin\n2.Celsius to Fahrenheit & Kelvin\n3.Kelvin to Fahrenheit & Celsius\n4.Exit')
n=int(input('Enter the choice:'))
if n==1:
Fah()
elif n==2:
Cel()
elif n==3:
Kel()
elif n==4:
exit()
else:
print('Invalid options')
F=int(input('Enter the temperature on Fahrenheit(F)'))
C=(F - 32) * 5/9
K=(F - 32) * 5/9 + 273.15
print("Fahrenheit Value :",F)
print("Celsius Value :",C)
print("Kelvin Value:",K)
def Cel():
C=int(input('Enter the temperature on Celsius(C)'))
F=(C * 9/5) + 32
K=C + 273.15
print("Fahrenheit Value :",F)
print("Celsius Value :",C)
print("Kelvin Value:",K)
def Kel():
K=int(input('Enter the temperature on Kelvin(K)'))
F=(K - 273.15) * 9/5 + 32
C=K - 273.15
print("Fahrenheit Value :",F)
print("Celsius Value :",C)
print("Kelvin Value:",K)
print("\n")
print('1.Fahrenheit to Celsius & Kelvin\n2.Celsius to Fahrenheit & Kelvin\n3.Kelvin to Fahrenheit & Celsius\n4.Exit')
n=int(input('Enter the choice:'))
if n==1:
Fah()
elif n==2:
Cel()
elif n==3:
Kel()
elif n==4:
exit()
else:
print('Invalid options')
Newton Raphson Method
# Newton Raphson Method
# The Newton-Raphson method (also known as Newton's method) is a way
# to quickly find a good approximation for the root of a real-valued function
xcube=int(input('Enter the values for Xcube: '))
xsquare=int(input('Enter the values for Xsquare: '))
x=int(input('Enter the values for X: '))
constant=int(input('Enter the values for Constant: '))
X0=int(input('Enter the values for inital vaule X0: '))
# It can be any value, but based on the incorrectness the root convergence
# will delay. Here we can use trail and error method for input value.
X1= X0-((((xcube*X0*X0*X0)+(xsquare*X0*X0)+(x*X0)+constant)/((xcube*3*X0*X0)+(xsquare*2*X0)+x)))
print ("Root at first approximations:",X1)
X2= X1-((((xcube*X1*X1*X1)+(xsquare*X1*X1)+(x*X1)+constant)/((xcube*3*X1*X1)+(xsquare*2*X1)+x)))
print ("Root at second approximations:",X2)
X3= X2-((((xcube*X2*X2*X2)+(xsquare*X2*X2)+(x*X2)+constant)/((xcube*3*X2*X2)+(xsquare*2*X2)+x)))
print ("Root at thrid approximations:",X3)
X4= X3-((((xcube*X3*X3*X3)+(xsquare*X3*X3)+(x*X3)+constant)/((xcube*3*X3*X3)+(xsquare*2*X3)+x)))
print ("Root at fourth approximations:",X4)
X5= X4-((((xcube*X4*X4*X4)+(xsquare*X4*X4)+(x*X4)+constant)/((xcube*3*X4*X4)+(xsquare*2*X4)+x)))
print ("Root at fifth approximations:",X5)
# The Newton-Raphson method (also known as Newton's method) is a way
# to quickly find a good approximation for the root of a real-valued function
xcube=int(input('Enter the values for Xcube: '))
xsquare=int(input('Enter the values for Xsquare: '))
x=int(input('Enter the values for X: '))
constant=int(input('Enter the values for Constant: '))
X0=int(input('Enter the values for inital vaule X0: '))
# It can be any value, but based on the incorrectness the root convergence
# will delay. Here we can use trail and error method for input value.
X1= X0-((((xcube*X0*X0*X0)+(xsquare*X0*X0)+(x*X0)+constant)/((xcube*3*X0*X0)+(xsquare*2*X0)+x)))
print ("Root at first approximations:",X1)
X2= X1-((((xcube*X1*X1*X1)+(xsquare*X1*X1)+(x*X1)+constant)/((xcube*3*X1*X1)+(xsquare*2*X1)+x)))
print ("Root at second approximations:",X2)
X3= X2-((((xcube*X2*X2*X2)+(xsquare*X2*X2)+(x*X2)+constant)/((xcube*3*X2*X2)+(xsquare*2*X2)+x)))
print ("Root at thrid approximations:",X3)
X4= X3-((((xcube*X3*X3*X3)+(xsquare*X3*X3)+(x*X3)+constant)/((xcube*3*X3*X3)+(xsquare*2*X3)+x)))
print ("Root at fourth approximations:",X4)
X5= X4-((((xcube*X4*X4*X4)+(xsquare*X4*X4)+(x*X4)+constant)/((xcube*3*X4*X4)+(xsquare*2*X4)+x)))
print ("Root at fifth approximations:",X5)
To read a coordinate point in a XY coordinate system and determine its Quadrant
x=int(input('Enter the values for X'))
y=int(input('Enter the values for Y'))
if x > 0 and y > 0:
print ('x, y point lies in the First quandrant')
elif x < 0 and y > 0:
print ('x, y point lies in the Second quandrant')
elif x < 0 and y < 0:
print ('x, y point lies in the Third quandrant')
elif x > 0 and y < 0:
print ('x, y point lies in the Fourth quandrant')
elif x == 0 and y == 0:
print ('x, y point lies at the origin')
y=int(input('Enter the values for Y'))
if x > 0 and y > 0:
print ('x, y point lies in the First quandrant')
elif x < 0 and y > 0:
print ('x, y point lies in the Second quandrant')
elif x < 0 and y < 0:
print ('x, y point lies in the Third quandrant')
elif x > 0 and y < 0:
print ('x, y point lies in the Fourth quandrant')
elif x == 0 and y == 0:
print ('x, y point lies at the origin')
Subscribe to:
Posts (Atom)