def printMessage(): #Function to Print Message
print("Programming is Fun.\n") #Print Statement
def main(): #Main() function
printMessage()
if __name__ =='__main__': #Setting Top-level conditional script
main()
def printMessage(): #Function to Print Message
print("Programming is Fun.\n") #Print Statement
def main(): #Main() function
printMessage() #First Function call
printMessage() #Second Function Call
if __name__ =='__main__': #Setting Top-level conditional script
main()
def printMessage(): #Function to Print Message
print("Programming is Fun.\n") #Print Statement
def main():
for i in range (1,6): #Main() function
printMessage()
if __name__ =='__main__': #Setting Top-level script environment
main()
def calculateTriangularNumber(n): #Function to calculate triangular number
triangularNumber=0 #Variable Dclaration
for i in range (1,n+1): #Calculation/Iteration
triangularNumber+=i
print("Triangular Number {0} is {1}".format(n,triangularNumber))
def main():
calculateTriangularNumber(10) #Function call-1
calculateTriangularNumber(20) #Function call-2
calculateTriangularNumber(50) #Function call-3
if __name__=='__main__': #Setting top level conditional script
main()
import sys
def gcd(u,v): #Function to calculate gcd
sys.stdout.write("gcd of {0} and {1} is: ".format(u,v))
while(v!=0):
temp=u%v
u=v
v=temp
sys.stdout.write("{0}\n".format(u))
def main(): #Main() function
gcd(150,35)
gcd(1026,405)
gcd(83,240)
if __name__=='__main__': #Setting Top level conditional script
main()
import sys
def gcd(u,v): #Function to calculate gcd
while(v!=0):
temp=u%v
u=v
v=temp
return u
def main(): #Main() function
result= gcd(150,35)
print("the gcd of 150 and 35 is: {0}".format(result))
result=gcd(1026,405)
print("the gcd of 1026 and 405 is: {0}".format(result))
result= gcd(83,240)
print("the gcd of 83 and 240 is: {0}".format(result))
if __name__=='__main__': #Top level conditional script
main()
def absoluteValue(x): #Function to calculate & return absolute values
if(x<0):
x=-x
return x
def main(): #Main() Function
f1=-15.5
f2=20.0
f3=-5.0
il=-716
result=absoluteValue(f1)
print("result= {0:.2f}".format(result))
print("f1={0:.2f}".format(f1))
result=absoluteValue(f2)+absoluteValue(f3)
print("result= {0:.2f}".format(result))
result=absoluteValue(float(il))
print("result= {0:.2f}".format(result))
result=absoluteValue(il)
print("resut= {0:.2f}".format(result))
print("{0:.2f}".format(absoluteValue((-6.0)/4)))
if __name__=='__main__': #Setting Top level conditional script
main()
def absoluteValue(x):
if(x<0):
x=-x
return x
def squareRoot(x):
epsilon=0.0001
guess=1.0
while(absoluteValue(guess*guess-x)>=epsilon):
guess=(x/guess+guess)/2.0
return guess
def main():
print("squareRoot (2.0) = {0}".format(squareRoot (2.0)));
print("squareRoot (144.0) = {0}".format(squareRoot (144.0)));
print("squareRoot (17.5) = {0}".format(squareRoot (17.5)));
if __name__=='__main__':
main()
def minimum(values):
minValue=values[0] #Variable Declaration
for i in range (1,10): #Calculation
if(values[i]<minValue):
minValue=values[i]
return minValue
def main():
scores=[] #Variable Declaration
print("Enter 10 scores:")
for i in range (0,10):
scores.append(5) #score=5 for all cases
#scores.append(input())
minScore=minimum(scores) #Function call & assignment
print("\nMinimum score is {0}\n".format(minScore)) #Result
if __name__=='__main__':
main()
def minimum(values,numberOfElements):
minValue=values[0] #Variable Declaration
for i in range(1,numberOfElements): #Iteration/Calculations
if(values[i]<minValue):
minValue=values[i]
return minValue
def main():
#List/Variable Delcaration
array1 = [ 157, -28, -37, 26, 10 ]
array2 = [ 12, 45, 1, 10, 5, 3, 22 ]
#Result
print("array1 minimum: {0}".format(minimum (array1, 5)));
print("array2 minimum: {0}".format(minimum (array2, 7)));
if __name__=='__main__':
main()
import sys
def multiplyBy2(array,n):
for i in range (0,n): #Iteration/Calculation
array[i]*=2
def main():
#List/Variable Declaration
floatVals=[1.2, -3.7, 6.2, 8.55]
multiplyBy2(floatVals,4)
for i in range (0,4):
sys.stdout.write("{0:.2f} ".format(floatVals[i])) #Result
print("\n")
if __name__=='__main__':
main()
import sys
def sort(a,n):
for i in range (0,n-1): #Calculations
for j in range (i+1,n):
if(a[i]>a[j]): #Conditional swapping
temp=a[i]
a[i]=a[j]
a[j]=temp
def main():
#List/Variable Declaration
array= [ 34, -5, 6, 0, 12, 100, 56, 22,\
44, -3, -9, 12, 17, 22, 6, 11 ]
print("The array before the sort:\n");
for i in range (0,16):
sys.stdout.write("{0} ".format(array[i]))
sort (array, 16); #Function Call
print("\n\n\nThe array after the sort:\n")
for i in range (0,16):
sys.stdout.write("{0} ".format(array[i]))
print("\n")
if __name__=='__main__':
main()
import sys
def main():
#List/Variable Declaration
sampleMatrix =[ [ 7, 16, 55, 13, 12 ],
[ 12, 10, 52, 0, 7 ],
[ -2, 1, 2, 4, 9 ] ]
print("Original matrix:\n")
displayMatrix(sampleMatrix) #Function call-1
scalarMultiply(sampleMatrix, 2) #Function Call-2
print("\nMultiplied by 2:\n")
displayMatrix(sampleMatrix); #Function call-3
scalarMultiply(sampleMatrix, -1) #Function call-4
print("\nThen multiplied by -1:\n")
displayMatrix(sampleMatrix) #Function call-5
def scalarMultiply(matrix,scalar):
for row in range(0,3): #Calculation
for column in range(0,5):
matrix[row][column]*=scalar
def displayMatrix(matrix):
for row in range(0,3): #Result
for column in range(0,5):
sys.stdout.write("{0:7}".format(matrix[row][column]))
sys.stdout.write("\n")
if __name__=='__main__':
main()
import sys
def main():
#List/Variable Declaration
sampleMatrix =[ [ 7, 16, 55, 13, 12 ],
[ 12, 10, 52, 0, 7 ],
[ -2, 1, 2, 4, 9 ] ]
print("Original matrix:\n")
displayMatrix(3,5,sampleMatrix) #Function call-1
scalarMultiply(3,5,sampleMatrix, 2) #Function Call-2
print("\nMultiplied by 2:\n")
displayMatrix(3,5,sampleMatrix); #Function call-3
scalarMultiply(3,5,sampleMatrix, -1) #Function call-4
print("\nThen multiplied by -1:\n")
displayMatrix(3,5,sampleMatrix) #Function call-5
def scalarMultiply(nRows,nCols,matrix,scalar):
for row in range(0,nRows): #Calculation
for column in range(0,nCols):
matrix[row][column]*=scalar
def displayMatrix(nRows,nCols,matrix):
for row in range(0,nRows): #Result
for column in range(0,nCols):
sys.stdout.write("{0:7}".format(matrix[row][column]))
sys.stdout.write("\n")
if __name__=='__main__':
main()
import sys
def getNumberAndBase():
#Global Reference
global digit
global numberToConvert
global base
#Variable Declaration
digit=0
numberToConvert=420 #numberToConvert=input("Number to be converted ?")
base=8 #base=input("Base?")
if(base<2 or base>16):
print("Bad base - must be between 2 and 16\n");
base = 10;
def convertNumber():
#Global Reference
global numberToConvert
global base
global convertedNumber
global digit
convertedNumber=[0]*64 #List declaration
while(numberToConvert!=0):
convertedNumber[digit]=numberToConvert%base #Calculations
digit=digit+1
numberToConvert/=base
def displayConvertedNumber():
#Global reference
global baseDigits
global digit
#List/Variable Declaration
baseDigits=[ '0', '1', '2', '3', '4', '5', '6', '7',\
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ]
sys.stdout.write("Converted number = ")
digit=digit-1
for i in range(digit,-1,-1):
nextDigit = convertedNumber[i];
sys.stdout.write("{0}".format(baseDigits[nextDigit])); #Result
sys.stdout.write("\n")
def main():
getNumberAndBase() #Function call-1
convertNumber() #Function call-2
displayConvertedNumber() #Function call-3
if __name__=='__main__':
main()
def auto_static():
global staticVar #Global reference
autoVar=1 #variable Declaration
print("automatic = {0}, static = {1}\n".format(autoVar,staticVar)); #Result
#Calculation
autoVar+=1
staticVar+=1
def main():
global staticVar
staticVar=1 #Variable Declaration
for i in range(0,5):
auto_static()
if __name__=='__main__':
main()
def factorial(n):
if( n == 0 ): #Calculation
result = 1
else:
result = n * factorial (n - 1)
return result;
def main():
for j in range (0,11):
print("{0:3}! = {1}\n".format(j,factorial (j)))
if __name__=='__main__':
main()