#!/usr/bin/env python # import required modules # import os import sys import numpy as np # a simple function # def add0(x, y): z = x + y return z # a more intesting function # def add1(x = -1, y = -99): z = x + y return z # a more intesting function # def add2(x = -1, y = -1): z = x + y return x, y, z # a more intesting function # def myresize(ml, str): ml.append(str) return ml # main: this is the main function of this Python # def main(argv): # call the simple version # value = add0(float(1.0), float(2.0)) print("value = ", value) # call the simple version with command line values # value = add0(float(sys.argv[1]), float(sys.argv[2])) print("value = ", value) # call the simple version with command line values # value = add1() print("value = ", value) # call the simple version with command line values # value = add1(y = 2, x = 3) print("value = ", value) # call the simple version with command line values # value = add1(y = 12345) print("value = ", value) # call the simple version with command line values # a, b, c = add2(y = 1, x = -1) _, _, value = add2(y = 1, x = -1) print("value = ", value) arg1, _, value = add2(y = 1, x = -1) print("arg1 = %f" % (arg1), "value = %f" % (value)) # dynamic memory allocation # mylist = ["one", "two", "three"] print("Before: ", mylist) myresize(mylist, "four") print("After: ", mylist) print("-------------") # matrices # v1 = np.array([1.5, 2.25, 3.1111], np.float32) print(v1) v2 = np.array([3.0, 2.0, 1.0], np.float32) print(v2) v3 = v1 + v2 print(v3) vi = np.array([8, 9, 10], np.int32) print(vi) print("Sum: ", vi + v1) m4 = [[1], [1, 2], [1, 2, 3]] print(m4) m5 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], np.float32) print(m5) m6 = m5 + m5 print(m6) # exit gracefully # return 0 # begin gracefully # if __name__ == "__main__": main(sys.argv) # # end of file