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...
Posts
Showing posts with the label list
- Get link
- X
- Other Apps
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
- Get link
- X
- Other Apps
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
- Get link
- X
- Other Apps
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 list in PYTHON
- Get link
- X
- Other Apps
Programe is: # # Functions list # def print_1(Word,val): # print string function Alfa = Word[val-1] print(Alfa) return def sum_x(x,nr): # add list elements function s =0 for i in range(nr): s+= x[i] return s def norma_a(a,nr): # add absolute value list elements function norm = 0 for i in range(nr): norm += abs(a[i]) return norm functions = [print_1, sum_x, norma_a] # functions list results = [0,0,0] WORD = ["aaaa", "bbbb", "cccc", "dddd","eeee", "ffff", "gggg", "hhhh", "mmmm","nnnn"] X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] A = [1, -2, 3, -4, 5, -6, 7, -8, 9, -10] NR = [10, 10, 10] NRR =3 i = 0 parameters = [WORD, X, A] for ijj in (functions): results[i] = ijj(parameters[i], NR[i]) print (results[...
Nested loop in PYTHON
- Get link
- X
- Other Apps
Programe is: (March 28, 2022) # # for loops in PYTHON # for i range(n) i =0, 1, 2, 3, ..., n-1 # for i range(k,n) i=k, k+1, k+2, ..., n-1 # for i reange(k,n,r) i =k,k+r, k+2r,k+3r,..., k+xr and k+xr<n for i in range(10): print(i) nr = int(input('Loops numbers is: ')) sum = 0 for i in range(nr): sum += i print('Sums 0, 1, 2, .., ', nr, 'is: ', sum) # # Odd numbers sum , i < n # n = nr = int(input('Numbers is: ')) for i in range(1, n, 2): print(i) # # for loops în list # cars_A = ["BMW", "FORD", "AUDI", "DACIA", "LAMBORGINI"] cars_B = ["PORSCHE", "TATA", "VOLVO", "SAAB", "PEUGEOT"] print( '\n List cars_A is: ') for i in cars_A: print(i, end = ' ') print( '\n List cars_B is: ') for i in cars_B: print(i, end = ' ') print( '\n List cars_A + cars_B is: ') for i in (cars_A + cars_...
Formated print
- Get link
- X
- Other Apps
PYTHON programe is: # # Formated print # X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Y = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] a = 7 b = 15 print(" a= %5d b= %5d" %(a, b)) nr = len(X) i = 0 while i < nr: print("%5d" %(X[i])) i = i +1 print('List was printed ', ' ') row_number = len(Y) column_number = len(Y[0]) i = 0 while i < row_number: j = 0 while j < column_number: print("%5d " %(Y[i][j]), end =" ") j = j+1 print(" " ) i = i +1 print('List of list (matrix) was printed ', ' ') Results are: Python 3.10.2 (v3.10.2:a58ebcc701, Jan 13 2022, 14:50:16) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin Type "help", "copyright", "credits" or "license()" for more information. ============== RESTART: /Users/ionivan/Documents/FormatedPrint.py ============== a= 7 b=...
Arithmetic, geometric, harmonic means calculation
- Get link
- X
- Other Apps
# # 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]) ...
Lists operators
- Get link
- X
- Other Apps
# # String and lists operations # # len() - components number list # + - concatenate list operator # set() - list conversion în set # & - intersection operator in setss String = 'This is a string' Word_list_A = ['AUDI ', 'FORD ', 'DACIA ', 'TATA', 'RENAULT ', 'CITROEN', 'LAMBORGHINI ', 'FERRARI' , 'VOLKSWAGEN', 'ASTON MARTIN' 'BUICK ', 'ALFA ROMEO ', 'CRYSLER ', 'BMW'] Word_list_B = ['LADA', 'SKODA', 'VOLGA', 'TATRA', 'PEUGEOT', 'SAAB', 'VOLVO', 'TOYOTA', 'HYUNDAI', 'SUBARU'] Length_A = len(Word_list_A) Length_B = len(Word_list_B) pri...
Count the positive elements in a list
- Get link
- X
- Other Apps
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...