"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

May 20, 2016

Day #24 - Python Code Examples

Examples for - for loop, while loop, dictionary, function examples and plotting graphs
#Example 1 - Working on Float Variables
print 'Program 1'
i = 5.44
j = 3.0
print j/i
#Example 2 - For loop
print 'Program 2'
for i in range(1,10):
print i
i = i+1
#Example 3 - While Loop with break condition
print 'Program 3'
print 'while loop'
i = 1
while(i<10):
print i
i = i+1
if(i>5):
break
print 'Program 4'
#Example 4 - Working with Functions and Dictionary
def computevalues(initval):
f_ = {}
i = 0
while(i<10):
try:
f_[i] = i
i = i+1
except Exception:
pass
return f_
f_ = {}
f_ = computevalues(0.01)
print 'f_'
for key,value in f_.iteritems():
print value
#Example 5 - Plot Graphs
print 'Program 5'
import matplotlib.pyplot as plt
x = [-.001,-0.0005,-0.0001,-0.00005,-0.00001]
y = [103,178,523,738,880]
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.plot(x,y,'-o', color = 'g')
plt.title("Demo X,Y Graph")
plt.show()
#Matrix and SVD
import numpy as np
from sympy import Matrix
b = np.arange(1,50,1)
z = np.eye(3,3)
print(z)
print("b")
print(b)
a = np.matrix([[2,2,5],[-4,2,3],[0,0,0]])
print("eig vector")
print(np.linalg.eig(a))
print("eig vals")
print(np.linalg.eigvals(a))
print("svd")
print(np.linalg.svd(a))
print("null space")
A = Matrix(a)
print(A*A.nullspace()[0])
view raw pythonbasics.py hosted with ❤ by GitHub
Happy Learning!!

No comments: