Posts

Showing posts with the label return
Programe is:   # #   Function list and parameter list # def Function_1(Word):     print(' One parameter', Word)     return 1 def Function_2(Word):     Word1 = Word[0] + Word[1]     print(Word1)     return 2 def Function_3(Word):     Word2 = Word[0] + Word[1] + Word[2]     print(Word2)     return 3 functions = [Function_1, Function_2, Function_3] results = [0,0,0] WORD1 = "aaaa" WORD2 = "bbbb" WORD3 = "cccc" i = 0 parameters = [[WORD1], [WORD1,WORD2], [WORD1, WORD2, WORD3]] #parameters = ((WORD1), (WORD1,WORD2), (WORD1, WORD2, WORD3)) for ijj in (functions):     results[i] = ijj(parameters[i])     print (results[i], "\n")     i += 1 print(' Final running!') Results are: ========= RESTART: /Users/ionivan/Documents/ListFunctionParametersX.py =========  One parameter ['aaaa'] 1  aaaabbbb 2  aaaabbbbcccc 3   Final running! (April 02, 2022)

Functions than return a function

Programe is: # # Functions than return a function # def function_power(X, NR):     Y = X     for i in range(NR):         Y[i] *= X[i]     return function_add(Y,NR) def function_add(X,NR):     suma = 0     for i in range(NR):         suma += X[i]     return suma a = [1,2,3,4, 5] nr = len(a) b = function_power(a, nr) print("For list a = ", a,  " the sum of the squares of these elements:  ", b) Results are: ========== RESTART: /Users/ionivan/Documents/FunctionReturnFunction.py ========= For list a =  [1, 4, 9, 16, 25]  the sum of the squares of these elements:   55 (April 02, 2022)

Functions and return parameters list

 Program is: # #   functions and return parameters list # def function_1(a,b):     return [a+b] def function_2(a,b):     suma = a + b     if a > b:         minimum =b     else:         minimum = a     return [suma, minimum] def function_3(a,b):     suma = a + b     if a > b:         minimum = b     else:         minimum = a     if a > b:         maximum = a     else:         maximum = b     return [suma, minimum, maximum] X = 10 Y = 20 Z = function_1(X,Y) W = function_2(X,Y) A = function_3(X,Y) print(' If a=', X,  'and b= ', Y, 'than a+b=', Z[0]) print(' If a=', X,  'and b= ', Y, 'than a+b=', W[0], 'minumum is', W[1] ) print(' If a=', X,  'and b= ', Y, 'than a+b=', A[0], 'minumum is', A[1],       'and maximu...

Functions and many return parameters

Programe is:  # #   functions and many return parameters # def function_1(a,b):     return a+b def function_2(a,b):     suma = a + b     if a > b:         minimum =b     else:         minimum = a     return suma, minimum def function_3(a,b):     suma = a + b     if a > b:         minimum = b     else:         minimum = a     if a > b:         maximum = a     else:         maximum = b     return suma, minimum, maximum X = 10 Y = 20 Z = function_1(X,Y) W, V = function_2(X,Y) A, B, C = function_3(X,Y) print(' If a=', X,  'and b= ', Y, 'than a+b=', Z) print(' If a=', X,  'and b= ', Y, 'than a+b=', W, 'minumum is', V ) print(' If a=', X,  'and b= ', Y, 'than a+b=', A, 'minumum is', B,       'and maximum is', C)...

Arithmetic, geometric, harmonic means calculation

# #    Mean_A is weighted arithmetic mean calculation. #    Mean_G is weighted geometric mean calculation. #    Mean_H is weighted harmonic average calculation. #    f[i] - weight data point x[i] #    x[i] - data point i in dataset #    Nr  - lenght dataset # Nr = int(input('Lenght dataset is:')) # # ********** Initialization list ************ # def Initialization_list(nr, string_mane):     termx = ['0']*nr     print(string_mane)     i = 0     while i < nr:         termx[i] = input('Term in dataset is:')         i = i + 1     return termx # # ********** Harmonic wighted mean calculation ************ # def Harmonic_mean(nr, termx, termf):     suma_xf = 0     suma_f = 0     i = 0     while i < nr:         suma_xf = suma_xf +  float(termf [i])/float(termx [i])   ...

Count the positive elements in a list

PYTHON program is: # #    Count the positive elements, the zero elements, #    and the negative elements in a list. #    A function is constructed that returns three values. #        return a, b, c #        x, y, z = Count_list(List) is x,y,z=a,b,c # Count_positive = 0 Count_negative = 0 Count_zero = 0 List_elements_A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List_elements_B = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] List_elements_C = [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10] List_elements_D = [1, 2, 3, 0, 0, 0, 0, -1, -2, -3] def Count_list(List):     C_pos = 0     C_neg = 0     C_zer = 0     No_elem = len(List)     for i in range(No_elem):         if(List[i] > 0):             C_pos = C_pos +1         if(List[i] < 0):             C_neg = C_neg +1         if(List...