Posts

Showing posts with the label def
 Prorgram is: # #           Equality elements lists, sets, dictionaries # Elem_A = 10 Elem_B = 10 Elem_C = 15 List_A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List_B = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] List_C = [1,2,3,3,3,6,7,8,9,10] Set_A = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Set_B = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Set_C = {1, 2, 3, 3, 3, 3, 7, 8, 9, 10} Dictionary_A = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:10} Dictionary_B = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, 10:10} Dictionary_C = {1:1, 2:2, 3:3, 4:3, 5:3, 6:3, 7:7, 8:8, 9:9, 10:10} def compare(X,Y):     if X == Y:         Alfa = True     else:         Alfa = False     return Alfa print(' Element equality') Elem_Test_AB = compare(Elem_A,Elem_B) print('Element A is equal to element B? Result is:', Elem_Test_AB) Elem_Test_AC = compare(Elem_A,Elem_C) print('Element A is equal to element C? Result is:', Elem_Test_AC) print(' Lis...

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 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)...

exp(A), A is a matrix

Programe is: # # Calculate A**n, A is a matrix,  n is an integer #                 A has maximum 15 rows and 15 columns # AA =  [[1,2,3,4],       [2,3,4,5],       [3,4,5,6],       [4,5,6,7]] AAA = [[2,2],       [2,2]] BB =  [[1,1,1,1],       [1,1,1,1],       [1,1,1,1],       [2,2,2,2]] NN_row = 4 NN_column = 4 def product_AB(A, B, N_row, N_column):     C =  [[1,1,1,1],       [1,1,1,1],       [1,1,1,1],       [2,2,2,2]]    # C = [[0]*N_row]*N_column     for i in range(N_row):         for j in range(N_column):             C[i][j]=0             for k in range(N_column):                 C[i][j] += A[i][k]*B[k][j]     retur...

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...